|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.awsxray.propagator; |
| 7 | + |
| 8 | +import static io.opentelemetry.contrib.awsxray.propagator.AwsXrayPropagator.TRACE_HEADER_KEY; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import io.opentelemetry.api.trace.Span; |
| 12 | +import io.opentelemetry.api.trace.SpanContext; |
| 13 | +import io.opentelemetry.api.trace.TraceFlags; |
| 14 | +import io.opentelemetry.api.trace.TraceState; |
| 15 | +import io.opentelemetry.api.trace.Tracer; |
| 16 | +import io.opentelemetry.context.Context; |
| 17 | +import io.opentelemetry.context.propagation.TextMapPropagator; |
| 18 | +import io.opentelemetry.sdk.trace.ReadableSpan; |
| 19 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 20 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; |
| 27 | +import uk.org.webcompere.systemstubs.jupiter.SystemStub; |
| 28 | +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; |
| 29 | +import uk.org.webcompere.systemstubs.properties.SystemProperties; |
| 30 | + |
| 31 | +@ExtendWith(SystemStubsExtension.class) |
| 32 | +class AwsXrayLambdaPropagatorTest extends AwsXrayPropagatorTest { |
| 33 | + |
| 34 | + @SystemStub final EnvironmentVariables environmentVariables = new EnvironmentVariables(); |
| 35 | + @SystemStub final SystemProperties systemProperties = new SystemProperties(); |
| 36 | + |
| 37 | + private Tracer tracer; |
| 38 | + |
| 39 | + @Override |
| 40 | + TextMapPropagator propagator() { |
| 41 | + return AwsXrayLambdaPropagator.getInstance(); |
| 42 | + } |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setup() { |
| 46 | + tracer = SdkTracerProvider.builder().build().get("awsxray"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void extract_fromEnvironmentVariable() { |
| 51 | + environmentVariables.set( |
| 52 | + "_X_AMZN_TRACE_ID", |
| 53 | + "Root=1-00000001-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1;Foo=Bar"); |
| 54 | + |
| 55 | + assertThat( |
| 56 | + getSpanContext(propagator().extract(Context.current(), Collections.emptyMap(), GETTER))) |
| 57 | + .isEqualTo( |
| 58 | + SpanContext.createFromRemoteParent( |
| 59 | + "00000001d188f8fa79d48a391a778fa6", |
| 60 | + SPAN_ID, |
| 61 | + TraceFlags.getSampled(), |
| 62 | + TraceState.getDefault())); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void extract_fromSystemProperty() { |
| 67 | + systemProperties.set( |
| 68 | + "com.amazonaws.xray.traceHeader", |
| 69 | + "Root=1-00000001-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=1;Foo=Bar"); |
| 70 | + |
| 71 | + assertThat( |
| 72 | + getSpanContext(propagator().extract(Context.current(), Collections.emptyMap(), GETTER))) |
| 73 | + .isEqualTo( |
| 74 | + SpanContext.createFromRemoteParent( |
| 75 | + "00000001d188f8fa79d48a391a778fa6", |
| 76 | + SPAN_ID, |
| 77 | + TraceFlags.getSampled(), |
| 78 | + TraceState.getDefault())); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void extract_systemPropertyBeforeEnvironmentVariable() { |
| 83 | + environmentVariables.set( |
| 84 | + "_X_AMZN_TRACE_ID", |
| 85 | + "Root=1-00000001-240000000000000000000001;Parent=1600000000000001;Sampled=1;Foo=Bar"); |
| 86 | + systemProperties.set( |
| 87 | + "com.amazonaws.xray.traceHeader", |
| 88 | + "Root=1-00000002-240000000000000000000002;Parent=1600000000000002;Sampled=1;Foo=Baz"); |
| 89 | + |
| 90 | + assertThat( |
| 91 | + getSpanContext(propagator().extract(Context.current(), Collections.emptyMap(), GETTER))) |
| 92 | + .isEqualTo( |
| 93 | + SpanContext.createFromRemoteParent( |
| 94 | + "00000002240000000000000000000002", |
| 95 | + "1600000000000002", |
| 96 | + TraceFlags.getSampled(), |
| 97 | + TraceState.getDefault())); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void addLink_SystemProperty() { |
| 102 | + Map<String, String> carrier = |
| 103 | + Collections.singletonMap( |
| 104 | + TRACE_HEADER_KEY, |
| 105 | + "Root=1-00000001-240000000000000000000001;Parent=1600000000000001;Sampled=1"); |
| 106 | + environmentVariables.set( |
| 107 | + "_X_AMZN_TRACE_ID", |
| 108 | + "Root=1-00000002-240000000000000000000002;Parent=1600000000000002;Sampled=1;Foo=Bar"); |
| 109 | + systemProperties.set( |
| 110 | + "com.amazonaws.xray.traceHeader", |
| 111 | + "Root=1-00000003-240000000000000000000003;Parent=1600000000000003;Sampled=1;Foo=Baz"); |
| 112 | + |
| 113 | + Context extract = propagator().extract(Context.current(), carrier, GETTER); |
| 114 | + ReadableSpan span = |
| 115 | + (ReadableSpan) |
| 116 | + tracer |
| 117 | + .spanBuilder("test") |
| 118 | + .setParent(extract) |
| 119 | + .addLink( |
| 120 | + Span.fromContext(propagator().extract(extract, carrier, GETTER)) |
| 121 | + .getSpanContext()) |
| 122 | + .startSpan(); |
| 123 | + assertThat(span.getParentSpanContext()) |
| 124 | + .isEqualTo( |
| 125 | + SpanContext.createFromRemoteParent( |
| 126 | + "00000003240000000000000000000003", |
| 127 | + "1600000000000003", |
| 128 | + TraceFlags.getSampled(), |
| 129 | + TraceState.getDefault())); |
| 130 | + |
| 131 | + assertThat(span.toSpanData().getLinks()) |
| 132 | + .isEqualTo( |
| 133 | + Collections.singletonList( |
| 134 | + LinkData.create( |
| 135 | + SpanContext.createFromRemoteParent( |
| 136 | + "00000001240000000000000000000001", |
| 137 | + "1600000000000001", |
| 138 | + TraceFlags.getSampled(), |
| 139 | + TraceState.getDefault())))); |
| 140 | + } |
| 141 | +} |
0 commit comments