From 06daffa21fd7c39baaca2b160f5944770fc42194 Mon Sep 17 00:00:00 2001 From: Marc Dougherty Date: Fri, 8 Apr 2022 16:09:00 -0700 Subject: [PATCH] Make the trace flags optional when parsing x-cloud-trace-context Fixes #167 --- .../propagators/cloud_trace_propagator/__init__.py | 4 ++-- .../tests/test_cloud_trace_propagator.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/opentelemetry-propagator-gcp/src/opentelemetry/propagators/cloud_trace_propagator/__init__.py b/opentelemetry-propagator-gcp/src/opentelemetry/propagators/cloud_trace_propagator/__init__.py index fc016041..bae14af5 100644 --- a/opentelemetry-propagator-gcp/src/opentelemetry/propagators/cloud_trace_propagator/__init__.py +++ b/opentelemetry-propagator-gcp/src/opentelemetry/propagators/cloud_trace_propagator/__init__.py @@ -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[0-9a-f]{32})\/(?P[\d]{1,20});o=(?P\d+)" +_TRACE_CONTEXT_HEADER_FORMAT = r"(?P[0-9a-f]{32})\/(?P[\d]{1,20})(;o=(?P\d+))?" _TRACE_CONTEXT_HEADER_RE = re.compile(_TRACE_CONTEXT_HEADER_FORMAT) _FIELDS = {_TRACE_CONTEXT_HEADER_NAME} @@ -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 diff --git a/opentelemetry-propagator-gcp/tests/test_cloud_trace_propagator.py b/opentelemetry-propagator-gcp/tests/test_cloud_trace_propagator.py index cdb8124b..49ff0242 100644 --- a/opentelemetry-propagator-gcp/tests/test_cloud_trace_propagator.py +++ b/opentelemetry-propagator-gcp/tests/test_cloud_trace_propagator.py @@ -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