diff --git a/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaRequestHandlerInstrumentation.java b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaRequestHandlerInstrumentation.java index 0743cdea75d6..dfc70b368fc4 100644 --- a/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaRequestHandlerInstrumentation.java +++ b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaRequestHandlerInstrumentation.java @@ -38,7 +38,12 @@ public ElementMatcher classLoaderOptimization() { @Override public ElementMatcher typeMatcher() { return implementsInterface(named("com.amazonaws.services.lambda.runtime.RequestHandler")) - .and(not(nameStartsWith("com.amazonaws.services.lambda.runtime.api.client"))); + .and(not(nameStartsWith("com.amazonaws.services.lambda.runtime.api.client"))) + // In Java 8 and Java 11 runtimes, + // AWS Lambda runtime is packaged under `lambdainternal` package. + // But it is `com.amazonaws.services.lambda.runtime.api.client` + // for new runtime likes Java 17 and Java 21. + .and(not(nameStartsWith("lambdainternal"))); } @Override diff --git a/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaTest.java b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaTest.java index 29700c2bf753..5b3678f0c6f8 100644 --- a/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaTest.java +++ b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/awslambdacore/v1_0/AwsLambdaTest.java @@ -16,6 +16,7 @@ import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; import io.opentelemetry.semconv.SemanticAttributes; +import lambdainternal.AwsLambdaLegacyInternalRequestHandler; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -57,6 +58,23 @@ void awsLambdaInternalHandlerIgnoredAndUserHandlerTraced() { equalTo(SemanticAttributes.FAAS_INVOCATION_ID, "1-22-333")))); } + @Test + void awsLambdaLegacyInternalHandlerIgnoredAndUserHandlerTraced() { + RequestHandler handler = new AwsLambdaLegacyInternalRequestHandler(handler()); + String result = handler.handleRequest("hello", context()); + assertThat(result).isEqualTo("world"); + + testing() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("my_function") + .hasKind(SpanKind.SERVER) + .hasAttributesSatisfyingExactly( + equalTo(SemanticAttributes.FAAS_INVOCATION_ID, "1-22-333")))); + } + private static final class TestRequestHandler implements RequestHandler { @Override diff --git a/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/lambdainternal/AwsLambdaLegacyInternalRequestHandler.java b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/lambdainternal/AwsLambdaLegacyInternalRequestHandler.java new file mode 100644 index 000000000000..5d4625ef47c1 --- /dev/null +++ b/instrumentation/aws-lambda/aws-lambda-core-1.0/javaagent/src/test/java/lambdainternal/AwsLambdaLegacyInternalRequestHandler.java @@ -0,0 +1,23 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package lambdainternal; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +public class AwsLambdaLegacyInternalRequestHandler implements RequestHandler { + + private final RequestHandler requestHandler; + + public AwsLambdaLegacyInternalRequestHandler(RequestHandler requestHandler) { + this.requestHandler = requestHandler; + } + + @Override + public String handleRequest(String input, Context context) { + return requestHandler.handleRequest(input, context); + } +}