Skip to content

Instrumentation Patch Removal and SPI AWS SDK Test Addition #1120

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 1 addition & 24 deletions .github/actions/patch-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "Patch dependencies"
description: |
Patches direct dependencies of this project leveraging maven local to publish the results.

This workflow supports patching opentelemetry-java and opentelemetry-java-instrumentation repositories by executing
This workflow supports patching opentelemetry-java and opentelemetry-java-contrib repositories by executing
the `patch.sh` script that will try to patch those repositories and after that will optionally test and then publish
the artifacts to maven local.
To add a patch you have to add a file in the `.github/patches/` directory with the name of the repository that must
Expand Down Expand Up @@ -49,9 +49,6 @@ runs:
if [[ -f .github/patches/opentelemetry-java.patch ]]; then
echo 'patch_otel_java=true' >> $GITHUB_ENV
fi
if [[ -f .github/patches/opentelemetry-java-instrumentation.patch ]]; then
echo 'patch_otel_java_instrumentation=true' >> $GITHUB_ENV
fi
if [[ -f .github/patches/opentelemetry-java-contrib.patch ]]; then
echo 'patch_otel_java_contrib=true' >> $GITHUB_ENV
fi
Expand All @@ -60,7 +57,6 @@ runs:
- name: Clone and patch repositories
run: .github/scripts/patch.sh
if: ${{ env.patch_otel_java == 'true' ||
env.patch_otel_java_instrumentation == 'true' ||
env.patch_otel_java_contrib == 'true' }}
shell: bash

Expand Down Expand Up @@ -101,22 +97,3 @@ runs:
run: rm -rf opentelemetry-java-contrib
if: ${{ env.patch_otel_java_contrib == 'true' }}
shell: bash

- name: Build opentelemetry-java-instrumentation with tests
uses: gradle/gradle-build-action@v2
if: ${{ env.patch_otel_java_instrumentation == 'true' && inputs.run_tests != 'false' }}
with:
arguments: check -x spotlessCheck publishToMavenLocal
build-root-directory: opentelemetry-java-instrumentation

- name: Build opentelemetry java instrumentation
uses: gradle/gradle-build-action@v2
if: ${{ env.patch_otel_java_instrumentation == 'true' && inputs.run_tests == 'false' }}
with:
arguments: publishToMavenLocal
build-root-directory: opentelemetry-java-instrumentation

- name: cleanup opentelmetry-java-instrumentation
run: rm -rf opentelemetry-java-instrumentation
if: ${{ env.patch_otel_java_instrumentation == 'true' }}
shell: bash
4,193 changes: 0 additions & 4,193 deletions .github/patches/opentelemetry-java-instrumentation.patch

This file was deleted.

14 changes: 0 additions & 14 deletions .github/scripts/patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set -x -e -u
# This is used so that we can properly clone the upstream repositories.
# This file should define the following variables:
# OTEL_JAVA_VERSION. Tag of the opentelemetry-java repository to use. E.g.: JAVA_OTEL_JAVA_VERSION=v1.21.0
# OTEL_JAVA_INSTRUMENTATION_VERSION. Tag of the opentelemetry-java-instrumentation repository to use, e.g.: OTEL_JAVA_INSTRUMENTATION_VERSION=v1.21.0
# OTEL_JAVA_CONTRIB_VERSION. Tag of the opentelemetry-java-contrib repository. E.g.: OTEL_JAVA_CONTRIB_VERSION=v1.21.0
# This script will fail if a variable that is supposed to exist is referenced.

Expand Down Expand Up @@ -45,16 +44,3 @@ if [[ -f "$OTEL_JAVA_CONTRIB_PATCH" ]]; then
else
echo "Skipping patching opentelemetry-java-contrib"
fi


