Skip to content

Commit 03b2980

Browse files
devinsbaadriancole
authored andcommitted
Backport changes that were learned in V2 impl (#113)
1 parent c9f88ec commit 03b2980

File tree

3 files changed

+33
-35
lines changed

3 files changed

+33
-35
lines changed

brave-instrumentation/aws-java-sdk-core/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# AWS SDK Instrumentation
22

3-
This module contains instrumentation for [AWS](https://github.com/aws/aws-sdk-java) clients that extend
4-
`AmazonWebServiceClient`.
3+
This module contains instrumentation for [AWS](https://github.com/aws/aws-sdk-java) clients that
4+
extend `AmazonWebServiceClient`.
55

6-
The `AwsClientTracing` class finalizes your `AwsClientBuilder` instances by adding a `RequestHandler2` instance to your client
7-
and wrapping the `ExecutorService` in the case of an async client.
6+
The `AwsClientTracing` class finalizes your `AwsClientBuilder` instances by adding a
7+
`RequestHandler2` instance to your client and wrapping the `ExecutorService` in the case of an async
8+
client.
89

910
## Span Model
1011

11-
Traces AWS Java SDK calls. Adds on the standard zipkin/brave http tags, as well as tags that align with the XRay data model.
12+
Traces AWS Java SDK calls. Adds on the standard zipkin/brave http tags, as well as tags that align
13+
with the XRay data model.
1214

1315
This implementation creates 2 types of spans to allow for better error visibility.
1416

15-
The outer span, "Application Span", wraps the whole SDK operation. This span uses the AWS service as it's name and will NOT
16-
have a remoteService configuration, making it a local span. If the entire operation results in an error then this span will
17-
have an error tag with the cause.
17+
The outer span, "Application Span", wraps the whole SDK operation. This span uses `aws-sdk` as it's
18+
name and will NOT have a remoteService configuration, making it a local span. If the entire
19+
operation results in an error then this span will have an error tag with the cause.
1820

19-
The inner span, "Client Span", is created for each outgoing HTTP request. This span will be of type CLIENT. The remoteService
20-
will be the name of the AWS service, and the span name will be the name of the operation being done. If the request results in
21-
an error then the span will be tagged with the error. The AWS request ID is added when available.
21+
The inner span, "Client Span", is created for each outgoing HTTP request. This span will be of type
22+
CLIENT. The remoteService will be the name of the AWS service, and the span name will be the name of
23+
the operation being done. If the request results in an error then the span will be tagged with the
24+
error. The AWS request ID is added when available.
2225

2326
## Wiring it up
2427

brave-instrumentation/aws-java-sdk-core/src/main/java/brave/instrumentation/aws/TracingRequestHandler.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
*
3838
* This implementation creates 2 types of spans to allow for better error visibility.
3939
*
40-
* The outer span, "Application Span", wraps the whole SDK operation. This span uses the AWS service
41-
* as it's name and will NOT have a remoteService configuration, making it a local span. If the
42-
* entire operation results in an error then this span will have an error tag with the cause.
40+
* The outer span, "Application Span", wraps the whole SDK operation. This span uses aws-sdk as it's
41+
* name and will NOT have a remoteService configuration, making it a local span. If the entire
42+
* operation results in an error then this span will have an error tag with the cause.
4343
*
4444
* The inner span, "Client Span", is created for each outgoing HTTP request. This span will be of
4545
* type CLIENT. The remoteService will be the name of the AWS service, and the span name will be the
@@ -49,8 +49,8 @@
4949
final class TracingRequestHandler extends RequestHandler2 {
5050
static final HandlerContextKey<Span> APPLICATION_SPAN =
5151
new HandlerContextKey<>("APPLICATION_SPAN");
52-
static final HandlerContextKey<TraceContext> DEFERRED_ROOT_SPAN =
53-
new HandlerContextKey<>("DEFERRED_ROOT_SPAN");
52+
static final HandlerContextKey<TraceContext> DEFERRED_ROOT_CONTEXT =
53+
new HandlerContextKey<>("DEFERRED_ROOT_CONTEXT");
5454
static final HandlerContextKey<Span> CLIENT_SPAN =
5555
new HandlerContextKey<>(Span.class.getCanonicalName());
5656

@@ -79,31 +79,31 @@ final class TracingRequestHandler extends RequestHandler2 {
7979
Span applicationSpan = tracer.nextSpan();
8080
// new root span, but we don't yet know if we should sample it
8181
if (applicationSpan.context().parentIdAsLong() == 0) {
82-
request.addHandlerContext(
83-
DEFERRED_ROOT_SPAN,
84-
applicationSpan.context().toBuilder().sampled(null).build()
85-
);
82+
request.addHandlerContext(DEFERRED_ROOT_CONTEXT, applicationSpan.context());
8683
} else {
8784
request.addHandlerContext(APPLICATION_SPAN, applicationSpan.start());
8885
}
8986
return request;
9087
}
9188

9289
@Override public void beforeAttempt(HandlerBeforeAttemptContext context) {
93-
TraceContext deferredRootSpan = context.getRequest().getHandlerContext(DEFERRED_ROOT_SPAN);
90+
TraceContext deferredRootContext = context.getRequest().getHandlerContext(DEFERRED_ROOT_CONTEXT);
9491
Span applicationSpan;
95-
if (deferredRootSpan != null) {
92+
if (deferredRootContext != null) {
9693
Boolean sampled = httpTracing.clientSampler().trySample(ADAPTER, context.getRequest());
9794
if (sampled == null) {
98-
sampled = httpTracing.tracing().sampler().isSampled(deferredRootSpan.traceId());
95+
sampled = httpTracing.tracing().sampler().isSampled(deferredRootContext.traceId());
9996
}
100-
applicationSpan = tracer.toSpan(deferredRootSpan.toBuilder().sampled(sampled).build());
97+
applicationSpan = tracer.toSpan(deferredRootContext.toBuilder().sampled(sampled).build());
10198
context.getRequest().addHandlerContext(APPLICATION_SPAN, applicationSpan.start());
10299
} else {
103100
applicationSpan = context.getRequest().getHandlerContext(APPLICATION_SPAN);
104101
}
105-
applicationSpan.name(context.getRequest().getServiceName());
106-
Span clientSpan = nextClientSpan(context.getRequest(), applicationSpan);
102+
String operation = getAwsOperationFromRequest(context.getRequest());
103+
applicationSpan.name("aws-sdk")
104+
.tag("aws.service_name", context.getRequest().getServiceName())
105+
.tag("aws.operation", operation);
106+
Span clientSpan = nextClientSpan(context.getRequest(), applicationSpan, operation);
107107
context.getRequest().addHandlerContext(CLIENT_SPAN, clientSpan);
108108
}
109109

@@ -129,15 +129,11 @@ final class TracingRequestHandler extends RequestHandler2 {
129129
applicationSpan.finish();
130130
}
131131

132-
private Span nextClientSpan(Request<?> request, Span applicationSpan) {
132+
private Span nextClientSpan(Request<?> request, Span applicationSpan, String operation) {
133133
Span span = tracer.newChild(applicationSpan.context());
134134
handler.handleSend(injector, request, span);
135-
String operation = getAwsOperationFromRequest(request);
136-
span.name(operation);
137-
span.remoteServiceName(request.getServiceName());
138-
span.tag("aws.service_name", request.getServiceName());
139-
span.tag("aws.operation", operation);
140-
return span;
135+
return span.name(operation)
136+
.remoteServiceName(request.getServiceName());
141137
}
142138

143139
private String getAwsOperationFromRequest(Request<?> request) {

brave-instrumentation/aws-java-sdk-core/src/test/java/brave/instrumentation/aws/AwsClientTracingTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ public void testSpanCreatedAndTagsApplied() throws InterruptedException {
8888
Span httpSpan = spans.poll(100, TimeUnit.MILLISECONDS);
8989
assertThat(httpSpan.remoteServiceName()).isEqualToIgnoringCase("amazondynamodbv2");
9090
assertThat(httpSpan.name()).isEqualToIgnoringCase("deleteitem");
91-
assertThat(httpSpan.tags().get("aws.operation")).isEqualToIgnoringCase("deleteitem");
9291
assertThat(httpSpan.tags().get("aws.request_id")).isEqualToIgnoringCase("abcd");
9392

9493
Span sdkSpan = spans.poll(100, TimeUnit.MILLISECONDS);
95-
assertThat(sdkSpan.name()).isEqualToIgnoringCase("amazondynamodbv2");
94+
assertThat(sdkSpan.name()).isEqualToIgnoringCase("aws-sdk");
9695
}
9796

9897
private MockResponse createDeleteItemResponse() {

0 commit comments

Comments
 (0)