Skip to content

Commit 4a4a533

Browse files
committed
add e2e tests for tracing
1 parent 38ff4c1 commit 4a4a533

File tree

10 files changed

+586
-14
lines changed

10 files changed

+586
-14
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>software.amazon.lambda</groupId>
6+
<artifactId>E2ETracingFunction</artifactId>
7+
<version>1.12.3</version>
8+
<packaging>jar</packaging>
9+
<name>A sample Hello World using powertools tracing</name>
10+
11+
<properties>
12+
<log4j.version>2.19.0</log4j.version>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<lambda.powertools.version>1.12.3</lambda.powertools.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>software.amazon.lambda</groupId>
20+
<artifactId>powertools-tracing</artifactId>
21+
<version>${lambda.powertools.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.amazonaws</groupId>
25+
<artifactId>aws-lambda-java-core</artifactId>
26+
<version>1.2.1</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.amazonaws</groupId>
30+
<artifactId>aws-lambda-java-events</artifactId>
31+
<version>3.11.0</version>
32+
</dependency>
33+
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>aspectj-maven-plugin</artifactId>
41+
<version>1.14.0</version>
42+
<configuration>
43+
<source>${maven.compiler.source}</source>
44+
<target>${maven.compiler.target}</target>
45+
<complianceLevel>${maven.compiler.target}</complianceLevel>
46+
<aspectLibraries>
47+
<aspectLibrary>
48+
<groupId>software.amazon.lambda</groupId>
49+
<artifactId>powertools-tracing</artifactId>
50+
</aspectLibrary>
51+
</aspectLibraries>
52+
</configuration>
53+
<executions>
54+
<execution>
55+
<goals>
56+
<goal>compile</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-shade-plugin</artifactId>
64+
<version>3.2.4</version>
65+
<configuration>
66+
<createDependencyReducedPom>false</createDependencyReducedPom>
67+
<finalName>function</finalName>
68+
</configuration>
69+
<executions>
70+
<execution>
71+
<phase>package</phase>
72+
<goals>
73+
<goal>shade</goal>
74+
</goals>
75+
<configuration>
76+
<transformers>
77+
<transformer
78+
implementation="io.github.edwgiz.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer">
79+
</transformer>
80+
</transformers>
81+
</configuration>
82+
</execution>
83+
</executions>
84+
<dependencies>
85+
<dependency>
86+
<groupId>io.github.edwgiz</groupId>
87+
<artifactId>log4j-maven-shade-plugin-extensions</artifactId>
88+
<version>2.17.2</version>
89+
</dependency>
90+
</dependencies>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-compiler-plugin</artifactId>
95+
<version>3.10.1</version>
96+
<configuration>
97+
<source>${maven.compiler.source}</source>
98+
<target>${maven.compiler.target}</target>
99+
</configuration>
100+
</plugin>
101+
</plugins>
102+
</build>
103+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.lambda.powertools.tracing.Tracing;
6+
import software.amazon.lambda.powertools.tracing.TracingUtils;
7+
8+
public class Function implements RequestHandler<Input, String> {
9+
10+
@Tracing
11+
public String handleRequest(Input input, Context context) {
12+
try {
13+
Thread.sleep(100); // simulate stuff
14+
} catch (InterruptedException e) {
15+
throw new RuntimeException(e);
16+
}
17+
String message = buildMessage(input.getMessage(), context.getFunctionName());
18+
19+
TracingUtils.withSubsegment("internal_stuff", subsegment -> {
20+
try {
21+
Thread.sleep(100); // simulate stuff
22+
} catch (InterruptedException e) {
23+
throw new RuntimeException(e);
24+
}
25+
});
26+
27+
return message;
28+
}
29+
30+
@Tracing
31+
private String buildMessage(String message, String funcName) {
32+
TracingUtils.putAnnotation("message", message);
33+
try {
34+
Thread.sleep(150); // simulate other stuff
35+
} catch (InterruptedException e) {
36+
throw new RuntimeException(e);
37+
}
38+
return String.format("%s (%s)", message, funcName);
39+
}
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
public class Input {
4+
private String message;
5+
6+
public Input() {
7+
}
8+
9+
public String getMessage() {
10+
return message;
11+
}
12+
13+
public void setMessage(String message) {
14+
this.message = message;
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Root>
12+
<Logger name="JsonLogger" level="INFO" additivity="false">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Logger>
15+
</Loggers>
16+
</Configuration>

powertools-e2e-tests/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
<maven.compiler.target>11</maven.compiler.target>
1818
<constructs.version>10.1.138</constructs.version>
1919
<cdk.version>2.47.0</cdk.version>
20+
<lombok.version>1.18.24</lombok.version>
2021
</properties>
2122

2223
<dependencies>
24+
<dependency>
25+
<groupId>org.projectlombok</groupId>
26+
<artifactId>lombok</artifactId>
27+
<version>${lombok.version}</version>
28+
<scope>provided</scope>
29+
</dependency>
2330

2431
<dependency>
2532
<groupId>ch.qos.logback</groupId>
@@ -128,6 +135,28 @@
128135
</dependency>
129136
</dependencies>
130137

138+
<build>
139+
<plugins>
140+
<plugin>
141+
<groupId>org.apache.maven.plugins</groupId>
142+
<artifactId>maven-compiler-plugin</artifactId>
143+
<version>3.10.1</version>
144+
<configuration>
145+
<source>${maven.compiler.source}</source>
146+
<target>${maven.compiler.target}</target>
147+
<annotationProcessorPaths>
148+
<path>
149+
<groupId>org.projectlombok</groupId>
150+
<artifactId>lombok</artifactId>
151+
<version>${lombok.version}</version>
152+
</path>
153+
</annotationProcessorPaths>
154+
<encoding>UTF-8</encoding>
155+
</configuration>
156+
</plugin>
157+
</plugins>
158+
</build>
159+
131160
<profiles>
132161
<profile>
133162
<id>it</id>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package software.amazon.lambda.powertools;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.Timeout;
7+
import software.amazon.lambda.powertools.testutils.Infrastructure;
8+
import software.amazon.lambda.powertools.testutils.InvocationResult;
9+
import software.amazon.lambda.powertools.testutils.tracing.SegmentDocument.SubSegment;
10+
import software.amazon.lambda.powertools.testutils.tracing.Trace;
11+
import software.amazon.lambda.powertools.testutils.tracing.TraceFetcher;
12+
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.UUID;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
public class TracingE2ETest {
21+
private static Infrastructure infrastructure;
22+
private static final String service = "TracingE2EService_"+UUID.randomUUID(); // "TracingE2EService_e479fb27-422b-4107-9f8c-086c62e1cd12";
23+
24+
@BeforeAll
25+
@Timeout(value = 5, unit = TimeUnit.MINUTES)
26+
public static void setup() {
27+
infrastructure = Infrastructure.builder()
28+
.testName(TracingE2ETest.class.getSimpleName())
29+
.pathToFunction("tracing")
30+
.tracing(true)
31+
.environmentVariables(new HashMap<>() {{
32+
put("POWERTOOLS_SERVICE_NAME", service);
33+
}}
34+
)
35+
.build();
36+
infrastructure.deploy();
37+
}
38+
39+
@AfterAll
40+
public static void tearDown() {
41+
if (infrastructure != null)
42+
infrastructure.destroy();
43+
}
44+
45+
@Test
46+
public void test_tracing() {
47+
// GIVEN
48+
String message = "Hello World";
49+
String event = String.format("{\"message\":\"%s\"}", message);
50+
String result = String.format("%s (%s)", message, infrastructure.getFunctionName());
51+
52+
// WHEN
53+
InvocationResult invocationResult = infrastructure.invokeFunction(event);
54+
55+
// THEN
56+
Trace trace = TraceFetcher.builder()
57+
.start(invocationResult.getStart())
58+
.end(invocationResult.getEnd())
59+
// .start(Instant.ofEpochSecond(1667468280))
60+
// .end(Instant.ofEpochSecond(1667468340))
61+
.functionName(infrastructure.getFunctionName())
62+
// .functionName("TracingE2ETest-744e0e5ba909-function")
63+
.build()
64+
.fetchTrace();
65+
66+
assertThat(trace.getSubsegments()).hasSize(1);
67+
SubSegment handleRequest = trace.getSubsegments().get(0);
68+
assertThat(handleRequest.getName()).isEqualTo("## handleRequest");
69+
assertThat(handleRequest.getAnnotations()).hasSize(2);
70+
assertThat(handleRequest.getAnnotations().get("ColdStart")).isEqualTo(true);
71+
assertThat(handleRequest.getAnnotations().get("Service")).isEqualTo(service);
72+
assertThat(handleRequest.getMetadata()).hasSize(1);
73+
Map<String, Object> metadata = (Map<String, Object>) handleRequest.getMetadata().get(service);
74+
assertThat(metadata.get("handleRequest response")).isEqualTo(result);
75+
assertThat(handleRequest.getSubsegments()).hasSize(2);
76+
77+
SubSegment sub = handleRequest.getSubsegments().get(0);
78+
assertThat(sub.getName()).isIn("## internal_stuff", "## buildMessage");
79+
80+
sub = handleRequest.getSubsegments().get(1);
81+
assertThat(sub.getName()).isIn("## internal_stuff", "## buildMessage");
82+
83+
SubSegment buildMessage = handleRequest.getSubsegments().stream().filter(subSegment -> subSegment.getName().equals("## buildMessage")).findFirst().orElse(null);
84+
assertThat(buildMessage).isNotNull();
85+
assertThat(buildMessage.getAnnotations()).hasSize(1);
86+
assertThat(buildMessage.getAnnotations().get("message")).isEqualTo(message);
87+
assertThat(buildMessage.getMetadata()).hasSize(1);
88+
metadata = (Map<String, Object>) buildMessage.getMetadata().get(service);
89+
assertThat(metadata.get("buildMessage response")).isEqualTo(result);
90+
}
91+
}

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/testutils/Infrastructure.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
import software.amazon.awscdk.services.logs.RetentionDays;
2424
import software.amazon.awscdk.services.s3.assets.AssetOptions;
2525
import software.amazon.awssdk.core.SdkBytes;
26-
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
27-
import software.amazon.awssdk.core.retry.RetryPolicy;
28-
import software.amazon.awssdk.core.retry.backoff.EqualJitterBackoffStrategy;
2926
import software.amazon.awssdk.core.waiters.WaiterResponse;
3027
import software.amazon.awssdk.http.SdkHttpClient;
3128
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
@@ -43,10 +40,6 @@
4340
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
4441
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
4542
import software.amazon.awssdk.services.sts.StsClient;
46-
import software.amazon.awssdk.services.xray.XRayClient;
47-
import software.amazon.awssdk.services.xray.model.GetTraceSummariesRequest;
48-
import software.amazon.awssdk.services.xray.model.GetTraceSummariesResponse;
49-
import software.amazon.awssdk.services.xray.model.TimeRangeType;
5043
import software.amazon.awssdk.utils.StringUtils;
5144
import software.amazon.lambda.powertools.utilities.JsonConfig;
5245

@@ -63,7 +56,7 @@
6356
import static java.util.Collections.singletonList;
6457

6558
public class Infrastructure {
66-
private static Logger LOG = LoggerFactory.getLogger(Infrastructure.class);
59+
private static final Logger LOG = LoggerFactory.getLogger(Infrastructure.class);
6760

6861
private final String stackName;
6962
private final boolean tracing;
@@ -77,7 +70,6 @@ public class Infrastructure {
7770
private final LambdaClient lambda;
7871
private final CloudFormationClient cfn;
7972
private final CloudWatchClient cloudwatch;
80-
private final XRayClient xray;
8173
private final Region region;
8274
private final String account;
8375
private final String idempotencyTable;
@@ -119,10 +111,6 @@ private Infrastructure(Builder builder) {
119111
.httpClient(httpClient)
120112
.region(region)
121113
.build();
122-
xray = XRayClient.builder()
123-
.httpClient(httpClient)
124-
.region(region)
125-
.build();
126114
cfn = CloudFormationClient.builder()
127115
.httpClient(httpClient)
128116
.region(region)
@@ -234,7 +222,7 @@ public static class Builder {
234222
public Infrastructure build() {
235223
Objects.requireNonNull(testName, "testName must not be null");
236224

237-
String uuid = UUID.randomUUID().toString().substring(0, 13);
225+
String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
238226
stackName = testName + "-" + uuid;
239227

240228
Objects.requireNonNull(pathToFunction, "pathToFunction must not be null");

0 commit comments

Comments
 (0)