Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Decouple httpclient and aws sdk instrumentations with blackhole
  • Loading branch information
amarziali committed Jan 5, 2024
commit 25b195ad076bae233d47f5435193df8ee5253d7a
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static datadog.trace.instrumentation.apachehttpclient.ApacheHttpClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.apachehttpclient.HttpHeadersInjectAdapter.SETTER;

import datadog.trace.api.Config;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand All @@ -19,46 +18,30 @@
import org.apache.http.client.methods.HttpUriRequest;

public class HelperMethods {
private static final boolean AWS_LEGACY_TRACING =
Config.get().isLegacyTracingEnabled(false, "aws-sdk");

public static AgentScope doMethodEnter(final HttpUriRequest request) {
boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");
if (!AWS_LEGACY_TRACING && awsClientCall) {
Comment thread
amarziali marked this conversation as resolved.
// avoid creating an extra HTTP client span beneath the AWS client call
return null;
}

final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

return activateHttpSpan(request, awsClientCall);
return activateHttpSpan(request);
}

public static AgentScope doMethodEnter(HttpHost host, HttpRequest request) {
boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");
if (!AWS_LEGACY_TRACING && awsClientCall) {
// avoid creating an extra HTTP client span beneath the AWS client call
return null;
}

final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

return activateHttpSpan(new HostAndRequestAsHttpUriRequest(host, request), awsClientCall);
return activateHttpSpan(new HostAndRequestAsHttpUriRequest(host, request));
}

private static AgentScope activateHttpSpan(
final HttpUriRequest request, final boolean awsClientCall) {
private static AgentScope activateHttpSpan(final HttpUriRequest request) {
final AgentSpan span = startSpan(HTTP_REQUEST);
final AgentScope scope = activateSpan(span);

DECORATE.afterStart(span);
DECORATE.onRequest(span, request);
final boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");

// AWS calls are often signed, so we can't add headers without breaking the signature.
if (!awsClientCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static datadog.trace.instrumentation.apachehttpclient5.ApacheHttpClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.apachehttpclient5.HttpHeadersInjectAdapter.SETTER;

import datadog.trace.api.Config;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand All @@ -18,47 +17,32 @@
import org.apache.hc.core5.http.HttpResponse;

public class HelperMethods {
private static final boolean AWS_LEGACY_TRACING =
Config.get().isLegacyTracingEnabled(false, "aws-sdk");

public static AgentScope doMethodEnter(final HttpRequest request) {
boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");
if (!AWS_LEGACY_TRACING && awsClientCall) {
// avoid creating an extra HTTP client span beneath the AWS client call
return null;
}

final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

return activateHttpSpan(request, awsClientCall);
return activateHttpSpan(request);
}

public static AgentScope doMethodEnter(HttpHost host, HttpRequest request) {
boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");
if (!AWS_LEGACY_TRACING && awsClientCall) {
// avoid creating an extra HTTP client span beneath the AWS client call
return null;
}

final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class);
if (callDepth > 0) {
return null;
}

return activateHttpSpan(new HostAndRequestAsHttpUriRequest(host, request), awsClientCall);
return activateHttpSpan(new HostAndRequestAsHttpUriRequest(host, request));
}

private static AgentScope activateHttpSpan(
final HttpRequest request, final boolean awsClientCall) {
private static AgentScope activateHttpSpan(final HttpRequest request) {
final AgentSpan span = startSpan(HTTP_REQUEST);
final AgentScope scope = activateSpan(span);

DECORATE.afterStart(span);
DECORATE.onRequest(span, request);

final boolean awsClientCall = request.containsHeader("amz-sdk-invocation-id");
// AWS calls are often signed, so we can't add headers without breaking the signature.
if (!awsClientCall) {
propagate().inject(span, request, SETTER);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.instrumentation.aws.v0;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.blackholeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.core.datastreams.TagsProcessor.DIRECTION_IN;
Expand Down Expand Up @@ -53,7 +53,7 @@ public void beforeRequest(final Request<?> request) {
if (!AWS_LEGACY_TRACING && isPollingRequest(request.getOriginalRequest())) {
// SQS messages spans are created by aws-java-sqs-1.0 - replace client scope with no-op,
// so we can tell when receive call is complete without affecting the rest of the trace
span = noopSpan();
activateSpan(blackholeSpan());
} else {
span = requestSpanStore.remove(request.getOriginalRequest());
if (span != null) {
Expand All @@ -74,10 +74,13 @@ public void beforeRequest(final Request<?> request) {
log.warn("Unable to inject trace header", e);
}
}
// This scope will be closed by AwsHttpClientInstrumentation
if (AWS_LEGACY_TRACING) {
activateSpan(span);
} else {
activateSpan(blackholeSpan());
}
}

// This scope will be closed by AwsHttpClientInstrumentation
activateSpan(span);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.instrumentation.aws.v2;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.blackholeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.aws.v2.AwsSdkClientDecorator.AWS_LEGACY_TRACING;
Expand Down Expand Up @@ -85,10 +85,8 @@ public SdkHttpRequest modifyHttpRequest(
public void beforeTransmission(
final Context.BeforeTransmission context, final ExecutionAttributes executionAttributes) {
final AgentSpan span;
if (!AWS_LEGACY_TRACING && isPollingRequest(context.request())) {
// SQS messages spans are created by aws-java-sqs-2.0 - replace client scope with no-op,
// so we can tell when receive call is complete without affecting the rest of the trace
span = noopSpan();
if (!AWS_LEGACY_TRACING) {
span = blackholeSpan();
} else {
span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ abstract class AgentTestRunner extends DDSpecification implements AgentBuilder.L

boolean enabledFinishTimingChecks = this.enabledFinishTimingChecks()
TEST_TRACER.startSpan(*_) >> {
DDSpan agentSpan = callRealMethod()
AgentSpan agentSpan = callRealMethod()
TEST_SPANS.add(agentSpan.spanId)
if (!enabledFinishTimingChecks) {
return agentSpan
}

// rest of closure if for checking duplicate finishes and tags set after finish
DDSpan spiedAgentSpan = Spy(agentSpan)
AgentSpan spiedAgentSpan = Spy(agentSpan)
originalToSpySpan[agentSpan] = spiedAgentSpan
def handleFinish = { MockInvocation mi ->
def depth = CallDepthThreadLocalMap.incrementCallDepth(DDSpan)
Expand Down