Skip to content

Commit

Permalink
Ignore aws sdk v2 presign requests
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Aug 22, 2023
1 parent 7d49bb1 commit 812225a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
17 changes: 17 additions & 0 deletions instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -93,6 +109,7 @@ tasks {

check {
dependsOn(testExperimentalSqs)
dependsOn(testing.suites)
}

withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

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;
import io.opentelemetry.instrumentation.awssdk.v2_2.SnsAdviceBridge;
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 {
Expand All @@ -20,6 +22,11 @@ public SnsInstrumentationModule() {
super("aws-sdk-2.2-sns");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("software.amazon.awssdk.services.sns.SnsClient");
}

@Override
public void doTransform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

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;
import io.opentelemetry.instrumentation.awssdk.v2_2.SqsAdviceBridge;
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 {
Expand All @@ -20,6 +22,11 @@ public SqsInstrumentationModule() {
super("aws-sdk-2.2-sqs");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("software.amazon.awssdk.services.sqs.SqsClient");
}

@Override
public void doTransform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 812225a

Please sign in to comment.