|
| 1 | +package io.opentelemetry.instrumentation.awssdk.v2_2; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import io.opentelemetry.api.trace.SpanKind; |
| 5 | +import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; |
| 6 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 7 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
| 8 | +import io.opentelemetry.testing.internal.armeria.common.HttpResponse; |
| 9 | +import io.opentelemetry.testing.internal.armeria.common.HttpStatus; |
| 10 | +import io.opentelemetry.testing.internal.armeria.common.MediaType; |
| 11 | +import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension; |
| 12 | +import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.RecordedRequest; |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
| 17 | +import org.junit.jupiter.params.provider.Arguments; |
| 18 | +import org.junit.jupiter.params.provider.MethodSource; |
| 19 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 20 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 21 | +import software.amazon.awssdk.core.client.builder.SdkClientBuilder; |
| 22 | +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; |
| 23 | +import software.amazon.awssdk.regions.Region; |
| 24 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 25 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder; |
| 26 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 27 | +import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; |
| 28 | +import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; |
| 29 | +import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; |
| 30 | +import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; |
| 31 | +import software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex; |
| 32 | +import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; |
| 33 | +import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; |
| 34 | +import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; |
| 35 | +import software.amazon.awssdk.services.dynamodb.model.QueryRequest; |
| 36 | +import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.Future; |
| 40 | +import java.util.function.Function; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +import static com.google.common.collect.ImmutableMap.of; |
| 44 | +import static io.opentelemetry.api.common.AttributeKey.stringKey; |
| 45 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 46 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 47 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD; |
| 48 | +import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE; |
| 49 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_OPERATION; |
| 50 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS; |
| 51 | +import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT; |
| 52 | +import static io.opentelemetry.semconv.UrlAttributes.URL_FULL; |
| 53 | +import static io.opentelemetry.semconv.incubating.AwsIncubatingAttributes.AWS_REQUEST_ID; |
| 54 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
| 55 | +import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_METHOD; |
| 56 | +import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SERVICE; |
| 57 | +import static io.opentelemetry.semconv.incubating.RpcIncubatingAttributes.RPC_SYSTEM; |
| 58 | +import static org.assertj.core.api.Assertions.assertThat; |
| 59 | + |
| 60 | +public abstract class AbstractAws2ClientCoreTest { |
| 61 | + protected abstract InstrumentationExtension getTesting(); |
| 62 | + |
| 63 | + abstract ClientOverrideConfiguration.Builder createOverrideConfigurationBuilder(); |
| 64 | + |
| 65 | + static boolean isSqsAttributeInjectionEnabled() { |
| 66 | + // See io.opentelemetry.instrumentation.awssdk.v2_2.autoconfigure.TracingExecutionInterceptor |
| 67 | + return ConfigPropertiesUtil.getBoolean( |
| 68 | + "otel.instrumentation.aws-sdk.experimental-use-propagator-for-messaging", false); |
| 69 | + } |
| 70 | + |
| 71 | + static final StaticCredentialsProvider CREDENTIALS_PROVIDER = StaticCredentialsProvider |
| 72 | + .create(AwsBasicCredentials.create("my-access-key", "my-secret-key")); |
| 73 | + |
| 74 | + static MockWebServerExtension server = new MockWebServerExtension(); |
| 75 | + |
| 76 | + @BeforeAll |
| 77 | + static void setup() { |
| 78 | + server.start(); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterAll |
| 82 | + static void cleanup() { |
| 83 | + server.stop(); |
| 84 | + } |
| 85 | + |
| 86 | + @BeforeEach |
| 87 | + void prepTest() { |
| 88 | + server.beforeTestExecution(null); |
| 89 | + } |
| 90 | + |
| 91 | + void configureSdkClient(SdkClientBuilder builder) { |
| 92 | + builder.overrideConfiguration(createOverrideConfigurationBuilder().build()); |
| 93 | + } |
| 94 | + |
| 95 | + static ImmutableMap<String, AttributeValue> createTableRequestKey = ImmutableMap.of( |
| 96 | + "anotherKey", AttributeValue.builder().s("value").build(), |
| 97 | + "key", AttributeValue.builder().s("value").build()); |
| 98 | + |
| 99 | + static ImmutableMap<String, AttributeValue> getItemRequestKey = ImmutableMap.of( |
| 100 | + "keyOne", AttributeValue.builder().s("value").build(), |
| 101 | + "keyTwo", AttributeValue.builder().s("differentValue").build()); |
| 102 | + |
| 103 | + static ImmutableMap<String, AttributeValue> putItemRequestKey = ImmutableMap.of( |
| 104 | + "key", AttributeValue.builder().s("value").build(), |
| 105 | + "attributeOne", AttributeValue.builder().s("one").build(), |
| 106 | + "attributeTwo", AttributeValue.builder().s("two").build()); |
| 107 | + |
| 108 | + private static Stream<Arguments> provideArguments() { |
| 109 | + return Stream.of( |
| 110 | + Arguments.of("CreateTable", |
| 111 | + (Function<DynamoDbClient, Object>) c -> c.createTable(createTableRequest())), |
| 112 | + Arguments.of("DeleteItem", |
| 113 | + (Function<DynamoDbClient, Object>) c -> c.deleteItem(DeleteItemRequest.builder() |
| 114 | + .tableName("sometable") |
| 115 | + .key(createTableRequestKey) |
| 116 | + .conditionExpression("property in (:one, :two)") |
| 117 | + .build())), |
| 118 | + Arguments.of("DeleteItem", |
| 119 | + (Function<DynamoDbClient, Object>) c -> c.deleteTable( |
| 120 | + DeleteTableRequest.builder().tableName("sometable").build())), |
| 121 | + Arguments.of("GetItem", |
| 122 | + (Function<DynamoDbClient, Object>) c -> c.getItem( |
| 123 | + GetItemRequest.builder() |
| 124 | + .tableName("sometable") |
| 125 | + .key(getItemRequestKey) |
| 126 | + .attributesToGet("propertyOne", "propertyTwo") |
| 127 | + .build())), |
| 128 | + Arguments.of("PutItem", |
| 129 | + (Function<DynamoDbClient, Object>) c -> c.putItem( |
| 130 | + PutItemRequest.builder() |
| 131 | + .tableName("sometable") |
| 132 | + .item(putItemRequestKey) |
| 133 | + .conditionExpression("attributeOne <> :someVal") |
| 134 | + .build())), |
| 135 | + Arguments.of("Query", |
| 136 | + (Function<DynamoDbClient, Object>) c -> c.query( |
| 137 | + QueryRequest.builder() |
| 138 | + .tableName("sometable") |
| 139 | + .select("ALL_ATTRIBUTES") |
| 140 | + .keyConditionExpression("attribute = :aValue") |
| 141 | + .filterExpression("anotherAttribute = :someVal") |
| 142 | + .limit(10).build())), |
| 143 | + Arguments.of("UpdateItem", |
| 144 | + (Function<DynamoDbClient, Object>) c -> c.updateItem( |
| 145 | + UpdateItemRequest.builder() |
| 146 | + .tableName("sometable") |
| 147 | + .key( |
| 148 | + of("keyOne", AttributeValue.builder().s("value").build(), |
| 149 | + "keyTwo", AttributeValue.builder().s("differentValue").build())) |
| 150 | + .conditionExpression("attributeOne <> :someVal") |
| 151 | + .updateExpression("set attributeOne = :updateValue") |
| 152 | + .build())) |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + @ParameterizedTest |
| 157 | + @MethodSource("provideArguments") |
| 158 | + void testSendDynamoDbRequestWithBuilderMockedResponse(String operation, |
| 159 | + Function<DynamoDbClient, Object> call) throws ExecutionException, InterruptedException { |
| 160 | + DynamoDbClientBuilder builder = DynamoDbClient.builder(); |
| 161 | + configureSdkClient(builder); |
| 162 | + DynamoDbClient client = builder |
| 163 | + .endpointOverride(server.httpUri()) |
| 164 | + .region(Region.AP_NORTHEAST_1) |
| 165 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 166 | + .build(); |
| 167 | + server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, "")); |
| 168 | + Object response = call.apply(client); |
| 169 | + |
| 170 | + if (response instanceof Future) { |
| 171 | + response = ((Future<?>) response).get(); |
| 172 | + } |
| 173 | + |
| 174 | + assertThat(response).isNotNull(); |
| 175 | + assertThat(response.getClass().getSimpleName()).startsWith(operation); |
| 176 | + |
| 177 | + RecordedRequest request = server.takeRequest(); |
| 178 | + assertThat(request).isNotNull(); |
| 179 | + assertThat(request.request().headers().get("X-Amzn-Trace-Id")).isNotNull(); |
| 180 | + assertThat(request.request().headers().get("traceparent")).isNotNull(); |
| 181 | + |
| 182 | + |
| 183 | + getTesting().waitAndAssertTraces( |
| 184 | + trace -> trace.hasSpansSatisfyingExactly( |
| 185 | + span -> { |
| 186 | + if (operation.equals("CreateTable")) { |
| 187 | + assertCreateTableRequest(span); |
| 188 | + } else if (operation.equals("Query")) { |
| 189 | + assertQueryRequest(span); |
| 190 | + } else { |
| 191 | + assertDynamoDbRequest(span, operation); |
| 192 | + } |
| 193 | + } |
| 194 | + ) |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + static CreateTableRequest createTableRequest() { |
| 199 | + return CreateTableRequest.builder() |
| 200 | + .tableName("sometable") |
| 201 | + .globalSecondaryIndexes(Arrays.asList( |
| 202 | + GlobalSecondaryIndex.builder() |
| 203 | + .indexName("globalIndex") |
| 204 | + .keySchema( |
| 205 | + KeySchemaElement.builder() |
| 206 | + .attributeName("attribute") |
| 207 | + .build()) |
| 208 | + .provisionedThroughput( |
| 209 | + ProvisionedThroughput.builder() |
| 210 | + .readCapacityUnits(10l) |
| 211 | + .writeCapacityUnits(12l) |
| 212 | + .build() |
| 213 | + ) |
| 214 | + .build(), |
| 215 | + GlobalSecondaryIndex.builder() |
| 216 | + .indexName("globalIndexSecondary") |
| 217 | + .keySchema( |
| 218 | + KeySchemaElement.builder() |
| 219 | + .attributeName("attributeSecondary") |
| 220 | + .build()) |
| 221 | + .provisionedThroughput( |
| 222 | + ProvisionedThroughput.builder() |
| 223 | + .readCapacityUnits(7l) |
| 224 | + .writeCapacityUnits(8l) |
| 225 | + .build() |
| 226 | + ) |
| 227 | + .build())) |
| 228 | + .provisionedThroughput( |
| 229 | + ProvisionedThroughput.builder() |
| 230 | + .readCapacityUnits(1l) |
| 231 | + .writeCapacityUnits(1l) |
| 232 | + .build() |
| 233 | + ) |
| 234 | + .build(); |
| 235 | + } |
| 236 | + |
| 237 | + static SpanDataAssert assertCreateTableRequest(SpanDataAssert span) { |
| 238 | + return span.hasName("DynamoDb.CreateTable") |
| 239 | + .hasKind(SpanKind.CLIENT) |
| 240 | + .hasNoParent() |
| 241 | + .hasAttributesSatisfyingExactly( |
| 242 | + equalTo(SERVER_ADDRESS, "127.0.0.1"), |
| 243 | + equalTo(SERVER_PORT, server.httpPort()), |
| 244 | + equalTo(URL_FULL, server.httpUri().toString()), |
| 245 | + equalTo(HTTP_REQUEST_METHOD, "POST"), |
| 246 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 200), |
| 247 | + equalTo(RPC_SYSTEM, "aws-api"), |
| 248 | + equalTo(RPC_SERVICE, "DynamoDb"), |
| 249 | + equalTo(RPC_METHOD, "CreateTable"), |
| 250 | + equalTo(stringKey("aws.agent"), "java-aws-sdk"), |
| 251 | + equalTo(AWS_REQUEST_ID, "UNKOWN"), |
| 252 | + equalTo(stringKey("aws.table.name"), "sometable"), |
| 253 | + equalTo(DB_SYSTEM, "dynamodb"), |
| 254 | + equalTo(maybeStable(DB_OPERATION), "CreateTable"), |
| 255 | + equalTo(stringKey("aws.dynamodb.global_secondary_indexes"), "[{\"IndexName\":\"globalIndex\",\"KeySchema\":[{\"AttributeName\":\"attribute\"}],\"ProvisionedThroughput\":{\"ReadCapacityUnits\":10,\"WriteCapacityUnits\":12}},{\"IndexName\":\"globalIndexSecondary\",\"KeySchema\":[{\"AttributeName\":\"attributeSecondary\"}],\"ProvisionedThroughput\":{\"ReadCapacityUnits\":7,\"WriteCapacityUnits\":8}}]"), |
| 256 | + equalTo(stringKey("aws.dynamodb.provisioned_throughput.read_capacity_units"), "1"), |
| 257 | + equalTo(stringKey("aws.dynamodb.provisioned_throughput.write_capacity_units"), "1")); |
| 258 | + } |
| 259 | + |
| 260 | + static SpanDataAssert assertQueryRequest(SpanDataAssert span) { |
| 261 | + return span.hasName("DynamoDb.Query") |
| 262 | + .hasKind(SpanKind.CLIENT) |
| 263 | + .hasNoParent() |
| 264 | + .hasAttributesSatisfyingExactly( |
| 265 | + equalTo(SERVER_ADDRESS, "127.0.0.1"), |
| 266 | + equalTo(SERVER_PORT, server.httpPort()), |
| 267 | + equalTo(URL_FULL, server.httpUri().toString()), |
| 268 | + equalTo(HTTP_REQUEST_METHOD, "POST"), |
| 269 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 200), |
| 270 | + equalTo(RPC_SYSTEM, "aws-api"), |
| 271 | + equalTo(RPC_SERVICE, "DynamoDb"), |
| 272 | + equalTo(RPC_METHOD, "Query"), |
| 273 | + equalTo(stringKey("aws.agent"), "java-aws-sdk"), |
| 274 | + equalTo(AWS_REQUEST_ID, "UNKOWN"), |
| 275 | + equalTo(stringKey("aws.table.name"), "sometable"), |
| 276 | + equalTo(DB_SYSTEM, "dynamodb"), |
| 277 | + equalTo(maybeStable(DB_OPERATION), "Query"), |
| 278 | + equalTo(stringKey("aws.dynamodb.limit"), "10"), |
| 279 | + equalTo(stringKey("aws.dynamodb.select"), "ALL_ATTRIBUTES")); |
| 280 | + } |
| 281 | + |
| 282 | + static SpanDataAssert assertDynamoDbRequest(SpanDataAssert span, String operation) { |
| 283 | + return span.hasName("DynamoDb." + operation) |
| 284 | + .hasKind(SpanKind.CLIENT) |
| 285 | + .hasNoParent() |
| 286 | + .hasAttributesSatisfyingExactly( |
| 287 | + equalTo(SERVER_ADDRESS, "127.0.0.1"), |
| 288 | + equalTo(SERVER_PORT, server.httpPort()), |
| 289 | + equalTo(URL_FULL, server.httpUri().toString()), |
| 290 | + equalTo(HTTP_REQUEST_METHOD, "POST"), |
| 291 | + equalTo(HTTP_RESPONSE_STATUS_CODE, 200), |
| 292 | + equalTo(RPC_SYSTEM, "aws-api"), |
| 293 | + equalTo(RPC_SERVICE, "DynamoDb"), |
| 294 | + equalTo(RPC_METHOD, operation), |
| 295 | + equalTo(stringKey("aws.agent"), "java-aws-sdk"), |
| 296 | + equalTo(AWS_REQUEST_ID, "UNKOWN"), |
| 297 | + equalTo(stringKey("aws.table.name"), "sometable"), |
| 298 | + equalTo(DB_SYSTEM, "dynamodb"), |
| 299 | + equalTo(maybeStable(DB_OPERATION), operation)); |
| 300 | + } |
| 301 | +} |
0 commit comments