Skip to content

Commit

Permalink
AWS Lambda Runtime legacy internal handlers need to be ignored from b…
Browse files Browse the repository at this point in the history
…eing instrumented and so traced … (#10942)

Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
serkan-ozal and trask authored Mar 26, 2024
1 parent 8c5b630 commit 9d94b4f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {
@Override
public ElementMatcher<TypeDescription> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +58,23 @@ void awsLambdaInternalHandlerIgnoredAndUserHandlerTraced() {
equalTo(SemanticAttributes.FAAS_INVOCATION_ID, "1-22-333"))));
}

@Test
void awsLambdaLegacyInternalHandlerIgnoredAndUserHandlerTraced() {
RequestHandler<String, String> 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<String, String> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> {

private final RequestHandler<String, String> requestHandler;

public AwsLambdaLegacyInternalRequestHandler(RequestHandler<String, String> requestHandler) {
this.requestHandler = requestHandler;
}

@Override
public String handleRequest(String input, Context context) {
return requestHandler.handleRequest(input, context);
}
}

0 comments on commit 9d94b4f

Please sign in to comment.