Skip to content

Commit

Permalink
Make the trace flags optional when parsing x-cloud-trace-context
Browse files Browse the repository at this point in the history
  • Loading branch information
muncus committed Apr 8, 2022
1 parent 224afa1 commit 06daffa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from opentelemetry.trace.span import SpanContext, TraceFlags, format_trace_id

_TRACE_CONTEXT_HEADER_NAME = "x-cloud-trace-context"
_TRACE_CONTEXT_HEADER_FORMAT = r"(?P<trace_id>[0-9a-f]{32})\/(?P<span_id>[\d]{1,20});o=(?P<trace_flags>\d+)"
_TRACE_CONTEXT_HEADER_FORMAT = r"(?P<trace_id>[0-9a-f]{32})\/(?P<span_id>[\d]{1,20})(;o=(?P<trace_flags>\d+))?"
_TRACE_CONTEXT_HEADER_RE = re.compile(_TRACE_CONTEXT_HEADER_FORMAT)
_FIELDS = {_TRACE_CONTEXT_HEADER_NAME}

Expand Down Expand Up @@ -71,7 +71,7 @@ def extract(

trace_id = match.group("trace_id")
span_id = match.group("span_id")
trace_options = match.group("trace_flags")
trace_options = match.group("trace_flags") or "0"

if trace_id == "0" * 32 or int(span_id) == 0:
return context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ def test_valid_header(self):
self.assertEqual(new_span_context.trace_flags, TraceFlags(0))
self.assertTrue(new_span_context.is_remote)

header = "{}/{}".format(format_trace_id(self.valid_trace_id), 345)
new_span_context = self._extract_span_context(header)
self.assertEqual(new_span_context.trace_id, self.valid_trace_id)
self.assertEqual(new_span_context.span_id, 345)
self.assertEqual(new_span_context.trace_flags, TraceFlags(0))
self.assertTrue(new_span_context.is_remote)

def test_mixed_case_header_key(self):
header_value = "{}/{};o=1".format(
format_trace_id(self.valid_trace_id), self.valid_span_id
Expand Down

0 comments on commit 06daffa

Please sign in to comment.