Skip to content

Commit dd9df6d

Browse files
chore(docs): Clarify test config needed for Tracing module (aws-powertools#735)
1 parent 27f9f02 commit dd9df6d

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

docs/core/tracing.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,89 @@ under a subsegment, or you are doing multithreaded programming. Refer examples b
243243

244244
User should make sure to instrument the SDK clients explicitly based on the function dependency. Refer details on
245245
[how to instrument SDK client with Xray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-awssdkclients.html) and [outgoing http calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-httpclients.html).
246+
247+
## Testing your code
248+
249+
When using `@Tracing` annotation, your Junit test cases needs to be configured to create parent Segment required by [AWS X-Ray SDK for Java](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java.html).
250+
251+
Below are two ways in which you can configure your tests.
252+
253+
#### Configure environment variable on project level (Recommended)
254+
255+
You can choose to configure environment variable on project level for your test cases run. This is recommended approach as it will avoid the need of configuring each test case specifically.
256+
257+
Below are examples configuring your maven/gradle projects. You can choose to configure it differently as well as long as you are making sure that environment variable `LAMBDA_TASK_ROOT` is set. This variable is
258+
used internally via AWS X-Ray SDK to configure itself properly for lambda runtime.
259+
260+
=== "Maven (pom.xml)"
261+
262+
```xml hl_lines="4-13"
263+
<build>
264+
...
265+
<plugins>
266+
<!-- Configures environment variable to avoid initialization of AWS X-Ray segments for each tests-->
267+
<plugin>
268+
<groupId>org.apache.maven.plugins</groupId>
269+
<artifactId>maven-surefire-plugin</artifactId>
270+
<configuration>
271+
<environmentVariables>
272+
<LAMBDA_TASK_ROOT>handler</LAMBDA_TASK_ROOT>
273+
</environmentVariables>
274+
</configuration>
275+
</plugin>
276+
</plugins>
277+
</build>
278+
279+
```
280+
281+
=== "Gradle (build.gradle) "
282+
283+
```json hl_lines="2-4"
284+
// Configures environment variable to avoid initialization of AWS X-Ray segments for each tests
285+
test {
286+
environment "LAMBDA_TASK_ROOT", "handler"
287+
}
288+
```
289+
290+
#### Configure test cases (Not Recommended)
291+
292+
You can choose to configure each of your test case instead as well if you choose not to configure environment variable on project level.
293+
Below is an example configuration needed for each test case.
294+
295+
=== "AppTest.java"
296+
297+
```java hl_lines="10 11 12 17 18 19 20 21 22 23 24"
298+
import com.amazonaws.xray.AWSXRay;
299+
import org.junit.After;
300+
import org.junit.Before;
301+
import org.junit.Test;
302+
303+
public class AppTest {
304+
305+
@Before
306+
public void setup() {
307+
if(null == System.getenv("LAMBDA_TASK_ROOT")) {
308+
AWSXRay.beginSegment("test");
309+
}
310+
}
311+
312+
@After
313+
public void tearDown() {
314+
// Needed when using sam build --use-container
315+
if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) {
316+
AWSXRay.endSubsegment();
317+
}
318+
319+
if(null == System.getenv("LAMBDA_TASK_ROOT")) {
320+
AWSXRay.endSegment();
321+
}
322+
}
323+
324+
@Test
325+
public void successfulResponse() {
326+
// test logic
327+
}
328+
```
329+
330+
331+

0 commit comments

Comments
 (0)