Skip to content

Commit 527a4e8

Browse files
feat(tracing): add service annotation (#655)
1 parent d922570 commit 527a4e8

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

docs/core/tracing.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The Powertools service name is used as the X-Ray namespace. This can be set usin
4242

4343
### Lambda handler
4444

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

@@ -81,6 +81,13 @@ segment name that appears in traces.
8181
}
8282
```
8383

84+
When using this `@Tracing` annotation, Utility performs these additional tasks to ease operations:
85+
86+
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead.
87+
* Creates a `Service` annotation if service parameter or `POWERTOOLS_SERVICE_NAME` is set.
88+
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata.
89+
90+
8491
By default, this annotation will automatically record method responses and exceptions. You can change the default behavior by setting
8592
the environment variables `POWERTOOLS_TRACER_CAPTURE_RESPONSE` and `POWERTOOLS_TRACER_CAPTURE_ERROR` as needed. Optionally, you can override behavior by
8693
different supported `captureMode` to record response, exception or both.

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/internal/LambdaTracingAspect.java

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public Object around(ProceedingJoinPoint pjp,
5050

5151
if (placedOnHandlerMethod(pjp)) {
5252
segment.putAnnotation("ColdStart", isColdStart());
53+
segment.putAnnotation("Service", namespace(tracing));
5354
}
5455

5556
boolean captureResponse = captureResponse(tracing);

powertools-tracing/src/test/java/software/amazon/lambda/powertools/tracing/internal/LambdaTracingAspectTest.java

+46-35
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ void shouldCaptureNonHandlerMethodWithCustomSegmentName() {
104104
void shouldCaptureTraces() {
105105
requestHandler.handleRequest(new Object(), context);
106106

107-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
107+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
108108
.hasSize(1)
109109
.allSatisfy(subsegment -> {
110110
assertThat(subsegment.getAnnotations())
111-
.hasSize(1)
112-
.containsEntry("ColdStart", true);
111+
.hasSize(2)
112+
.containsEntry("ColdStart", true)
113+
.containsEntry("Service", "lambdaHandler");
113114

114115
assertThat(subsegment.getMetadata())
115116
.hasSize(1)
@@ -123,12 +124,13 @@ void shouldCaptureTracesWithExceptionMetaData() {
123124

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

126-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
127+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
127128
.hasSize(1)
128129
.allSatisfy(subsegment -> {
129130
assertThat(subsegment.getAnnotations())
130-
.hasSize(1)
131-
.containsEntry("ColdStart", true);
131+
.hasSize(2)
132+
.containsEntry("ColdStart", true)
133+
.containsEntry("Service", "lambdaHandler");
132134

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

147-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
149+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
148150
.hasSize(1)
149151
.allSatisfy(subsegment -> {
150152
assertThat(subsegment.getAnnotations())
151-
.hasSize(1)
152-
.containsEntry("ColdStart", true);
153+
.hasSize(2)
154+
.containsEntry("ColdStart", true)
155+
.containsEntry("Service", "streamHandler");
153156

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

165-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
168+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
166169
.isEmpty();
167170

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

171-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
174+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
172175
.isEmpty();
173176
}
174177

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

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

181-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
184+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
182185
.hasSize(1)
183186
.allSatisfy(subsegment -> {
184187
assertThat(subsegment.getAnnotations())
185-
.hasSize(1)
186-
.containsEntry("ColdStart", true);
188+
.hasSize(2)
189+
.containsEntry("ColdStart", true)
190+
.containsEntry("Service", "service_undefined");
187191

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

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

199-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
203+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
200204
.hasSize(1)
201205
.allSatisfy(subsegment -> {
202206
assertThat(subsegment.getAnnotations())
203-
.hasSize(1)
204-
.containsEntry("ColdStart", true);
207+
.hasSize(2)
208+
.containsEntry("ColdStart", true)
209+
.containsEntry("Service", "service_undefined");
205210

206211
assertThat(subsegment.getMetadata())
207212
.isEmpty();
@@ -214,12 +219,13 @@ void shouldCaptureTracesWithNoMetadataDeprecated() {
214219

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

217-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
222+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
218223
.hasSize(1)
219224
.allSatisfy(subsegment -> {
220225
assertThat(subsegment.getAnnotations())
221-
.hasSize(1)
222-
.containsEntry("ColdStart", true);
226+
.hasSize(2)
227+
.containsEntry("ColdStart", true)
228+
.containsEntry("Service", "service_undefined");
223229

224230
assertThat(subsegment.getMetadata())
225231
.isEmpty();
@@ -234,12 +240,13 @@ void shouldNotCaptureTracesIfDisabledViaEnvironmentVariable() {
234240

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

237-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
243+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
238244
.hasSize(1)
239245
.allSatisfy(subsegment -> {
240246
assertThat(subsegment.getAnnotations())
241-
.hasSize(1)
242-
.containsEntry("ColdStart", true);
247+
.hasSize(2)
248+
.containsEntry("ColdStart", true)
249+
.containsEntry("Service", "lambdaHandler");
243250

244251
assertThat(subsegment.getMetadata())
245252
.isEmpty();
@@ -255,12 +262,13 @@ void shouldCaptureTracesIfExplicitlyEnabledAndEnvironmentVariableIsDisabled() {
255262

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

258-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
265+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
259266
.hasSize(1)
260267
.allSatisfy(subsegment -> {
261268
assertThat(subsegment.getAnnotations())
262-
.hasSize(1)
263-
.containsEntry("ColdStart", true);
269+
.hasSize(2)
270+
.containsEntry("ColdStart", true)
271+
.containsEntry("Service", "lambdaHandler");
264272

265273
assertThat(subsegment.getMetadata())
266274
.hasSize(1)
@@ -280,12 +288,13 @@ void shouldCaptureTracesIfExplicitlyEnabledBothAndEnvironmentVariableIsDisabled(
280288

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

283-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
291+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
284292
.hasSize(1)
285293
.allSatisfy(subsegment -> {
286294
assertThat(subsegment.getAnnotations())
287-
.hasSize(1)
288-
.containsEntry("ColdStart", true);
295+
.hasSize(2)
296+
.containsEntry("ColdStart", true)
297+
.containsEntry("Service", "lambdaHandler");
289298

290299
assertThat(subsegment.getMetadata())
291300
.hasSize(1)
@@ -303,12 +312,13 @@ void shouldNotCaptureTracesWithExceptionMetaDataIfDisabledViaEnvironmentVariable
303312

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

306-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
315+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
307316
.hasSize(1)
308317
.allSatisfy(subsegment -> {
309318
assertThat(subsegment.getAnnotations())
310-
.hasSize(1)
311-
.containsEntry("ColdStart", true);
319+
.hasSize(2)
320+
.containsEntry("ColdStart", true)
321+
.containsEntry("Service", "lambdaHandler");
312322

313323
assertThat(subsegment.getMetadata())
314324
.isEmpty();
@@ -325,12 +335,13 @@ void shouldCaptureTracesWithExceptionMetaDataEnabledExplicitlyAndEnvironmentVari
325335

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

328-
assertThat(AWSXRay.getTraceEntity().getSubsegments())
338+
assertThat(AWSXRay.getTraceEntity().getSubsegmentsCopy())
329339
.hasSize(1)
330340
.allSatisfy(subsegment -> {
331341
assertThat(subsegment.getAnnotations())
332-
.hasSize(1)
333-
.containsEntry("ColdStart", true);
342+
.hasSize(2)
343+
.containsEntry("ColdStart", true)
344+
.containsEntry("Service", "lambdaHandler");
334345

335346
assertThat(subsegment.getMetadata())
336347
.hasSize(1)

0 commit comments

Comments
 (0)