From 7a8dcf420f5551bf0df969f83928b18c443da5aa Mon Sep 17 00:00:00 2001 From: ksaw Date: Wed, 5 Jun 2024 04:51:36 +0900 Subject: [PATCH] Make the trace flags optional when parsing x-cloud-trace-context (#350) --- .../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 =