From a8a85c61292ab20d6d4f4b6d6f24551a56d60f84 Mon Sep 17 00:00:00 2001 From: John Watson Date: Thu, 6 May 2021 16:58:42 -0700 Subject: [PATCH] =?UTF-8?q?Add=20tests=20and=20update=20behavior=20to=20ve?= =?UTF-8?q?rify=20propagators=20do=20not=20alter=20the=20=E2=80=A6=20(#319?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add tests and update behavior to verify propagators do not alter the context when failing to parse inputs. * Ad a couple tests around null contexts * formatting --- .../propagation/W3CBaggagePropagator.java | 4 +- .../propagation/W3CBaggagePropagatorTest.java | 9 +-- .../W3CTraceContextPropagatorTest.java | 79 ++++++++----------- .../MultiTextMapPropagatorTest.java | 27 ++++++- .../NoopTextMapPropagatorTest.java | 33 ++++++++ .../extension/aws/AwsXrayPropagator.java | 5 +- .../extension/aws/AwsXrayPropagatorTest.java | 31 ++++---- .../trace/propagation/B3PropagatorTest.java | 55 ++++++------- .../propagation/JaegerPropagatorTest.java | 34 ++++---- .../propagation/OtTracePropagatorTest.java | 19 +++-- 10 files changed, 163 insertions(+), 133 deletions(-) diff --git a/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagator.java b/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagator.java index 4bffc0c7841..198a0d656f9 100644 --- a/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagator.java +++ b/api/all/src/main/java/io/opentelemetry/api/baggage/propagation/W3CBaggagePropagator.java @@ -78,14 +78,14 @@ public Context extract(Context context, @Nullable C carrier, TextMapGetter invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(W3CTraceContextPropagator.TRACE_PARENT, ""); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test void extract_invalidDelimiters() { Map carrier = new LinkedHashMap<>(); + Context input = Context.current(); + Context result; + carrier.put( W3CTraceContextPropagator.TRACE_PARENT, "01+" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02"); - assertThat( - getSpanContext(w3cTraceContextPropagator.extract(Context.current(), carrier, getter))) - .isEqualTo(SpanContext.getInvalid()); + result = w3cTraceContextPropagator.extract(input, carrier, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isEqualTo(SpanContext.getInvalid()); carrier.put( W3CTraceContextPropagator.TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "+" + SPAN_ID_BASE16 + "-00-02"); - assertThat( - getSpanContext(w3cTraceContextPropagator.extract(Context.current(), carrier, getter))) - .isEqualTo(SpanContext.getInvalid()); + result = w3cTraceContextPropagator.extract(input, carrier, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isEqualTo(SpanContext.getInvalid()); carrier.put( W3CTraceContextPropagator.TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "+00-02"); - assertThat( - getSpanContext(w3cTraceContextPropagator.extract(Context.current(), carrier, getter))) - .isEqualTo(SpanContext.getInvalid()); + result = w3cTraceContextPropagator.extract(input, carrier, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isEqualTo(SpanContext.getInvalid()); carrier.put( W3CTraceContextPropagator.TRACE_PARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00+02"); - assertThat( - getSpanContext(w3cTraceContextPropagator.extract(Context.current(), carrier, getter))) - .isEqualTo(SpanContext.getInvalid()); + result = w3cTraceContextPropagator.extract(input, carrier, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isEqualTo(SpanContext.getInvalid()); } @Test @@ -342,10 +342,7 @@ void extract_InvalidTraceId() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID_BASE16 + "-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -354,10 +351,7 @@ void extract_InvalidTraceId_Size() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "00-" + SPAN_ID_BASE16 + "-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -366,10 +360,7 @@ void extract_InvalidSpanId() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + "abcdefghijklmnop" + "-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -378,10 +369,7 @@ void extract_InvalidSpanId_Size() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "00-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -390,10 +378,7 @@ void extract_InvalidTraceFlags() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-gh"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -402,10 +387,14 @@ void extract_InvalidTraceFlags_Size() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-0100"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); + } + + private void verifyInvalidBehavior(Map invalidHeaders) { + Context input = Context.current(); + Context result = w3cTraceContextPropagator.extract(input, invalidHeaders, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isSameAs(SpanContext.getInvalid()); } @Test @@ -474,10 +463,7 @@ void extract_InvalidVersion_ff() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "ff-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -486,10 +472,7 @@ void extract_InvalidTraceparent_extraTrailing() { invalidHeaders.put( W3CTraceContextPropagator.TRACE_PARENT, "00-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-01"); - assertThat( - getSpanContext( - w3cTraceContextPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test diff --git a/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java b/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java index f572985ee8c..39507797463 100644 --- a/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java +++ b/context/src/test/java/io/opentelemetry/context/propagation/MultiTextMapPropagatorTest.java @@ -93,6 +93,19 @@ void inject_allDelegated() { verify(propagator3).inject(context, carrier, setter); } + @Test + void inject_nullContextAllDelegated() { + Map carrier = new HashMap<>(); + Context context = null; + TextMapSetter> setter = Map::put; + + TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2, propagator3); + prop.inject(context, carrier, setter); + verify(propagator1).inject(context, carrier, setter); + verify(propagator2).inject(context, carrier, setter); + verify(propagator3).inject(context, carrier, setter); + } + @Test void extract_noPropagators() { Map carrier = new HashMap<>(); @@ -121,7 +134,6 @@ void extract_found_all() { @Test void extract_notFound() { - Map carrier = new HashMap<>(); Context context = mock(Context.class); when(propagator1.extract(context, carrier, getter)).thenReturn(context); @@ -132,4 +144,17 @@ void extract_notFound() { assertThat(result).isSameAs(context); } + + @Test + void extract_nullContext() { + Map carrier = new HashMap<>(); + Context context = null; + when(propagator1.extract(context, carrier, getter)).thenReturn(context); + when(propagator2.extract(context, carrier, getter)).thenReturn(context); + + TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2); + Context result = prop.extract(context, carrier, getter); + + assertThat(result).isSameAs(context); + } } diff --git a/context/src/test/java/io/opentelemetry/context/propagation/NoopTextMapPropagatorTest.java b/context/src/test/java/io/opentelemetry/context/propagation/NoopTextMapPropagatorTest.java index 7eb7d09e64d..1d16115d9aa 100644 --- a/context/src/test/java/io/opentelemetry/context/propagation/NoopTextMapPropagatorTest.java +++ b/context/src/test/java/io/opentelemetry/context/propagation/NoopTextMapPropagatorTest.java @@ -7,6 +7,9 @@ import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.context.Context; +import java.util.HashMap; +import javax.annotation.Nullable; import org.junit.jupiter.api.Test; class NoopTextMapPropagatorTest { @@ -15,4 +18,34 @@ class NoopTextMapPropagatorTest { void noopFields() { assertThat(TextMapPropagator.noop().fields()).isEmpty(); } + + @Test + void extract_contextUnchanged() { + Context input = Context.current(); + Context result = + TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter()); + assertThat(result).isSameAs(input); + } + + @Test + void extract_nullContext() { + Context input = null; + Context result = + TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter()); + assertThat(result).isSameAs(input); + } + + private static class HashMapTextMapGetter + implements TextMapGetter> { + @Override + public Iterable keys(HashMap carrier) { + return null; + } + + @Nullable + @Override + public String get(@Nullable HashMap carrier, String key) { + return null; + } + } } diff --git a/extensions/aws/src/main/java/io/opentelemetry/extension/aws/AwsXrayPropagator.java b/extensions/aws/src/main/java/io/opentelemetry/extension/aws/AwsXrayPropagator.java index 1ffb275d2a3..a596ddfaffb 100644 --- a/extensions/aws/src/main/java/io/opentelemetry/extension/aws/AwsXrayPropagator.java +++ b/extensions/aws/src/main/java/io/opentelemetry/extension/aws/AwsXrayPropagator.java @@ -233,8 +233,9 @@ private static Context getContextFromHeader( spanId, isSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault()); - - context = context.with(Span.wrap(spanContext)); + if (spanContext.isValid()) { + context = context.with(Span.wrap(spanContext)); + } if (baggage != null) { context = context.with(baggage.build()); } diff --git a/extensions/aws/src/test/java/io/opentelemetry/extension/aws/AwsXrayPropagatorTest.java b/extensions/aws/src/test/java/io/opentelemetry/extension/aws/AwsXrayPropagatorTest.java index 3ef7f16e08b..1ad62173db2 100644 --- a/extensions/aws/src/test/java/io/opentelemetry/extension/aws/AwsXrayPropagatorTest.java +++ b/extensions/aws/src/test/java/io/opentelemetry/extension/aws/AwsXrayPropagatorTest.java @@ -273,8 +273,7 @@ void extract_EmptyHeaderValue() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(TRACE_HEADER_KEY, ""); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -284,8 +283,7 @@ void extract_InvalidTraceId() { TRACE_HEADER_KEY, "Root=abcdefghijklmnopabcdefghijklmnop;Parent=53995c3f42cd8ad8;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -295,8 +293,7 @@ void extract_InvalidTraceId_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa600;Parent=53995c3f42cd8ad8;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -306,8 +303,7 @@ void extract_InvalidSpanId() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=abcdefghijklmnop;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -317,8 +313,7 @@ void extract_InvalidSpanId_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad800;Sampled=0"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -328,8 +323,7 @@ void extract_InvalidFlags() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled="); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -339,8 +333,7 @@ void extract_InvalidFlags_Size() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=10220"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -350,8 +343,14 @@ void extract_InvalidFlags_NonNumeric() { TRACE_HEADER_KEY, "Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=53995c3f42cd8ad8;Sampled=a"); - assertThat(getSpanContext(xrayPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); + } + + private void verifyInvalidBehavior(Map invalidHeaders) { + Context input = Context.current(); + Context result = xrayPropagator.extract(input, invalidHeaders, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isSameAs(SpanContext.getInvalid()); } @Test diff --git a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/B3PropagatorTest.java b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/B3PropagatorTest.java index e69ca3c46f9..ed996b2c961 100644 --- a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/B3PropagatorTest.java +++ b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/B3PropagatorTest.java @@ -234,8 +234,7 @@ void extract_InvalidTraceId_NotHex() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, "g" + TRACE_ID.substring(1)); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -244,8 +243,7 @@ void extract_InvalidTraceId_TooShort() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID.substring(2)); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -254,8 +252,7 @@ void extract_InvalidTraceId_TooLong() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID + "00"); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -264,8 +261,7 @@ void extract_InvalidTraceId_AllZero() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID_ALL_ZERO); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -274,8 +270,7 @@ void extract_InvalidSpanId_NotHex() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, "g" + SPAN_ID.substring(1)); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -284,8 +279,7 @@ void extract_InvalidSpanId_TooShort() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID.substring(2)); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -294,8 +288,7 @@ void extract_InvalidSpanId_TooLong() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID + "00"); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -304,8 +297,7 @@ void extract_InvalidSpanId_AllZeros() { invalidHeaders.put(B3Propagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(B3Propagator.SPAN_ID_HEADER, SPAN_ID_ALL_ZERO); invalidHeaders.put(B3Propagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -481,8 +473,14 @@ void extract_Null_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, null); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); + } + + private void verifyInvalidBehavior(Map invalidHeaders) { + Context input = Context.current(); + Context extractedContext = b3Propagator.extract(input, invalidHeaders, getter); + assertThat(extractedContext).isSameAs(input); + assertThat(getSpanContext(extractedContext)).isSameAs(SpanContext.getInvalid()); } @Test @@ -490,8 +488,7 @@ void extract_Empty_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, ""); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -501,8 +498,7 @@ void extract_InvalidTraceId_SingleHeader() { B3Propagator.COMBINED_HEADER, "abcdefghijklmnopabcdefghijklmnop" + "-" + SPAN_ID + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -512,8 +508,7 @@ void extract_InvalidTraceId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, "abcdefghijklmnopabcdefghijklmnop" + "00" + "-" + SPAN_ID + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -522,8 +517,7 @@ void extract_InvalidSpanId_SingleHeader() { invalidHeaders.put( B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + "abcdefghijklmnop" + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -533,8 +527,7 @@ void extract_InvalidSpanId_Size_SingleHeader() { B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + "abcdefghijklmnop" + "00" + "-" + Common.TRUE_INT); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -542,8 +535,7 @@ void extract_TooFewParts_SingleHeader() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(B3Propagator.COMBINED_HEADER, TRACE_ID); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -552,8 +544,7 @@ void extract_TooManyParts_SingleHeader() { invalidHeaders.put( B3Propagator.COMBINED_HEADER, TRACE_ID + "-" + SPAN_ID + "-" + Common.TRUE_INT + "-extra-extra"); - assertThat(getSpanContext(b3Propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test diff --git a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/JaegerPropagatorTest.java b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/JaegerPropagatorTest.java index 83c19ce055e..a518d9d3d5a 100644 --- a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/JaegerPropagatorTest.java +++ b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/JaegerPropagatorTest.java @@ -213,8 +213,14 @@ void extract_NotEnoughParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); + } + + private void verifyInvalidBehavior(Map invalidHeaders) { + Context input = Context.current(); + Context result = jaegerPropagator.extract(input, invalidHeaders, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isSameAs(SpanContext.getInvalid()); } @Test @@ -222,8 +228,7 @@ void extract_TooManyParts() { Map invalidHeaders = new LinkedHashMap<>(); invalidHeaders.put(PROPAGATION_HEADER, "aa:bb:cc:dd:ee"); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -234,8 +239,7 @@ void extract_InvalidTraceId() { generateTraceIdHeaderValue( "abcdefghijklmnopabcdefghijklmnop", SPAN_ID, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -245,8 +249,7 @@ void extract_InvalidTraceId_Size() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID + "00", SPAN_ID, DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -256,8 +259,7 @@ void extract_InvalidSpanId() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, "abcdefghijklmnop", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -267,8 +269,7 @@ void extract_InvalidSpanId_Size() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, SPAN_ID + "00", DEPRECATED_PARENT_SPAN, "0")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -278,8 +279,7 @@ void extract_InvalidFlags() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, SPAN_ID, DEPRECATED_PARENT_SPAN, "")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -289,8 +289,7 @@ void extract_InvalidFlags_Size() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, SPAN_ID, DEPRECATED_PARENT_SPAN, "10220")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -300,8 +299,7 @@ void extract_InvalidFlags_NonNumeric() { PROPAGATION_HEADER, generateTraceIdHeaderValue(TRACE_ID, SPAN_ID, DEPRECATED_PARENT_SPAN, "abcdefr")); - assertThat(getSpanContext(jaegerPropagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test diff --git a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/OtTracePropagatorTest.java b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/OtTracePropagatorTest.java index f5fb42bcd89..2da3a970ed3 100644 --- a/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/OtTracePropagatorTest.java +++ b/extensions/trace-propagators/src/test/java/io/opentelemetry/extension/trace/propagation/OtTracePropagatorTest.java @@ -264,8 +264,7 @@ void extract_InvalidTraceId_Size() { invalidHeaders.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID + "00"); invalidHeaders.put(OtTracePropagator.SPAN_ID_HEADER, SPAN_ID); invalidHeaders.put(OtTracePropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -274,8 +273,7 @@ void extract_InvalidSpanId() { invalidHeaders.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(OtTracePropagator.SPAN_ID_HEADER, "abcdefghijklmnop"); invalidHeaders.put(OtTracePropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); } @Test @@ -284,15 +282,20 @@ void extract_InvalidSpanId_Size() { invalidHeaders.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID); invalidHeaders.put(OtTracePropagator.SPAN_ID_HEADER, "abcdefghijklmnop" + "00"); invalidHeaders.put(OtTracePropagator.SAMPLED_HEADER, Common.TRUE_INT); - assertThat(getSpanContext(propagator.extract(Context.current(), invalidHeaders, getter))) - .isSameAs(SpanContext.getInvalid()); + verifyInvalidBehavior(invalidHeaders); + } + + private void verifyInvalidBehavior(Map invalidHeaders) { + Context input = Context.current(); + Context result = propagator.extract(input, invalidHeaders, getter); + assertThat(result).isSameAs(input); + assertThat(getSpanContext(result)).isSameAs(SpanContext.getInvalid()); } @Test void extract_emptyCarrier() { Map emptyHeaders = new HashMap<>(); - assertThat(getSpanContext(propagator.extract(Context.current(), emptyHeaders, getter))) - .isEqualTo(SpanContext.getInvalid()); + verifyInvalidBehavior(emptyHeaders); } @Test