-
Notifications
You must be signed in to change notification settings - Fork 64
Base of AWS SDK v1.11 SPI Implementation #1115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fec3f1
Added base v1.11 implementation and updated README
anahatAWS 609e6b1
updated documentation for AdotAwsSdkClientInstrumentation
anahatAWS 1d7fb87
added upstream code reference
anahatAWS 4217429
updated documentation and added references
anahatAWS 4a367c2
added unit tests
anahatAWS 5966e8c
added documentation and updated unit tests
anahatAWS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
104 changes: 104 additions & 0 deletions
104
...opentelemetry/javaagent/instrumentation/awssdk_v1_11/AdotAwsSdkClientInstrumentation.java
This file contains hidden or 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,104 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.*; | ||
|
||
import com.amazonaws.handlers.RequestHandler2; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.List; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
/** | ||
* This class provides instrumentation by injecting our request handler into the AWS client's | ||
* handler chain. Key components: | ||
* | ||
* <p>1. Type Matching: Targets AmazonWebServiceClient (base class for all AWS SDK v1.11 clients). | ||
* Ensures handler injection during client initialization. | ||
* | ||
* <p>2. Transformation: Uses ByteBuddy to modify the client constructor. Injects our handler | ||
* registration code. | ||
* | ||
* <p>3. Handler Registration (via Advice): Checks for existing OpenTelemetry handler and adds ADOT | ||
* handler only if: a) OpenTelemetry handler is present (ensuring base instrumentation) b) ADOT | ||
* handler isn't already added (preventing duplicates) | ||
* | ||
* <p>Based on OpenTelemetry Java Instrumentation's AWS SDK v1.11 AwsClientInstrumentation | ||
* (release/v2.11.x). Adapts the base instrumentation pattern to add ADOT-specific functionality. | ||
* | ||
* <p>Source: <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/release/v2.11.x/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v1_11/AwsClientInstrumentation.java">...</a> | ||
*/ | ||
public class AdotAwsSdkClientInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
// AmazonWebServiceClient is the base interface for all AWS SDK clients. | ||
// Type matching against it ensures our interceptor is injected as soon as any AWS SDK client is | ||
// initialized. | ||
return named("com.amazonaws.AmazonWebServiceClient") | ||
.and(declaresField(named("requestHandler2s"))); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor(), | ||
AdotAwsSdkClientInstrumentation.class.getName() + "$AdotAwsSdkClientAdvice"); | ||
} | ||
|
||
/** | ||
* Upstream handler registration: @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v1_11/AwsClientInstrumentation.java#L39">...</a> | ||
*/ | ||
@SuppressWarnings("unused") | ||
public static class AdotAwsSdkClientAdvice { | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
public static void addHandler( | ||
pxaws marked this conversation as resolved.
Show resolved
Hide resolved
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Advice.FieldValue(value = "requestHandler2s") List<RequestHandler2> handlers) { | ||
|
||
if (handlers == null) { | ||
return; | ||
} | ||
|
||
boolean hasOtelHandler = false; | ||
boolean hasAdotHandler = false; | ||
|
||
// Checks if aws-sdk spans are enabled | ||
for (RequestHandler2 handler : handlers) { | ||
if (handler | ||
.toString() | ||
.contains( | ||
"io.opentelemetry.javaagent.instrumentation.awssdk.v1_11.TracingRequestHandler")) { | ||
hasOtelHandler = true; | ||
} | ||
if (handler instanceof AdotAwsSdkTracingRequestHandler) { | ||
hasAdotHandler = true; | ||
break; | ||
} | ||
} | ||
|
||
// Only adds our handler if aws-sdk spans are enabled. This also ensures upstream | ||
// instrumentation is applied first. | ||
if (hasOtelHandler && !hasAdotHandler) { | ||
handlers.add(new AdotAwsSdkTracingRequestHandler()); | ||
} | ||
} | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...opentelemetry/javaagent/instrumentation/awssdk_v1_11/AdotAwsSdkInstrumentationModule.java
This file contains hidden or 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,61 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
/** | ||
* Based on OpenTelemetry Java Instrumentation's AWS SDK v1.11 AbstractAwsSdkInstrumentationModule | ||
* (release/v2.11.x). Adapts the base instrumentation pattern to add ADOT-specific functionality. | ||
* | ||
* <p>Source: <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/release/v2.11.x/instrumentation/aws-sdk/aws-sdk-1.11/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/awssdk/v1_11/AbstractAwsSdkInstrumentationModule.java">...</a> | ||
*/ | ||
public class AdotAwsSdkInstrumentationModule extends InstrumentationModule { | ||
thpierce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public AdotAwsSdkInstrumentationModule() { | ||
super("aws-sdk-adot", "aws-sdk-1.11-adot"); | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// Ensure this runs after OTel (> 0) | ||
return Integer.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
public List<String> getAdditionalHelperClassNames() { | ||
return Arrays.asList( | ||
"software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AdotAwsSdkTracingRequestHandler"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
return hasClassesNamed("com.amazonaws.AmazonWebServiceClient"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return Collections.singletonList(new AdotAwsSdkClientInstrumentation()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...opentelemetry/javaagent/instrumentation/awssdk_v1_11/AdotAwsSdkTracingRequestHandler.java
This file contains hidden or 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,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; | ||
|
||
import com.amazonaws.Request; | ||
import com.amazonaws.handlers.HandlerAfterAttemptContext; | ||
import com.amazonaws.handlers.RequestHandler2; | ||
|
||
/** | ||
* Based on OpenTelemetry Java Instrumentation's AWS SDK v1.11 TracingRequestHandler | ||
* (release/v2.11.x). Adapts the base instrumentation pattern to add ADOT-specific functionality. | ||
* | ||
* <p>Source: <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/release/v2.11.x/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingRequestHandler.java">...</a> | ||
*/ | ||
public class AdotAwsSdkTracingRequestHandler extends RequestHandler2 { | ||
|
||
public AdotAwsSdkTracingRequestHandler() {} | ||
|
||
/** | ||
* This is the latest point we can obtain the Sdk Request after it is modified by the upstream | ||
* TracingInterceptor. It ensures upstream handles the request and applies its changes first. | ||
* | ||
* <p>Upstream's last Sdk Request modification: @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingRequestHandler.java#L58">reference</a> | ||
*/ | ||
@Override | ||
public void beforeRequest(Request<?> request) {} | ||
|
||
/** | ||
* This is the latest point to access the sdk response before the span closes in the upstream | ||
* afterResponse/afterError methods. This ensures we capture attributes from the final, fully | ||
* modified response after all upstream interceptors have processed it. | ||
* | ||
* <p>Upstream's last Sdk Response modification before span closure: @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingRequestHandler.java#L116">reference</a> | ||
* | ||
* @see <a | ||
* href="https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingRequestHandler.java#L131">reference</a> | ||
*/ | ||
@Override | ||
public void afterAttempt(HandlerAfterAttemptContext context) {} | ||
} |
3 changes: 2 additions & 1 deletion
3
...A-INF/services/io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule
This file contains hidden or 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v2_2.AdotAwsSdkInstrumentationModule | ||
software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v2_2.AdotAwsSdkInstrumentationModule | ||
software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AdotAwsSdkInstrumentationModule |
86 changes: 86 additions & 0 deletions
86
...azon/opentelemetry/javaagent/instrumentation/awssdk_v1_11/AdotAwsSdkClientAdviceTest.java
This file contains hidden or 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,86 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.amazonaws.handlers.RequestHandler2; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AdotAwsSdkClientAdviceTest { | ||
|
||
private AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice advice; | ||
private List<RequestHandler2> handlers; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
advice = new AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice(); | ||
handlers = new ArrayList<>(); | ||
} | ||
|
||
@Test | ||
void testAddHandlerWhenHandlersIsNull() { | ||
AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice.addHandler(null); | ||
assertThat(handlers).hasSize(0); | ||
} | ||
|
||
@Test | ||
void testAddHandlerWhenNoOtelHandler() { | ||
RequestHandler2 someOtherHandler = mock(RequestHandler2.class); | ||
handlers.add(someOtherHandler); | ||
|
||
AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice.addHandler(handlers); | ||
|
||
assertThat(handlers).hasSize(1); | ||
assertThat(handlers).containsExactly(someOtherHandler); | ||
} | ||
|
||
@Test | ||
void testAddHandlerWhenOtelHandlerPresent() { | ||
RequestHandler2 otelHandler = mock(RequestHandler2.class); | ||
when(otelHandler.toString()) | ||
.thenReturn( | ||
"io.opentelemetry.javaagent.instrumentation.awssdk.v1_11.TracingRequestHandler"); | ||
handlers.add(otelHandler); | ||
|
||
AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice.addHandler(handlers); | ||
|
||
assertThat(handlers).hasSize(2); | ||
assertThat(handlers.get(0)).isEqualTo(otelHandler); | ||
assertThat(handlers.get(1)).isInstanceOf(AdotAwsSdkTracingRequestHandler.class); | ||
} | ||
|
||
@Test | ||
void testAddHandlerWhenAdotHandlerAlreadyPresent() { | ||
RequestHandler2 otelHandler = mock(RequestHandler2.class); | ||
when(otelHandler.toString()) | ||
.thenReturn( | ||
"io.opentelemetry.javaagent.instrumentation.awssdk.v1_11.TracingRequestHandler"); | ||
handlers.add(otelHandler); | ||
handlers.add(new AdotAwsSdkTracingRequestHandler()); | ||
|
||
AdotAwsSdkClientInstrumentation.AdotAwsSdkClientAdvice.addHandler(handlers); | ||
|
||
assertThat(handlers).hasSize(2); | ||
assertThat(handlers.get(0)).isEqualTo(otelHandler); | ||
assertThat(handlers.get(1)).isInstanceOf(AdotAwsSdkTracingRequestHandler.class); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.