|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.awssdk.v2_2; |
| 7 | + |
| 8 | +import io.opentelemetry.context.Context; |
| 9 | +import io.opentelemetry.context.propagation.TextMapGetter; |
| 10 | +import io.opentelemetry.contrib.awsxray.propagator.AwsXrayPropagator; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 15 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 16 | +import software.amazon.awssdk.regions.Region; |
| 17 | +import software.amazon.awssdk.services.sqs.SqsClient; |
| 18 | + |
| 19 | +class AwsXrayPropagatorTest { |
| 20 | + private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = |
| 21 | + StaticCredentialsProvider.create( |
| 22 | + AwsBasicCredentials.create("my-access-key", "my-secret-key")); |
| 23 | + |
| 24 | + @Test |
| 25 | + void testAgentShadesAwsXrayPropagator() { |
| 26 | + // first build a SqsClient to trigger instrumentation |
| 27 | + SqsClient.builder() |
| 28 | + .region(Region.AP_NORTHEAST_1) |
| 29 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 30 | + .build(); |
| 31 | + // verify that AwsXrayPropagator is usable, if agent has not shaded AwsXrayPropagator this will |
| 32 | + // fail with NoSuchMethodError |
| 33 | + AwsXrayPropagator.getInstance() |
| 34 | + .extract( |
| 35 | + Context.root(), |
| 36 | + Collections.singletonMap( |
| 37 | + "X-Amzn-Trace-Id", |
| 38 | + "Root=1-35a77be2-beae321878f706079d392ac3;Parent=df79f9d51134dc0b;Sampled=1"), |
| 39 | + StringMapGetter.INSTANCE); |
| 40 | + } |
| 41 | + |
| 42 | + private enum StringMapGetter implements TextMapGetter<Map<String, String>> { |
| 43 | + INSTANCE; |
| 44 | + |
| 45 | + @Override |
| 46 | + public Iterable<String> keys(Map<String, String> map) { |
| 47 | + return map.keySet(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String get(Map<String, String> map, String s) { |
| 52 | + return map.get(s); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments