-
Notifications
You must be signed in to change notification settings - Fork 323
Example of how to group related instrumentations under a single module (AwsSdkModule) #8141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions
47
.../aws-java-sdk-1.11.0/src/main/java/datadog/trace/instrumentation/aws/v0/AwsSdkModule.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,47 @@ | ||
| package datadog.trace.instrumentation.aws.v0; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** Groups the instrumentations for AWS SDK 1.11.0+. */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class AwsSdkModule extends InstrumenterModule.Tracing { | ||
|
|
||
| public AwsSdkModule() { | ||
| super("aws-sdk"); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| packageName + ".AwsSdkClientDecorator", | ||
| packageName + ".GetterAccess", | ||
| packageName + ".GetterAccess$1", | ||
| packageName + ".TracingRequestHandler", | ||
| packageName + ".AwsNameCache", | ||
| packageName + ".OnErrorDecorator", | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| Map<String, String> map = new java.util.HashMap<>(); | ||
| map.put("com.amazonaws.services.sqs.model.ReceiveMessageResult", "java.lang.String"); | ||
| map.put( | ||
| "com.amazonaws.AmazonWebServiceRequest", | ||
| "datadog.trace.bootstrap.instrumentation.api.AgentSpan"); | ||
| return map; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Instrumenter> typeInstrumentations() { | ||
| return Arrays.asList( | ||
| new AWSHttpClientInstrumentation(), | ||
| new RequestExecutorInstrumentation(), | ||
| new HandlerChainFactoryInstrumentation()); | ||
| } | ||
| } | ||
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
60 changes: 60 additions & 0 deletions
60
....0/src/main/java/datadog/trace/instrumentation/aws/v0/RequestExecutorInstrumentation.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,60 @@ | ||
| package datadog.trace.instrumentation.aws.v0; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope; | ||
| import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.DECORATE; | ||
| import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.SPAN_CONTEXT_KEY; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
|
|
||
| import com.amazonaws.Request; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** | ||
| * Due to a change in the AmazonHttpClient class, this instrumentation is needed to support newer | ||
| * versions. The {@link AWSHttpClientInstrumentation} class should cover older versions. | ||
| */ | ||
| public final class RequestExecutorInstrumentation | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "com.amazonaws.http.AmazonHttpClient$RequestExecutor"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("doExecute")), | ||
| RequestExecutorInstrumentation.class.getName() + "$RequestExecutorAdvice"); | ||
| } | ||
|
|
||
| public static class RequestExecutorAdvice { | ||
| @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
| public static void methodExit( | ||
| @Advice.FieldValue("request") final Request<?> request, | ||
| @Advice.Thrown final Throwable throwable) { | ||
|
|
||
| final AgentScope scope = activeScope(); | ||
| // check name in case TracingRequestHandler failed to activate the span | ||
| if (scope != null | ||
| && (AwsNameCache.spanName(request).equals(scope.span().getSpanName()) | ||
| || scope.span() instanceof AgentTracer.NoopAgentSpan)) { | ||
| scope.close(); | ||
| } | ||
|
|
||
| if (throwable != null) { | ||
| final AgentSpan span = request.getHandlerContext(SPAN_CONTEXT_KEY); | ||
| if (span != null) { | ||
| request.addHandlerContext(SPAN_CONTEXT_KEY, null); | ||
| DECORATE.onError(span, throwable); | ||
| DECORATE.beforeFinish(span); | ||
| span.finish(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.
Sometimes we have different instrumentation names if we want to turn off a specific advice. I understand that's handy to have the group decide for what activate or not. How does it play if we need to disable a specific one? (I was thinking about
spring-path-filter). Can this name be still declared on the Instrumentation (extendingInstrumenterModule.Tracing) or that kind ofifshould be implemented rather intypeInstrumentationsmethod?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.
You can only set the integration name on the
InstrumenterModule- if you want to have an instrumentation under a different integration name then it's likely independent and deserves its ownInstrumenterModule(i.e. itself if it only covers one instrumentation.)Otherwise if you want instrumentations to share a module, but still be individually enabled/disabled then implement that logic inside
typeInstrumentations()- that could involve checking which products are enabled, or using theInstrumenterConfig.isIntegrationEnabled(...)method. This should be a relatively rare situation, because typically instrumentations grouped together under a module should be considered as a logical unit.