Skip to content

Commit 2d0e08e

Browse files
authored
feat: custom segment names (#221)
* add segmentName attribute to Trace annotation * if segmentName is provided, use it as subsegment name intead of traced method's name
1 parent 4bb632c commit 2d0e08e

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

docs/content/core/tracing.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
6262
```
6363

6464
By default this annotation will automatically record method responses and exceptions.
65+
If you want to customize segment name that appears in traces, use:
66+
`@Tracing(segmentName="yourCustomName")`
6567

6668
<Note type="warning">
6769
<strong>Returning sensitive information from your Lambda handler or functions, where Tracer is used?</strong>

powertools-tracing/src/main/java/software/amazon/lambda/powertools/tracing/Tracing.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
String namespace() default "";
5151
boolean captureResponse() default true;
5252
boolean captureError() default true;
53+
String segmentName() default "";
5354
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.amazonaws.xray.AWSXRay;
1717
import com.amazonaws.xray.entities.Subsegment;
18+
import java.util.function.Supplier;
1819
import org.aspectj.lang.ProceedingJoinPoint;
1920
import org.aspectj.lang.annotation.Around;
2021
import org.aspectj.lang.annotation.Aspect;
@@ -42,7 +43,9 @@ public Object around(ProceedingJoinPoint pjp,
4243
Tracing tracing) throws Throwable {
4344
Object[] proceedArgs = pjp.getArgs();
4445

45-
Subsegment segment = AWSXRay.beginSubsegment("## " + pjp.getSignature().getName());
46+
Subsegment segment = AWSXRay.beginSubsegment(
47+
customSegmentNameOrDefault(tracing,
48+
() -> "## " + pjp.getSignature().getName()));
4649
segment.setNamespace(namespace(tracing));
4750

4851
if (placedOnHandlerMethod(pjp)) {
@@ -69,6 +72,11 @@ public Object around(ProceedingJoinPoint pjp,
6972
}
7073
}
7174

75+
private String customSegmentNameOrDefault(Tracing powerToolsTracing, Supplier<String> defaultSegmentName) {
76+
String segmentName = powerToolsTracing.segmentName();
77+
return segmentName.isEmpty() ? defaultSegmentName.get() : segmentName;
78+
}
79+
7280
private String namespace(Tracing powerToolsTracing) {
7381
return powerToolsTracing.namespace().isEmpty() ? serviceName() : powerToolsTracing.namespace();
7482
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.Test;
2727
import org.mockito.Mock;
2828
import software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor;
29+
import software.amazon.lambda.powertools.tracing.nonhandler.PowerToolNonHandler;
2930
import software.amazon.lambda.powertools.tracing.handlers.PowerToolDisabled;
3031
import software.amazon.lambda.powertools.tracing.handlers.PowerToolDisabledForStream;
3132
import software.amazon.lambda.powertools.tracing.handlers.PowerTracerToolEnabled;
@@ -43,6 +44,7 @@
4344
class LambdaTracingAspectTest {
4445
private RequestHandler<Object, Object> requestHandler;
4546
private RequestStreamHandler streamHandler;
47+
private PowerToolNonHandler nonHandlerMethod;
4648

4749
@Mock
4850
private Context context;
@@ -54,6 +56,7 @@ void setUp() throws IllegalAccessException {
5456
setupContext();
5557
requestHandler = new PowerTracerToolEnabled();
5658
streamHandler = new PowerTracerToolEnabledForStream();
59+
nonHandlerMethod = new PowerToolNonHandler();
5760
AWSXRay.beginSegment(LambdaTracingAspectTest.class.getName());
5861
}
5962

@@ -63,6 +66,24 @@ void tearDown() {
6366
}
6467

6568
@Test
69+
void shouldCaptureNonHandlerMethod() {
70+
nonHandlerMethod.doSomething();
71+
assertThat(AWSXRay.getTraceEntity().getSubsegments())
72+
.hasSize(1)
73+
.anySatisfy(segment ->
74+
assertThat(segment.getName()).isEqualTo("## doSomething"));
75+
}
76+
77+
@Test
78+
void shouldCaptureNonHandlerMethodWithCustomSegmentName() {
79+
nonHandlerMethod.doSomethingCustomName();
80+
assertThat(AWSXRay.getTraceEntity().getSubsegments())
81+
.hasSize(1)
82+
.anySatisfy(segment ->
83+
assertThat(segment.getName()).isEqualTo("custom"));
84+
}
85+
86+
@Test
6687
void shouldCaptureTraces() {
6788
requestHandler.handleRequest(new Object(), context);
6889

@@ -177,4 +198,4 @@ private void setupContext() {
177198
when(context.getFunctionVersion()).thenReturn("1");
178199
when(context.getMemoryLimitInMB()).thenReturn(10);
179200
}
180-
}
201+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package software.amazon.lambda.powertools.tracing.nonhandler;
2+
3+
import software.amazon.lambda.powertools.tracing.Tracing;
4+
5+
public class PowerToolNonHandler {
6+
7+
@Tracing
8+
public void doSomething() {
9+
}
10+
11+
@Tracing(segmentName = "custom")
12+
public void doSomethingCustomName() {
13+
}
14+
}

0 commit comments

Comments
 (0)