Skip to content

feat(tracing): add service annotation #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/core/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The Powertools service name is used as the X-Ray namespace. This can be set usin

### Lambda handler

To enable Powertools tracing to your function add the `@Tracing annotation to your `handleRequest` method or on
To enable Powertools tracing to your function add the `@Tracing` annotation to your `handleRequest` method or on
any method will capture the method as a separate subsegment automatically. You can optionally choose to customize
segment name that appears in traces.

Expand Down Expand Up @@ -81,6 +81,13 @@ segment name that appears in traces.
}
```

When using this `@Tracing` annotation, Utility performs these additional tasks to ease operations:

* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead.
* Creates a `Service` annotation if service parameter or `POWERTOOLS_SERVICE_NAME` is set.
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata.


By default, this annotation will automatically record method responses and exceptions. You can change the default behavior by setting
the environment variables `POWERTOOLS_TRACER_CAPTURE_RESPONSE` and `POWERTOOLS_TRACER_CAPTURE_ERROR` as needed. Optionally, you can override behavior by
different supported `captureMode` to record response, exception or both.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Object around(ProceedingJoinPoint pjp,

if (placedOnHandlerMethod(pjp)) {
segment.putAnnotation("ColdStart", isColdStart());
segment.putAnnotation("Service", namespace(tracing));
}

boolean captureResponse = captureResponse(tracing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ void shouldCaptureNonHandlerMethodWithCustomSegmentName() {
void shouldCaptureTraces() {
requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand All @@ -123,12 +124,13 @@ void shouldCaptureTracesWithExceptionMetaData() {

Throwable exception = catchThrowable(() -> requestHandler.handleRequest(new Object(), context));

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand All @@ -144,12 +146,13 @@ void shouldCaptureTracesWithExceptionMetaData() {
void shouldCaptureTracesForStream() throws IOException {
streamHandler.handleRequest(new ByteArrayInputStream("test".getBytes()), new ByteArrayOutputStream(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "streamHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand All @@ -162,13 +165,13 @@ void shouldNotCaptureTracesNotEnabled() throws IOException {
requestHandler = new PowerToolDisabled();
requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.isEmpty();

streamHandler = new PowerToolDisabledForStream();
streamHandler.handleRequest(new ByteArrayInputStream("test".getBytes()), new ByteArrayOutputStream(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.isEmpty();
}

Expand All @@ -178,12 +181,13 @@ void shouldCaptureTracesWithNoMetadata() {

requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "service_undefined");

assertThat(subsegment.getMetadata())
.isEmpty();
Expand All @@ -196,12 +200,13 @@ void shouldCaptureTracesForStreamWithNoMetadata() throws IOException {

streamHandler.handleRequest(new ByteArrayInputStream("test".getBytes()), new ByteArrayOutputStream(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "service_undefined");

assertThat(subsegment.getMetadata())
.isEmpty();
Expand All @@ -214,12 +219,13 @@ void shouldCaptureTracesWithNoMetadataDeprecated() {

requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "service_undefined");

assertThat(subsegment.getMetadata())
.isEmpty();
Expand All @@ -234,12 +240,13 @@ void shouldNotCaptureTracesIfDisabledViaEnvironmentVariable() {

requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.isEmpty();
Expand All @@ -255,12 +262,13 @@ void shouldCaptureTracesIfExplicitlyEnabledAndEnvironmentVariableIsDisabled() {

requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand All @@ -280,12 +288,13 @@ void shouldCaptureTracesIfExplicitlyEnabledBothAndEnvironmentVariableIsDisabled(

requestHandler.handleRequest(new Object(), context);

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand All @@ -303,12 +312,13 @@ void shouldNotCaptureTracesWithExceptionMetaDataIfDisabledViaEnvironmentVariable

catchThrowable(() -> requestHandler.handleRequest(new Object(), context));

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.isEmpty();
Expand All @@ -325,12 +335,13 @@ void shouldCaptureTracesWithExceptionMetaDataEnabledExplicitlyAndEnvironmentVari

Throwable exception = catchThrowable(() -> requestHandler.handleRequest(new Object(), context));

assertThat(AWSXRay.getTraceEntity().getSubsegments())
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
.hasSize(1)
.allSatisfy(subsegment -> {
assertThat(subsegment.getAnnotations())
.hasSize(1)
.containsEntry("ColdStart", true);
.hasSize(2)
.containsEntry("ColdStart", true)
.containsEntry("Service", "lambdaHandler");

assertThat(subsegment.getMetadata())
.hasSize(1)
Expand Down