From a5bddae4121cf2932065d1b1764e71a426c1b107 Mon Sep 17 00:00:00 2001 From: sawadakaku Date: Mon, 27 May 2024 03:38:57 +0900 Subject: [PATCH] Make the trace flags optional when parsing x-cloud-trace-context --- .../XCloudTraceContextPropagator.java | 2 +- .../propagators/PropagatorTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/propagators/gcp/src/main/java/com/google/cloud/opentelemetry/propagators/XCloudTraceContextPropagator.java b/propagators/gcp/src/main/java/com/google/cloud/opentelemetry/propagators/XCloudTraceContextPropagator.java index c4882619..c10c02bd 100644 --- a/propagators/gcp/src/main/java/com/google/cloud/opentelemetry/propagators/XCloudTraceContextPropagator.java +++ b/propagators/gcp/src/main/java/com/google/cloud/opentelemetry/propagators/XCloudTraceContextPropagator.java @@ -42,7 +42,7 @@ public final class XCloudTraceContextPropagator implements TextMapPropagator { private static final String FIELD = "x-cloud-trace-context"; private static final Collection FIELDS = Collections.singletonList(FIELD); private static final Pattern VALUE_PATTERN = - Pattern.compile("(?[0-9a-f]{32})\\/(?[\\d]{1,20});o=(?\\d+)"); + Pattern.compile("(?[0-9a-f]{32})\\/(?[\\d]{1,20})(;o=(?\\d+))?"); private static final Logger LOGGER = Logger.getLogger(XCloudTraceContextPropagator.class.getCanonicalName()); diff --git a/propagators/gcp/src/test/java/com/google/cloud/opentelemetry/propagators/PropagatorTest.java b/propagators/gcp/src/test/java/com/google/cloud/opentelemetry/propagators/PropagatorTest.java index 8db615b8..a3a68191 100644 --- a/propagators/gcp/src/test/java/com/google/cloud/opentelemetry/propagators/PropagatorTest.java +++ b/propagators/gcp/src/test/java/com/google/cloud/opentelemetry/propagators/PropagatorTest.java @@ -93,6 +93,22 @@ public void testExtractNotSampled() { Assert.assertEquals(false, span.getSpanContext().getTraceFlags().isSampled()); } + @Test + public void testExtractWithoutTraceFlags() { + Context context = Context.root(); + Map carrier = new HashMap<>(); + carrier.put("x-cloud-trace-context", "00000000000000000000000000000010/15"); + TextMapPropagator propagator = new XCloudTraceContextPropagator(false); + + // Now try to extract the value. + Context updated = propagator.extract(context, carrier, GETTER); + Span span = Span.fromContext(updated); + Assert.assertNotNull(span); + Assert.assertEquals("00000000000000000000000000000010", span.getSpanContext().getTraceId()); + Assert.assertEquals("000000000000000f", span.getSpanContext().getSpanId()); + Assert.assertEquals(false, span.getSpanContext().getTraceFlags().isSampled()); + } + @Test public void testNotInjectOneway() { Span span =