Better span names for aws-sdk (#258)

This commit is contained in:
Trask Stalnaker 2020-03-25 09:07:33 -07:00 committed by GitHub
parent c5c907e037
commit 2a738506a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 34 deletions

View File

@ -21,7 +21,6 @@ import com.amazonaws.Request;
import com.amazonaws.Response;
import io.opentelemetry.auto.bootstrap.ContextStore;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpClientDecorator;
import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.trace.Span;
import java.net.URI;
import java.util.Map;
@ -40,6 +39,16 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<Request, Response
this.contextStore = contextStore;
}
@Override
public String spanNameForRequest(final Request request) {
if (request == null) {
return DEFAULT_SPAN_NAME;
}
final String awsServiceName = request.getServiceName();
final Class<?> awsOperation = request.getOriginalRequest().getClass();
return remapServiceName(awsServiceName) + "." + remapOperationName(awsOperation);
}
@Override
public Span onRequest(final Span span, final Request request) {
// Call super first because we override the resource name below.
@ -54,10 +63,6 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<Request, Response
span.setAttribute("aws.operation", awsOperation.getSimpleName());
span.setAttribute("aws.endpoint", request.getEndpoint().toString());
span.setAttribute(
MoreTags.RESOURCE_NAME,
remapServiceName(awsServiceName) + "." + remapOperationName(awsOperation));
if (contextStore != null) {
final RequestMeta requestMeta = contextStore.get(originalRequest);
if (requestMeta != null) {

View File

@ -150,13 +150,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 2) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored false
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "$server.address/"
"$Tags.HTTP_METHOD" "$method"
@ -239,13 +238,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 2) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "http://localhost:${UNUSABLE_PORT}/"
"$Tags.HTTP_METHOD" "$method"
@ -300,13 +298,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 1) {
span(0) {
operationName expectedOperationName("HEAD")
operationName "S3.HeadBucket"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "S3.HeadBucket"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "https://s3.amazonaws.com/"
"$Tags.HTTP_METHOD" "HEAD"
@ -344,13 +341,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 5) {
span(0) {
operationName expectedOperationName("GET")
operationName "S3.GetObject"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "S3.GetObject"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "$server.address/"
"$Tags.HTTP_METHOD" "GET"

View File

@ -113,13 +113,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 2) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored false
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "$server.address/"
"$Tags.HTTP_METHOD" "$method"
@ -184,13 +183,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 2) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "http://localhost:${UNUSABLE_PORT}/"
"$Tags.HTTP_METHOD" "$method"
@ -245,13 +243,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 1) {
span(0) {
operationName expectedOperationName("GET")
operationName "S3.GetObject"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "S3.GetObject"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "https://s3.amazonaws.com/"
"$Tags.HTTP_METHOD" "GET"
@ -290,13 +287,12 @@ class AWSClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 5) {
span(0) {
operationName expectedOperationName("GET")
operationName "S3.GetObject"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "S3.GetObject"
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.HTTP_URL" "$server.address/"
"$Tags.HTTP_METHOD" "GET"

View File

@ -17,7 +17,6 @@ package io.opentelemetry.auto.instrumentation.awssdk.v2_2;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpClientDecorator;
import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import java.net.URI;
@ -60,14 +59,18 @@ public class AwsSdkClientDecorator extends HttpClientDecorator<SdkHttpRequest, S
return span;
}
public String spanName(final ExecutionAttributes attributes) {
final String awsServiceName = attributes.getAttribute(SdkExecutionAttribute.SERVICE_NAME);
final String awsOperation = attributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME);
return awsServiceName + "." + awsOperation;
}
public Span onAttributes(final Span span, final ExecutionAttributes attributes) {
final String awsServiceName = attributes.getAttribute(SdkExecutionAttribute.SERVICE_NAME);
final String awsOperation = attributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME);
// Resource Name has to be set after the HTTP_URL because otherwise decorators overwrite it
span.setAttribute(MoreTags.RESOURCE_NAME, awsServiceName + "." + awsOperation);
span.setAttribute("aws.agent", COMPONENT_NAME);
span.setAttribute("aws.service", awsServiceName);
span.setAttribute("aws.operation", awsOperation);

View File

@ -15,7 +15,6 @@
*/
package io.opentelemetry.auto.instrumentation.awssdk.v2_2;
import static io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpClientDecorator.DEFAULT_SPAN_NAME;
import static io.opentelemetry.auto.instrumentation.awssdk.v2_2.AwsSdkClientDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.awssdk.v2_2.AwsSdkClientDecorator.TRACER;
import static io.opentelemetry.trace.Span.Kind.CLIENT;
@ -49,7 +48,8 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
@Override
public void beforeExecution(
final Context.BeforeExecution context, final ExecutionAttributes executionAttributes) {
final Span span = TRACER.spanBuilder(DEFAULT_SPAN_NAME).setSpanKind(CLIENT).startSpan();
final Span span =
TRACER.spanBuilder(DECORATE.spanName(executionAttributes)).setSpanKind(CLIENT).startSpan();
try (final Scope scope = TRACER.withSpan(span)) {
DECORATE.afterStart(span);
executionAttributes.putAttribute(SPAN_ATTRIBUTE, span);
@ -61,7 +61,6 @@ public class TracingExecutionInterceptor implements ExecutionInterceptor {
final Context.AfterMarshalling context, final ExecutionAttributes executionAttributes) {
final Span span = executionAttributes.getAttribute(SPAN_ATTRIBUTE);
span.updateName(DECORATE.spanNameForRequest(context.httpRequest()));
DECORATE.onRequest(span, context.httpRequest());
DECORATE.onSdkRequest(span, context.request());
DECORATE.onAttributes(span, executionAttributes);

View File

@ -91,13 +91,12 @@ class AwsClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 2) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored false
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$MoreTags.NET_PEER_NAME" "localhost"
"$MoreTags.NET_PEER_PORT" server.address.port
@ -195,13 +194,12 @@ class AwsClientTest extends AgentTestRunner {
assertTraces(2) {
trace(0, 1) {
span(0) {
operationName expectedOperationName(method)
operationName "$service.$operation"
spanKind CLIENT
errored false
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "$service.$operation"
"$Tags.COMPONENT" "java-aws-sdk"
"$MoreTags.NET_PEER_NAME" "localhost"
"$MoreTags.NET_PEER_PORT" server.address.port
@ -310,13 +308,12 @@ class AwsClientTest extends AgentTestRunner {
assertTraces(1) {
trace(0, 5) {
span(0) {
operationName expectedOperationName("GET")
operationName "S3.GetObject"
spanKind CLIENT
errored true
parent()
tags {
"$MoreTags.SERVICE_NAME" "java-aws-sdk"
"$MoreTags.RESOURCE_NAME" "S3.GetObject"
"$Tags.COMPONENT" "java-aws-sdk"
"$MoreTags.NET_PEER_NAME" "localhost"
"$MoreTags.NET_PEER_PORT" server.address.port