|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.opentelemetry.javaagent.instrumentation.awssdk_v1_11; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.eq; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import com.amazonaws.Request; |
| 22 | +import com.amazonaws.Response; |
| 23 | +import com.amazonaws.auth.AWSCredentials; |
| 24 | +import com.amazonaws.handlers.HandlerContextKey; |
| 25 | +import com.amazonaws.services.kinesis.model.PutRecordRequest; |
| 26 | +import com.amazonaws.services.lambda.model.CreateFunctionRequest; |
| 27 | +import com.amazonaws.services.lambda.model.FunctionConfiguration; |
| 28 | +import com.amazonaws.services.lambda.model.GetFunctionResult; |
| 29 | +import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest; |
| 30 | +import com.amazonaws.services.sns.model.PublishRequest; |
| 31 | +import com.amazonaws.services.stepfunctions.model.StartExecutionRequest; |
| 32 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 33 | +import io.opentelemetry.context.Context; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +/* |
| 38 | + * NOTE: V1.11 attribute extraction is difficult to test in unit tests due to reflection-based |
| 39 | + * method access via MethodHandle. Many tests here only verify that the extractor correctly |
| 40 | + * identifies different AWS service types rather than actual attribute extraction. However, these |
| 41 | + * attributes are comprehensively tested in the contract tests which provide end-to-end validation |
| 42 | + * of the reflection-based extraction logic. The contract tests cover most V1.11 attributes |
| 43 | + * including all Bedrock Gen AI attributes. |
| 44 | + */ |
| 45 | +class AwsSdkExperimentalAttributesInjectionTest { |
| 46 | + |
| 47 | + private AwsSdkExperimentalAttributesExtractor extractor; |
| 48 | + private AttributesBuilder attributes; |
| 49 | + private Request<?> mockRequest; |
| 50 | + private Response<?> mockResponse; |
| 51 | + private static final HandlerContextKey<AWSCredentials> AWS_CREDENTIALS = |
| 52 | + new HandlerContextKey<>("AWSCredentials"); |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setUp() { |
| 56 | + extractor = new AwsSdkExperimentalAttributesExtractor(); |
| 57 | + attributes = mock(AttributesBuilder.class); |
| 58 | + mockRequest = mock(Request.class); |
| 59 | + mockResponse = mock(Response.class); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testSnsExperimentalAttributes() { |
| 64 | + PublishRequest snsRequest = mock(PublishRequest.class); |
| 65 | + when(mockRequest.getServiceName()).thenReturn("AmazonSNS"); |
| 66 | + when(mockRequest.getOriginalRequest()).thenReturn(snsRequest); |
| 67 | + when(snsRequest.getTopicArn()).thenReturn("arn:aws:sns:region:account:topic/test"); |
| 68 | + |
| 69 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 70 | + |
| 71 | + verify(attributes) |
| 72 | + .put( |
| 73 | + eq(AwsExperimentalAttributes.AWS_SNS_TOPIC_ARN), |
| 74 | + eq("arn:aws:sns:region:account:topic/test")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testKinesisExperimentalAttributes() { |
| 79 | + PutRecordRequest kinesisRequest = mock(PutRecordRequest.class); |
| 80 | + when(mockRequest.getServiceName()).thenReturn("AmazonKinesis"); |
| 81 | + when(mockRequest.getOriginalRequest()).thenReturn(kinesisRequest); |
| 82 | + when(kinesisRequest.getStreamARN()).thenReturn("arn:aws:kinesis:region:account:stream/test"); |
| 83 | + |
| 84 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 85 | + |
| 86 | + verify(attributes) |
| 87 | + .put( |
| 88 | + eq(AwsExperimentalAttributes.AWS_STREAM_ARN), |
| 89 | + eq("arn:aws:kinesis:region:account:stream/test")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testStepFunctionsExperimentalAttributes() { |
| 94 | + StartExecutionRequest sfnRequest = mock(StartExecutionRequest.class); |
| 95 | + when(mockRequest.getServiceName()).thenReturn("AWSStepFunctions"); |
| 96 | + when(mockRequest.getOriginalRequest()).thenReturn(sfnRequest); |
| 97 | + when(sfnRequest.getStateMachineArn()) |
| 98 | + .thenReturn("arn:aws:states:region:account:stateMachine/test"); |
| 99 | + |
| 100 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 101 | + |
| 102 | + verify(attributes) |
| 103 | + .put( |
| 104 | + eq(AwsExperimentalAttributes.AWS_STATE_MACHINE_ARN), |
| 105 | + eq("arn:aws:states:region:account:stateMachine/test")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testAuthAccessKeyAttributes() { |
| 110 | + AWSCredentials credentials = mock(AWSCredentials.class); |
| 111 | + when(mockRequest.getHandlerContext(AWS_CREDENTIALS)).thenReturn(credentials); |
| 112 | + when(credentials.getAWSAccessKeyId()).thenReturn("AKIAIOSFODNN7EXAMPLE"); |
| 113 | + when(mockRequest.getOriginalRequest()).thenReturn(mock(PublishRequest.class)); |
| 114 | + when(mockRequest.getServiceName()).thenReturn("AmazonSNS"); |
| 115 | + |
| 116 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 117 | + |
| 118 | + verify(attributes) |
| 119 | + .put(eq(AwsExperimentalAttributes.AWS_AUTH_ACCESS_KEY), eq("AKIAIOSFODNN7EXAMPLE")); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void testSecretsManagerExperimentalAttributes() { |
| 124 | + GetSecretValueRequest secretRequest = mock(GetSecretValueRequest.class); |
| 125 | + when(mockRequest.getServiceName()).thenReturn("AWSSecretsManager"); |
| 126 | + when(mockRequest.getOriginalRequest()).thenReturn(secretRequest); |
| 127 | + |
| 128 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 129 | + // We're not verifying anything here since the actual attribute setting depends on reflection |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testLambdaNameExperimentalAttributes() { |
| 134 | + CreateFunctionRequest lambdaRequest = mock(CreateFunctionRequest.class); |
| 135 | + when(mockRequest.getServiceName()).thenReturn("AWSLambda"); |
| 136 | + when(mockRequest.getOriginalRequest()).thenReturn(lambdaRequest); |
| 137 | + when(lambdaRequest.getFunctionName()).thenReturn("test-function"); |
| 138 | + |
| 139 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 140 | + |
| 141 | + verify(attributes).put(eq(AwsExperimentalAttributes.AWS_LAMBDA_NAME), eq("test-function")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testLambdaArnExperimentalAttributes() { |
| 146 | + GetFunctionResult lambdaResult = mock(GetFunctionResult.class); |
| 147 | + FunctionConfiguration config = mock(FunctionConfiguration.class); |
| 148 | + when(mockResponse.getAwsResponse()).thenReturn(lambdaResult); |
| 149 | + when(lambdaResult.getConfiguration()).thenReturn(config); |
| 150 | + when(config.getFunctionArn()).thenReturn("arn:aws:lambda:region:account:function:test"); |
| 151 | + when(mockRequest.getServiceName()).thenReturn("AWSLambda"); |
| 152 | + |
| 153 | + extractor.onEnd(attributes, Context.current(), mockRequest, mockResponse, null); |
| 154 | + |
| 155 | + verify(attributes) |
| 156 | + .put( |
| 157 | + eq(AwsExperimentalAttributes.AWS_LAMBDA_ARN), |
| 158 | + eq("arn:aws:lambda:region:account:function:test")); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testLambdaResourceIdExperimentalAttributes() { |
| 163 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 164 | + when(mockRequest.getServiceName()).thenReturn("AWSLambda"); |
| 165 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 166 | + |
| 167 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 168 | + // We can't verify the actual attribute setting since it depends on reflection |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testTableArnExperimentalAttributes() { |
| 173 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 174 | + when(mockRequest.getServiceName()).thenReturn("AmazonDynamoDBv2"); |
| 175 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 176 | + |
| 177 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 178 | + // We can't verify the actual attribute setting since it depends on reflection |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void testBedrockRuntimeAttributes() { |
| 183 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 184 | + when(mockRequest.getServiceName()).thenReturn("AmazonBedrockRuntime"); |
| 185 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 186 | + |
| 187 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 188 | + // We can't verify the actual attribute setting since it depends on reflection and class name |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + void testBedrockAgentAttributes() { |
| 193 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 194 | + when(mockRequest.getServiceName()).thenReturn("AWSBedrockAgent"); |
| 195 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 196 | + |
| 197 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 198 | + // We can't verify the actual attribute setting since it depends on reflection |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void testBedrockAgentRuntimeAttributes() { |
| 203 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 204 | + when(mockRequest.getServiceName()).thenReturn("AWSBedrockAgentRuntime"); |
| 205 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 206 | + |
| 207 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 208 | + // We can't verify the actual attribute setting since it depends on reflection |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + void testBedrockGuardrailAttributes() { |
| 213 | + PublishRequest originalRequest = mock(PublishRequest.class); |
| 214 | + when(mockRequest.getServiceName()).thenReturn("AmazonBedrock"); |
| 215 | + when(mockRequest.getOriginalRequest()).thenReturn(originalRequest); |
| 216 | + |
| 217 | + extractor.onStart(attributes, Context.current(), mockRequest); |
| 218 | + // We can't verify the actual attribute setting since it depends on reflection |
| 219 | + } |
| 220 | +} |
0 commit comments