Skip to content

Commit

Permalink
Automatic AWS library instrumentor
Browse files Browse the repository at this point in the history
Like AWS X-Ray, provide an instrumentor which automatically registers
opentelemetry instrumentation in the AWS SDK without any code changes.
Those instrumentors are separate libraries published as
opentelemetry-aws-sdk-1.11-instrumentor and opentelemetry-aws-sdk-2.2-instrumentor
  • Loading branch information
steven-aerts committed Nov 19, 2021
1 parent 123de52 commit b84ad1e
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.awssdk.v1_11.AwsSdkTracing;

/**
Expand All @@ -33,12 +32,7 @@ public class TracingRequestHandler extends RequestHandler2 {
new HandlerContextKey<>(Scope.class.getName());

public static final RequestHandler2 tracingHandler =
AwsSdkTracing.builder(GlobalOpenTelemetry.get())
.setCaptureExperimentalSpanAttributes(
Config.get()
.getBoolean("otel.instrumentation.aws-sdk.experimental-span-attributes", false))
.build()
.newRequestHandler();
AwsSdkTracing.fromAgentConfig(GlobalOpenTelemetry.get()).newRequestHandler();

@Override
public void beforeRequest(Request<?> request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id("otel.library-instrumentation")
}

base.archivesName.set("${base.archivesName.get()}-autoconfigure")

dependencies {
implementation(project(":instrumentation:aws-sdk:aws-sdk-1.11:library"))

library("com.amazonaws:aws-java-sdk-core:1.11.0")

testImplementation(project(":instrumentation:aws-sdk:aws-sdk-1.11:testing"))

testLibrary("com.amazonaws:aws-java-sdk-s3:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-rds:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-ec2:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-kinesis:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-dynamodb:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-sns:1.11.106")
testLibrary("com.amazonaws:aws-java-sdk-sqs:1.11.106")
}

tasks.test {
systemProperty("otel.instrumentation.aws-sdk.experimental-span-attributes", "true")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v1_11.autoconfigure;

import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.Request;
import com.amazonaws.Response;
import com.amazonaws.handlers.RequestHandler2;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.awssdk.v1_11.AwsSdkTracing;

/**
* A {@link RequestHandler2} for use as an SPI by the AWS SDK to automatically trace all requests.
*/
public class TracingRequestHandler extends RequestHandler2 {

private static final RequestHandler2 DELEGATE =
AwsSdkTracing.fromAgentConfig(GlobalOpenTelemetry.get()).newRequestHandler();

@Override
public void beforeRequest(Request<?> request) {
DELEGATE.beforeRequest(request);
}

@Override
public AmazonWebServiceRequest beforeMarshalling(AmazonWebServiceRequest request) {
return DELEGATE.beforeMarshalling(request);
}

@Override
public void afterResponse(Request<?> request, Response<?> response) {
DELEGATE.afterResponse(request, response);
}

@Override
public void afterError(Request<?> request, Response<?> response, Exception e) {
DELEGATE.afterError(request, response, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.instrumentation.awssdk.v1_11.autoconfigure.TracingRequestHandler
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v1_11.instrumentor

import io.opentelemetry.instrumentation.awssdk.v1_11.AbstractAws1ClientTest
import io.opentelemetry.instrumentation.test.LibraryTestTrait

class Aws1ClientTest extends AbstractAws1ClientTest implements LibraryTestTrait {
@Override
def configureClient(def client) {
return client
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.awssdk.v1_11.instrumentor

import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder
import io.opentelemetry.instrumentation.awssdk.v1_11.AbstractSqsTracingTest
import io.opentelemetry.instrumentation.test.LibraryTestTrait

class SqsTracingTest extends AbstractSqsTracingTest implements LibraryTestTrait {
@Override
AmazonSQSAsyncClientBuilder configureClient(AmazonSQSAsyncClientBuilder client) {
return client
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.amazonaws.handlers.RequestHandler2;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;

/**
Expand All @@ -22,6 +23,8 @@
* witness broken traces.
*/
public class AwsSdkTracing {
private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES_DEFAULT =
Config.get().getBoolean("otel.instrumentation.aws-sdk.experimental-span-attributes", false);

/**
* Returns the OpenTelemetry {@link Context} stored in the {@link Request}, or {@code null} if
Expand All @@ -37,6 +40,16 @@ public static AwsSdkTracing create(OpenTelemetry openTelemetry) {
return builder(openTelemetry).build();
}

/**
* Returns a new {@link AwsSdkTracing} configured with the given {@link OpenTelemetry} and from
* the global agent {@link Config}.
*/
public static AwsSdkTracing fromAgentConfig(OpenTelemetry openTelemetry) {
return builder(openTelemetry)
.setCaptureExperimentalSpanAttributes(CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES_DEFAULT)
.build();
}

/** Returns a new {@link AwsSdkTracingBuilder} configured with the given {@link OpenTelemetry}. */
public static AwsSdkTracingBuilder builder(OpenTelemetry openTelemetry) {
return new AwsSdkTracingBuilder(openTelemetry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ abstract class AbstractAws1ClientTest extends InstrumentationSpecification {
response != null

client.requestHandler2s != null
client.requestHandler2s.get(0).getClass().getSimpleName() == "TracingRequestHandler"
client.requestHandler2s.find{it.getClass().getSimpleName() == "TracingRequestHandler"} != null

assertTraces(1) {
trace(0, 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ muzzle {
}

dependencies {
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:library"))
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:library-autoconfigure"))

library("software.amazon.awssdk:aws-core:2.2.0")

Expand All @@ -31,3 +31,9 @@ tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.aws-sdk.experimental-span-attributes=true")
}

tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>().configureEach {
mergeServiceFiles {
include("software/amazon/awssdk/global/handlers/execution.interceptors")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import io.opentelemetry.instrumentation.awssdk.v2_2.autoconfigure.TracingExecutionInterceptor;
import io.opentelemetry.javaagent.extension.instrumentation.HelperResourceBuilder;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id("otel.library-instrumentation")
}

base.archivesName.set("${base.archivesName.get()}-autoconfigure")

dependencies {
implementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:library"))

library("software.amazon.awssdk:aws-core:2.2.0")
library("software.amazon.awssdk:aws-json-protocol:2.2.0")

testImplementation(project(":instrumentation:aws-sdk:aws-sdk-2.2:testing"))

latestDepTestLibrary("software.amazon.awssdk:kinesis:+")
}

tasks {
test {
systemProperty("otel.instrumentation.aws-sdk.experimental-span-attributes", true)
}
}
Loading

0 comments on commit b84ad1e

Please sign in to comment.