-
Notifications
You must be signed in to change notification settings - Fork 339
DSMON-886: add Sqs spring messaging context propagation support #9662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
682dd03
add sqs spring messaging context propagation support
piochelepiotr e9813ca
Merge branch 'master' into piotr-wolski/test-with-spring
piochelepiotr c22ad1f
use queue URL to mark as spring or not
piochelepiotr 167a188
fix tests
piochelepiotr 930d836
update version
piochelepiotr 22bd9eb
rename module and remove print
piochelepiotr 5293e8c
use min test java version
piochelepiotr d9114a8
Merge branch 'master' into piotr-wolski/test-with-spring
piochelepiotr be69e4f
use single type instrumentation
piochelepiotr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ out/ | |
| # Visual Studio Code # | ||
| ###################### | ||
| .vscode | ||
| .cursor | ||
|
|
||
| # Others # | ||
| ########## | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...src/main/java/datadog/trace/instrumentation/aws/v2/sqs/SqsAsyncClientInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package datadog.trace.instrumentation.aws.v2.sqs; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import java.util.Map; | ||
| import net.bytebuddy.asm.Advice; | ||
| import software.amazon.awssdk.services.sqs.SqsAsyncClient; | ||
| import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; | ||
|
|
||
| /** | ||
| * Instrumentation for SqsAsyncClient receiveMessage calls to track when Spring-managed clients are | ||
| * making receive operations and mark the responses accordingly. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public class SqsAsyncClientInstrumentation extends AbstractSqsInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "software.amazon.awssdk.services.sqs.DefaultSqsAsyncClient"; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| Map<String, String> contextStore = new java.util.HashMap<>(2); | ||
| contextStore.put("software.amazon.awssdk.services.sqs.SqsAsyncClient", Boolean.class.getName()); | ||
| // Map queue URL to Spring management status | ||
| contextStore.put("java.lang.String", "java.lang.Boolean"); | ||
| return contextStore; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| // Instrument the receiveMessage method to map queue URLs to Spring management status | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("receiveMessage")) | ||
| .and( | ||
| takesArgument( | ||
| 0, named("software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest"))), | ||
| getClass().getName() + "$ReceiveMessageAdvice"); | ||
| } | ||
|
|
||
| public static class ReceiveMessageAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| static void onExit( | ||
| @Advice.This SqsAsyncClient client, @Advice.Argument(0) ReceiveMessageRequest req) { | ||
|
|
||
| Boolean isSpringClient = | ||
| InstrumentationContext.get(SqsAsyncClient.class, Boolean.class).get(client); | ||
|
|
||
| if (Boolean.TRUE.equals(isSpringClient)) { | ||
| // Map the queue URL to Spring management status | ||
| final ContextStore<String, Boolean> queueUrlFlags = | ||
| InstrumentationContext.get(String.class, Boolean.class); | ||
| queueUrlFlags.put(req.queueUrl(), Boolean.TRUE); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
dd-java-agent/instrumentation/spring/spring-sqs-3.0/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| muzzle { | ||
| pass { | ||
| group = 'io.awspring.cloud' | ||
| module = 'spring-cloud-aws-sqs' | ||
| versions = "[3.0.0,)" | ||
| assertInverse = true | ||
| } | ||
| } | ||
|
|
||
| ext { | ||
| minJavaVersionForTests = JavaVersion.VERSION_17 | ||
| } | ||
|
|
||
|
|
||
| apply from: "$rootDir/gradle/java.gradle" | ||
|
|
||
| addTestSuiteForDir('latestDepTest', 'test') | ||
|
|
||
| ["compileTestGroovy", "compileLatestDepTestGroovy"].each { name -> | ||
| tasks.named(name, GroovyCompile) { | ||
| javaLauncher = getJavaLauncherFor(17) | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly group: 'software.amazon.awssdk', name: 'sqs', version: '2.20.162' | ||
| compileOnly group: 'org.springframework', name: 'spring-messaging', version: '5.3.23' | ||
|
|
||
| testImplementation project(':dd-java-agent:instrumentation:trace-annotation') | ||
| testImplementation project(':dd-java-agent:instrumentation:aws-java:aws-java-sdk-2.2') | ||
| testImplementation project(':dd-java-agent:instrumentation:aws-java:aws-java-sqs-2.0') | ||
| testImplementation project(':dd-java-agent:instrumentation:spring:spring-messaging-4.0') | ||
|
|
||
| testImplementation group: 'org.springframework', name: 'spring-context', version: '6.1.10' | ||
| testImplementation group: 'org.springframework', name: 'spring-test', version: '6.1.10' | ||
| testImplementation group: 'org.springframework', name: 'spring-core', version: '6.1.10' | ||
| testImplementation group: 'io.awspring.cloud', name: 'spring-cloud-aws-sqs', version: '3.1.0' | ||
| testImplementation group: 'software.amazon.awssdk', name: 'sqs', version: '2.20.162' | ||
| testImplementation group: 'software.amazon.awssdk', name: 'aws-core', version: '2.20.162' | ||
| testImplementation group: 'org.testcontainers', name: 'localstack', version: libs.versions.testcontainers.get() | ||
| testImplementation 'org.slf4j:slf4j-api:2.0.13' | ||
| testImplementation 'ch.qos.logback:logback-classic:1.4.14' | ||
| testImplementation 'ch.qos.logback:logback-core:1.4.14' | ||
|
|
||
| latestDepTestImplementation group: 'org.springframework', name: 'spring-context', version: '6.+' | ||
| latestDepTestImplementation group: 'org.springframework', name: 'spring-test', version: '6.+' | ||
| latestDepTestImplementation group: 'org.springframework', name: 'spring-core', version: '6.+' | ||
| latestDepTestImplementation group: 'software.amazon.awssdk', name: 'sqs', version: '2.+' | ||
| latestDepTestImplementation group: 'software.amazon.awssdk', name: 'aws-core', version: '2.+' | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, this seems suspicious. Do we really need a context store for string types? Perhaps there is a more specific class for this context store.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's not possible to find a more specific class for the context store, then perhaps a value class with a more specific type than Boolean can be used for both queueURL and asyncClient instead?