-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of how to group related instrumentations under a single modul…
…e (AwsSdkModule) (#8141)
- Loading branch information
Showing
5 changed files
with
138 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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