From 812225a39045dfe32e95cb1c0ab1eda90715c8b6 Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Tue, 22 Aug 2023 12:58:29 +0300 Subject: [PATCH] Ignore aws sdk v2 presign requests --- .../aws-sdk-2.2/javaagent/build.gradle.kts | 17 +++++ .../awssdk/v2_2/SnsInstrumentationModule.java | 7 ++ .../awssdk/v2_2/SqsInstrumentationModule.java | 7 ++ .../s3PresignerTest/java/S3PresignerTest.java | 69 +++++++++++++++++++ .../v2_2/TracingExecutionInterceptor.java | 9 +++ 5 files changed, 109 insertions(+) create mode 100644 instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/s3PresignerTest/java/S3PresignerTest.java diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts index 0aa4f3625cd2..ee46be39c3e9 100644 --- a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts +++ b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts @@ -84,6 +84,22 @@ dependencies { testLibrary("software.amazon.awssdk:ses:2.2.0") } +val latestDepTest = findProperty("testLatestDeps") as Boolean + +testing { + suites { + val s3PresignerTest by registering(JvmTestSuite::class) { + dependencies { + if (latestDepTest) { + implementation("software.amazon.awssdk:s3:+") + } else { + implementation("software.amazon.awssdk:s3:2.10.12") + } + } + } + } +} + tasks { val testExperimentalSqs by registering(Test::class) { group = "verification" @@ -93,6 +109,7 @@ tasks { check { dependsOn(testExperimentalSqs) + dependsOn(testing.suites) } withType().configureEach { diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SnsInstrumentationModule.java b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SnsInstrumentationModule.java index 82158fa5318a..6086fb4431e3 100644 --- a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SnsInstrumentationModule.java +++ b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SnsInstrumentationModule.java @@ -5,6 +5,7 @@ package io.opentelemetry.javaagent.instrumentation.awssdk.v2_2; +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; import static net.bytebuddy.matcher.ElementMatchers.none; import com.google.auto.service.AutoService; @@ -12,6 +13,7 @@ import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import net.bytebuddy.asm.Advice; +import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) public class SnsInstrumentationModule extends AbstractAwsSdkInstrumentationModule { @@ -20,6 +22,11 @@ public SnsInstrumentationModule() { super("aws-sdk-2.2-sns"); } + @Override + public ElementMatcher.Junction classLoaderMatcher() { + return hasClassesNamed("software.amazon.awssdk.services.sns.SnsClient"); + } + @Override public void doTransform(TypeTransformer transformer) { transformer.applyAdviceToMethod( diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SqsInstrumentationModule.java b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SqsInstrumentationModule.java index 3fdab6da5d2f..8a19f6fdcbd5 100644 --- a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SqsInstrumentationModule.java +++ b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v2_2/SqsInstrumentationModule.java @@ -5,6 +5,7 @@ package io.opentelemetry.javaagent.instrumentation.awssdk.v2_2; +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; import static net.bytebuddy.matcher.ElementMatchers.none; import com.google.auto.service.AutoService; @@ -12,6 +13,7 @@ import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import net.bytebuddy.asm.Advice; +import net.bytebuddy.matcher.ElementMatcher; @AutoService(InstrumentationModule.class) public class SqsInstrumentationModule extends AbstractAwsSdkInstrumentationModule { @@ -20,6 +22,11 @@ public SqsInstrumentationModule() { super("aws-sdk-2.2-sqs"); } + @Override + public ElementMatcher.Junction classLoaderMatcher() { + return hasClassesNamed("software.amazon.awssdk.services.sqs.SqsClient"); + } + @Override public void doTransform(TypeTransformer transformer) { transformer.applyAdviceToMethod( diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/s3PresignerTest/java/S3PresignerTest.java b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/s3PresignerTest/java/S3PresignerTest.java new file mode 100644 index 000000000000..5d4f6f0442c6 --- /dev/null +++ b/instrumentation/aws-sdk/aws-sdk-2.2/javaagent/src/s3PresignerTest/java/S3PresignerTest.java @@ -0,0 +1,69 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import java.time.Duration; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.presigner.S3Presigner; +import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; +import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; + +class S3PresignerTest { + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = + StaticCredentialsProvider.create( + AwsBasicCredentials.create("my-access-key", "my-secret-key")); + + private static S3Presigner s3Presigner; + + @BeforeAll + static void setUp() { + // trigger adding tracing interceptor + S3Client.builder() + .region(Region.AP_NORTHEAST_1) + .credentialsProvider(CREDENTIALS_PROVIDER) + .build(); + + s3Presigner = + S3Presigner.builder() + .region(Region.AP_NORTHEAST_1) + .credentialsProvider(CREDENTIALS_PROVIDER) + .build(); + } + + @Test + void testPresignGetObject() { + s3Presigner.presignGetObject( + GetObjectPresignRequest.builder() + .getObjectRequest(builder -> builder.bucket("test").key("test")) + .signatureDuration(Duration.ofDays(1)) + .build()); + + assertThat(Span.current().getSpanContext().isValid()).isFalse(); + } + + @Test + void testPresignPutObject() { + s3Presigner.presignPutObject( + PutObjectPresignRequest.builder() + .putObjectRequest(builder -> builder.bucket("test").key("test")) + .signatureDuration(Duration.ofDays(1)) + .build()); + + assertThat(Span.current().getSpanContext().isValid()).isFalse(); + } +} diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java index c344596f4ad9..1bc6d5ca8b56 100644 --- a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java +++ b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/TracingExecutionInterceptor.java @@ -17,6 +17,7 @@ import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.util.List; import javax.annotation.Nullable; +import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute; import software.amazon.awssdk.awscore.AwsResponse; import software.amazon.awssdk.core.ClientType; import software.amazon.awssdk.core.SdkRequest; @@ -89,6 +90,14 @@ public SdkRequest modifyRequest( io.opentelemetry.context.Context parentOtelContext = io.opentelemetry.context.Context.current(); SdkRequest request = context.request(); + + // Ignore presign request. These requests don't run all interceptor methods and the span created + // here would never be ended and scope closed. + if (executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME) + != null) { + return request; + } + executionAttributes.putAttribute(SDK_REQUEST_ATTRIBUTE, request); if (!requestInstrumenter.shouldStart(parentOtelContext, executionAttributes)) {