Skip to content

Commit 6459b5e

Browse files
authored
Pass Lambda Request ID to Extension (#8814)
* update LambdaHandlerInstrumentation.java * update unit test
1 parent f21ec9e commit 6459b5e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

dd-java-agent/instrumentation/aws-lambda-handler/src/main/java/datadog/trace/instrumentation/aws/v1/lambda/LambdaHandlerInstrumentation.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1414
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
1515

16+
import com.amazonaws.services.lambda.runtime.Context;
1617
import com.amazonaws.services.lambda.runtime.RequestHandler;
1718
import com.google.auto.service.AutoService;
1819
import datadog.trace.agent.tooling.Instrumenter;
@@ -79,20 +80,24 @@ public static class ExtensionCommunicationAdvice {
7980
@OnMethodEnter
8081
static AgentScope enter(
8182
@This final Object that,
82-
@Advice.Argument(0) final Object event,
83+
@Advice.Argument(0) final Object in,
84+
@Advice.Argument(1) final Object out,
85+
@Advice.Argument(2) final Context awsContext,
8386
@Origin("#m") final String methodName) {
8487

8588
if (CallDepthThreadLocalMap.incrementCallDepth(RequestHandler.class) > 0) {
8689
return null;
8790
}
8891

89-
AgentSpanContext lambdaContext = AgentTracer.get().notifyExtensionStart(event);
92+
AgentSpanContext lambdaContext = AgentTracer.get().notifyExtensionStart(in);
9093
final AgentSpan span;
9194
if (null == lambdaContext) {
9295
span = startSpan(INVOCATION_SPAN_NAME);
9396
} else {
9497
span = startSpan(INVOCATION_SPAN_NAME, lambdaContext);
9598
}
99+
span.setTag("request_id", awsContext.getAwsRequestId());
100+
96101
final AgentScope scope = activateSpan(span);
97102
return scope;
98103
}

dd-java-agent/instrumentation/aws-lambda-handler/src/test/groovy/LambdaHandlerInstrumentationTest.groovy

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import datadog.trace.agent.test.naming.VersionedNamingTestBase
22
import java.nio.charset.StandardCharsets
3+
import com.amazonaws.services.lambda.runtime.Context
34

45
abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase {
6+
def requestId = "test-request-id"
57

68
@Override
79
String service() {
@@ -12,7 +14,10 @@ abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase
1214
when:
1315
def input = new ByteArrayInputStream(StandardCharsets.UTF_8.encode("Hello").array())
1416
def output = new ByteArrayOutputStream()
15-
new HandlerStreaming().handleRequest(input, output, null)
17+
def ctx = Stub(Context) {
18+
getAwsRequestId() >> requestId
19+
}
20+
new HandlerStreaming().handleRequest(input, output, ctx)
1621

1722
then:
1823
assertTraces(1) {
@@ -29,7 +34,10 @@ abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase
2934
when:
3035
def input = new ByteArrayInputStream(StandardCharsets.UTF_8.encode("Hello").array())
3136
def output = new ByteArrayOutputStream()
32-
new HandlerStreamingWithError().handleRequest(input, output, null)
37+
def ctx = Stub(Context) {
38+
getAwsRequestId() >> requestId
39+
}
40+
new HandlerStreamingWithError().handleRequest(input, output, ctx)
3341

3442
then:
3543
thrown(Error)
@@ -39,6 +47,7 @@ abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase
3947
operationName operation()
4048
errored true
4149
tags {
50+
tag "request_id", requestId
4251
tag "error.type", "java.lang.Error"
4352
tag "error.message", "Some error"
4453
tag "error.stack", String

0 commit comments

Comments
 (0)