|
| 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.awspropagator; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import io.opentelemetry.api.baggage.Baggage; |
| 21 | +import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; |
| 22 | +import io.opentelemetry.api.trace.Span; |
| 23 | +import io.opentelemetry.api.trace.SpanContext; |
| 24 | +import io.opentelemetry.api.trace.TraceFlags; |
| 25 | +import io.opentelemetry.api.trace.TraceState; |
| 26 | +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; |
| 27 | +import io.opentelemetry.context.Context; |
| 28 | +import io.opentelemetry.context.propagation.TextMapGetter; |
| 29 | +import io.opentelemetry.context.propagation.TextMapPropagator; |
| 30 | +import io.opentelemetry.extension.aws.AwsXrayPropagator; |
| 31 | +import io.opentelemetry.extension.trace.propagation.B3Propagator; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.Arguments; |
| 39 | +import org.junit.jupiter.params.provider.ArgumentsProvider; |
| 40 | +import org.junit.jupiter.params.provider.ArgumentsSource; |
| 41 | + |
| 42 | +class AwsCompositePropagatorTest { |
| 43 | + |
| 44 | + private static final Span SPAN1 = |
| 45 | + Span.wrap( |
| 46 | + SpanContext.createFromRemoteParent( |
| 47 | + "ff000000000000000000000000000041", |
| 48 | + "ff00000000000041", |
| 49 | + TraceFlags.getDefault(), |
| 50 | + TraceState.getDefault())); |
| 51 | + private static final Span SPAN2 = |
| 52 | + Span.wrap( |
| 53 | + SpanContext.createFromRemoteParent( |
| 54 | + "ff000000000000000000000000000042", |
| 55 | + "ff00000000000042", |
| 56 | + TraceFlags.getDefault(), |
| 57 | + TraceState.getDefault())); |
| 58 | + |
| 59 | + private static final TextMapGetter<Map<String, String>> MAP_GETTER = |
| 60 | + new TextMapGetter<>() { |
| 61 | + @Override |
| 62 | + public Iterable<String> keys(Map<String, String> carrier) { |
| 63 | + return carrier.keySet(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String get(Map<String, String> carrier, String key) { |
| 68 | + return carrier.get(key); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + @ParameterizedTest |
| 73 | + @ArgumentsSource(SupportedPropagators.class) |
| 74 | + void extractsAndInjectAws(TextMapPropagator propagator) { |
| 75 | + Map<String, String> map = new HashMap<>(); |
| 76 | + propagator.inject(Context.root().with(SPAN1), map, Map::put); |
| 77 | + TextMapPropagator awsPropagator = AwsCompositePropagator.injectingAwsFormat(); |
| 78 | + Context context = awsPropagator.extract(Context.root(), map, MAP_GETTER); |
| 79 | + assertThat(Span.fromContext(context).getSpanContext()).isEqualTo(SPAN1.getSpanContext()); |
| 80 | + |
| 81 | + Map<String, String> map2 = new HashMap<>(); |
| 82 | + awsPropagator.inject(context, map2, Map::put); |
| 83 | + |
| 84 | + Map<String, String> map3 = new HashMap<>(); |
| 85 | + AwsXrayPropagator.getInstance().inject(context, map3, Map::put); |
| 86 | + assertThat(map2).containsExactlyInAnyOrderEntriesOf(map3); |
| 87 | + } |
| 88 | + |
| 89 | + @ParameterizedTest |
| 90 | + @ArgumentsSource(SupportedPropagators.class) |
| 91 | + void extractsAndInjectExtracted(TextMapPropagator propagator) { |
| 92 | + Map<String, String> map = new HashMap<>(); |
| 93 | + propagator.inject(Context.root().with(SPAN1), map, Map::put); |
| 94 | + TextMapPropagator awsPropagator = AwsCompositePropagator.injectingExtractedFormat(); |
| 95 | + Context context = awsPropagator.extract(Context.root(), map, MAP_GETTER); |
| 96 | + assertThat(Span.fromContext(context).getSpanContext()).isEqualTo(SPAN1.getSpanContext()); |
| 97 | + |
| 98 | + Map<String, String> map2 = new HashMap<>(); |
| 99 | + awsPropagator.inject(context, map2, Map::put); |
| 100 | + |
| 101 | + Map<String, String> map3 = new HashMap<>(); |
| 102 | + propagator.inject(context, map3, Map::put); |
| 103 | + assertThat(map2).containsExactlyInAnyOrderEntriesOf(map3); |
| 104 | + } |
| 105 | + |
| 106 | + @ParameterizedTest |
| 107 | + @ArgumentsSource(SupportedPropagators.class) |
| 108 | + void baggageExtractedFormat(TextMapPropagator propagator) { |
| 109 | + Map<String, String> map = new HashMap<>(); |
| 110 | + propagator.inject(Context.root().with(SPAN1), map, Map::put); |
| 111 | + Baggage baggage = Baggage.builder().put("cat", "meow").build(); |
| 112 | + W3CBaggagePropagator.getInstance().inject(Context.root().with(baggage), map, Map::put); |
| 113 | + TextMapPropagator awsPropagator = AwsCompositePropagator.injectingExtractedFormat(); |
| 114 | + Context context = awsPropagator.extract(Context.root(), map, MAP_GETTER); |
| 115 | + assertThat(Span.fromContext(context).getSpanContext()).isEqualTo(SPAN1.getSpanContext()); |
| 116 | + assertThat(Baggage.fromContext(context)).isEqualTo(baggage); |
| 117 | + |
| 118 | + Map<String, String> map2 = new HashMap<>(); |
| 119 | + awsPropagator.inject(context, map2, Map::put); |
| 120 | + |
| 121 | + Map<String, String> map3 = new HashMap<>(); |
| 122 | + propagator.inject(context, map3, Map::put); |
| 123 | + |
| 124 | + if (propagator != AwsXrayPropagator.getInstance()) { |
| 125 | + W3CBaggagePropagator.getInstance().inject(context, map3, Map::put); |
| 126 | + } |
| 127 | + |
| 128 | + assertThat(map2).containsExactlyInAnyOrderEntriesOf(map3); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void awsPrioritized() { |
| 133 | + Map<String, String> map = new HashMap<>(); |
| 134 | + AwsXrayPropagator.getInstance().inject(Context.root().with(SPAN1), map, Map::put); |
| 135 | + W3CTraceContextPropagator.getInstance().inject(Context.root().with(SPAN2), map, Map::put); |
| 136 | + assertThat( |
| 137 | + Span.fromContext( |
| 138 | + AwsCompositePropagator.injectingExtractedFormat() |
| 139 | + .extract(Context.root(), map, MAP_GETTER)) |
| 140 | + .getSpanContext()) |
| 141 | + .isEqualTo(SPAN1.getSpanContext()); |
| 142 | + } |
| 143 | + |
| 144 | + private static class SupportedPropagators implements ArgumentsProvider { |
| 145 | + @Override |
| 146 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { |
| 147 | + return Stream.of( |
| 148 | + AwsXrayPropagator.getInstance(), |
| 149 | + W3CTraceContextPropagator.getInstance(), |
| 150 | + B3Propagator.injectingMultiHeaders()) |
| 151 | + .map(Arguments::of); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments