Skip to content

Commit

Permalink
Make the trace flags optional when parsing x-cloud-trace-context (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
sawadakaku authored Jun 4, 2024
1 parent f04d7e9 commit 7a8dcf4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class XCloudTraceContextPropagator implements TextMapPropagator {
private static final String FIELD = "x-cloud-trace-context";
private static final Collection<String> FIELDS = Collections.singletonList(FIELD);
private static final Pattern VALUE_PATTERN =
Pattern.compile("(?<traceid>[0-9a-f]{32})\\/(?<spanid>[\\d]{1,20});o=(?<sampled>\\d+)");
Pattern.compile("(?<traceid>[0-9a-f]{32})\\/(?<spanid>[\\d]{1,20})(;o=(?<sampled>\\d+))?");
private static final Logger LOGGER =
Logger.getLogger(XCloudTraceContextPropagator.class.getCanonicalName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ public void testExtractNotSampled() {
Assert.assertEquals(false, span.getSpanContext().getTraceFlags().isSampled());
}

@Test
public void testExtractWithoutTraceFlags() {
Context context = Context.root();
Map<String, String> 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 =
Expand Down

0 comments on commit 7a8dcf4

Please sign in to comment.