Skip to content

Commit

Permalink
AWS Lambda Runtime internal handlers need to be ignored from being in…
Browse files Browse the repository at this point in the history
…strumented and so traced. (#10736)
  • Loading branch information
serkan-ozal authored Mar 12, 2024
1 parent df83eaa commit e988d48
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import static io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0.AwsLambdaInstrumentationHelper.functionInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.amazonaws.services.lambda.runtime.Context;
Expand All @@ -35,7 +37,8 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return implementsInterface(named("com.amazonaws.services.lambda.runtime.RequestHandler"));
return implementsInterface(named("com.amazonaws.services.lambda.runtime.RequestHandler"))
.and(not(nameStartsWith("com.amazonaws.services.lambda.runtime.api.client")));
}

@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 com.amazonaws.services.lambda.runtime.api.client;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class AwsLambdaInternalRequestHandler implements RequestHandler<String, String> {

private final RequestHandler<String, String> requestHandler;

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

@Override
public String handleRequest(String input, Context context) {
return requestHandler.handleRequest(input, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

package io.opentelemetry.javaagent.instrumentation.awslambdacore.v1_0;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.api.client.AwsLambdaInternalRequestHandler;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.awslambdacore.v1_0.AbstractAwsLambdaTest;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.SemanticAttributes;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class AwsLambdaTest extends AbstractAwsLambdaTest {
Expand All @@ -35,6 +40,23 @@ void tearDown() {
assertThat(testing.forceFlushCalled()).isTrue();
}

@Test
void awsLambdaInternalHandlerIgnoredAndUserHandlerTraced() {
RequestHandler<String, String> handler = new AwsLambdaInternalRequestHandler(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
Expand Up @@ -53,6 +53,10 @@ void tearDown() {
assertThat(testing().forceFlushCalled()).isTrue();
}

protected Context context() {
return context;
}

@Test
void handlerTraced() {
String result = handler().handleRequest("hello", context);
Expand Down

0 comments on commit e988d48

Please sign in to comment.