OTEL_JAVA_INSTRUMENTATION_PATCH=".github/patches/opentelemetry-java-instrumentation.patch"
if [[ -f "$OTEL_JAVA_INSTRUMENTATION_PATCH" ]]; then
git clone https://github.com/open-telemetry/opentelemetry-java-instrumentation.git
cd opentelemetry-java-instrumentation
git checkout ${OTEL_JAVA_INSTRUMENTATION_VERSION} -b tag-${OTEL_JAVA_INSTRUMENTATION_VERSION}
patch -p1 < "../${OTEL_JAVA_INSTRUMENTATION_PATCH}"
git commit -a -m "ADOT Patch release"
cd -
else
echo "Skipping patching opentelemetry-java-instrumentation"
fi
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class DependencySet(val group: String, val version: String, val modules: Li
val testSnapshots = rootProject.findProperty("testUpstreamSnapshots") == "true"

// This is the version of the upstream instrumentation BOM
val otelVersion = "2.11.0-adot1"
val otelVersion = "2.11.0"
val otelSnapshotVersion = "2.12.0"
val otelAlphaVersion = if (!testSnapshots) "$otelVersion-alpha" else "$otelSnapshotVersion-alpha-SNAPSHOT"
val otelJavaAgentVersion = if (!testSnapshots) otelVersion else "$otelSnapshotVersion-SNAPSHOT"
Expand Down
26 changes: 23 additions & 3 deletions instrumentation/aws-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,32 @@ _Class Functionalities:_

### Commands for Running Groovy Tests

To run the BedrockJsonParserTest for aws-sdk v1.11:
#### aws-sdk v1.11
To run the `BedrockJsonParserTest`:
````
./gradlew :instrumentation:aws-sdk:test --tests "software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.BedrockJsonParserTest"
````

To run the BedrockJsonParserTest for aws-sdk v2.2:
#### aws-sdk v2.2
To run the `BedrockJsonParserTest`:
````
./gradlew :instrumentation:aws-sdk:test --tests "software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v2_2.BedrockJsonParserTest"
````
````

### Commands for Running Java Tests

#### aws-sdk v1.11
To run the `AwsSdkExperimentalAttributesInjectionTest`:
````
./gradlew :instrumentation:aws-sdk:test --tests "software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AwsSdkExperimentalAttributesInjectionTest"
````

To run the `AdotAwsSdkClientAdviceTest`:
````
./gradlew :instrumentation:aws-sdk:test --tests "software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11.AdotAwsSdkClientAdviceTest"
````

#### aws-sdk v2.2
To run the `AwsSdkExperimentalAttributesInjectionTest`:
````
./gradlew :instrumentation:aws-sdk:test --tests "software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v2_2.AwsSdkExperimentalAttributesInjectionTest"
15 changes: 8 additions & 7 deletions instrumentation/aws-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ dependencies {
compileOnly("com.amazonaws:aws-java-sdk-core:1.11.0")
compileOnly("software.amazon.awssdk:aws-core:2.2.0")
compileOnly("software.amazon.awssdk:aws-json-protocol:2.2.0")

compileOnly("net.bytebuddy:byte-buddy")
compileOnly("com.google.code.findbugs:jsr305:3.0.2")

testImplementation("com.google.guava:guava")
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common")

testImplementation("com.amazonaws:aws-java-sdk-core:1.11.0")
testImplementation("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")
testImplementation("org.mockito:mockito-core:5.14.2")
testImplementation("com.google.guava:guava")
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common")
testImplementation("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")

testImplementation("software.amazon.awssdk:aws-core:2.2.0")
testImplementation("com.amazonaws:aws-java-sdk-lambda:1.11.678")
testImplementation("com.amazonaws:aws-java-sdk-kinesis:1.11.106")
testImplementation("com.amazonaws:aws-java-sdk-sns:1.11.106")
testImplementation("com.amazonaws:aws-java-sdk-stepfunctions:1.11.230")
testImplementation("com.amazonaws:aws-java-sdk-secretsmanager:1.11.309")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* 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.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

import com.amazonaws.Request;
import com.amazonaws.Response;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.handlers.HandlerContextKey;
import com.amazonaws.services.kinesis.model.PutRecordRequest;
import com.amazonaws.services.lambda.model.CreateFunctionRequest;
import com.amazonaws.services.lambda.model.FunctionConfiguration;
import com.amazonaws.services.lambda.model.GetFunctionResult;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.stepfunctions.model.StartExecutionRequest;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/*
* NOTE: V1.11 attribute extraction is difficult to test in unit tests due to reflection-based
* method access via MethodHandle. Many tests here only verify that the extractor correctly
* identifies different AWS service types rather than actual attribute extraction. However, these
* attributes are comprehensively tested in the contract tests which provide end-to-end validation
* of the reflection-based extraction logic. The contract tests cover most V1.11 attributes
* including all Bedrock Gen AI attributes.
*/
class AwsSdkExperimentalAttributesInjectionTest {

private AwsSdkExperimentalAttributesExtractor extractor;
private AttributesBuilder attributes;
private Request<?> mockRequest;
private Response<?> mockResponse;
private static final HandlerContextKey<AWSCredentials> AWS_CREDENTIALS =
new HandlerContextKey<>("AWSCredentials");

@BeforeEach
void setUp() {
extractor = new AwsSdkExperimentalAttributesExtractor();
attributes = mock(AttributesBuilder.class);
mockRequest = mock(Request.class);
mockResponse = mock(Response.class);
}

@Test
void testSnsExperimentalAttributes() {
PublishRequest snsRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AmazonSNS");
when(mockRequest.getOriginalRequest()).thenReturn(snsRequest);
when(snsRequest.getTopicArn()).thenReturn("arn:aws:sns:region:account:topic/test");

extractor.onStart(attributes, Context.current(), mockRequest);

verify(attributes)
.put(
eq(AwsExperimentalAttributes.AWS_SNS_TOPIC_ARN),
eq("arn:aws:sns:region:account:topic/test"));
}

@Test
void testKinesisExperimentalAttributes() {
PutRecordRequest kinesisRequest = mock(PutRecordRequest.class);
when(mockRequest.getServiceName()).thenReturn("AmazonKinesis");
when(mockRequest.getOriginalRequest()).thenReturn(kinesisRequest);
when(kinesisRequest.getStreamARN()).thenReturn("arn:aws:kinesis:region:account:stream/test");

extractor.onStart(attributes, Context.current(), mockRequest);

verify(attributes)
.put(
eq(AwsExperimentalAttributes.AWS_STREAM_ARN),
eq("arn:aws:kinesis:region:account:stream/test"));
}

@Test
void testStepFunctionsExperimentalAttributes() {
StartExecutionRequest sfnRequest = mock(StartExecutionRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSStepFunctions");
when(mockRequest.getOriginalRequest()).thenReturn(sfnRequest);
when(sfnRequest.getStateMachineArn())
.thenReturn("arn:aws:states:region:account:stateMachine/test");

extractor.onStart(attributes, Context.current(), mockRequest);

verify(attributes)
.put(
eq(AwsExperimentalAttributes.AWS_STATE_MACHINE_ARN),
eq("arn:aws:states:region:account:stateMachine/test"));
}

@Test
void testAuthAccessKeyAttributes() {
AWSCredentials credentials = mock(AWSCredentials.class);
when(mockRequest.getHandlerContext(AWS_CREDENTIALS)).thenReturn(credentials);
when(credentials.getAWSAccessKeyId()).thenReturn("AKIAIOSFODNN7EXAMPLE");
when(mockRequest.getOriginalRequest()).thenReturn(mock(PublishRequest.class));
when(mockRequest.getServiceName()).thenReturn("AmazonSNS");

extractor.onStart(attributes, Context.current(), mockRequest);

verify(attributes)
.put(eq(AwsExperimentalAttributes.AWS_AUTH_ACCESS_KEY), eq("AKIAIOSFODNN7EXAMPLE"));
}

@Test
void testSecretsManagerExperimentalAttributes() {
GetSecretValueRequest secretRequest = mock(GetSecretValueRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSSecretsManager");
when(mockRequest.getOriginalRequest()).thenReturn(secretRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We're not verifying anything here since the actual attribute setting depends on reflection
}

@Test
void testLambdaNameExperimentalAttributes() {
CreateFunctionRequest lambdaRequest = mock(CreateFunctionRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSLambda");
when(mockRequest.getOriginalRequest()).thenReturn(lambdaRequest);
when(lambdaRequest.getFunctionName()).thenReturn("test-function");

extractor.onStart(attributes, Context.current(), mockRequest);

verify(attributes).put(eq(AwsExperimentalAttributes.AWS_LAMBDA_NAME), eq("test-function"));
}

@Test
void testLambdaArnExperimentalAttributes() {
GetFunctionResult lambdaResult = mock(GetFunctionResult.class);
FunctionConfiguration config = mock(FunctionConfiguration.class);
when(mockResponse.getAwsResponse()).thenReturn(lambdaResult);
when(lambdaResult.getConfiguration()).thenReturn(config);
when(config.getFunctionArn()).thenReturn("arn:aws:lambda:region:account:function:test");
when(mockRequest.getServiceName()).thenReturn("AWSLambda");

extractor.onEnd(attributes, Context.current(), mockRequest, mockResponse, null);

verify(attributes)
.put(
eq(AwsExperimentalAttributes.AWS_LAMBDA_ARN),
eq("arn:aws:lambda:region:account:function:test"));
}

@Test
void testLambdaResourceIdExperimentalAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSLambda");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection
}

@Test
void testTableArnExperimentalAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AmazonDynamoDBv2");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection
}

@Test
void testBedrockRuntimeAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AmazonBedrockRuntime");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection and class name
}

@Test
void testBedrockAgentAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSBedrockAgent");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection
}

@Test
void testBedrockAgentRuntimeAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AWSBedrockAgentRuntime");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection
}

@Test
void testBedrockGuardrailAttributes() {
PublishRequest originalRequest = mock(PublishRequest.class);
when(mockRequest.getServiceName()).thenReturn("AmazonBedrock");
when(mockRequest.getOriginalRequest()).thenReturn(originalRequest);

extractor.onStart(attributes, Context.current(), mockRequest);
// We can't verify the actual attribute setting since it depends on reflection
}
}
Loading