Skip to content

Commit 96fd84f

Browse files
authored
Fixed B3 Propagator (#1750)
1 parent cad261e commit 96fd84f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#1721](https://github.com/open-telemetry/opentelemetry-python/pull/1721))
2020
- Update bootstrap cmd to use exact version when installing instrumentation packages.
2121
([#1722](https://github.com/open-telemetry/opentelemetry-python/pull/1722))
22+
- Fix B3 propagator to never return None.
23+
([#1750](https://github.com/open-telemetry/opentelemetry-python/pull/1750))
2224

2325

2426
## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26

propagator/opentelemetry-propagator-b3/src/opentelemetry/propagators/b3/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def extract(
9797
or self._trace_id_regex.fullmatch(trace_id) is None
9898
or self._span_id_regex.fullmatch(span_id) is None
9999
):
100+
if context is None:
101+
return trace.set_span_in_context(trace.INVALID_SPAN, context)
100102
return context
101103

102104
trace_id = int(trace_id, 16)
@@ -119,7 +121,8 @@ def extract(
119121
trace_flags=trace.TraceFlags(options),
120122
trace_state=trace.TraceState(),
121123
)
122-
)
124+
),
125+
context,
123126
)
124127

125128
def inject(

propagator/opentelemetry-propagator-b3/tests/test_b3_format.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def get_child_parent_new_carrier(old_carrier):
5151

5252

5353
class TestB3Format(unittest.TestCase):
54+
# pylint: disable=too-many-public-methods
55+
5456
@classmethod
5557
def setUpClass(cls):
5658
generator = id_generator.RandomIdGenerator()
@@ -215,6 +217,31 @@ def test_flags_and_sampling(self):
215217

216218
self.assertEqual(new_carrier[FORMAT.SAMPLED_KEY], "1")
217219

220+
def test_derived_ctx_is_returned_for_success(self):
221+
"""Ensure returned context is derived from the given context."""
222+
old_ctx = {"k1": "v1"}
223+
new_ctx = FORMAT.extract(
224+
{
225+
FORMAT.TRACE_ID_KEY: self.serialized_trace_id,
226+
FORMAT.SPAN_ID_KEY: self.serialized_span_id,
227+
FORMAT.FLAGS_KEY: "1",
228+
},
229+
old_ctx,
230+
)
231+
self.assertIn("current-span", new_ctx)
232+
for key, value in old_ctx.items():
233+
self.assertIn(key, new_ctx)
234+
self.assertEqual(new_ctx[key], value)
235+
236+
def test_derived_ctx_is_returned_for_failure(self):
237+
"""Ensure returned context is derived from the given context."""
238+
old_ctx = {"k2": "v2"}
239+
new_ctx = FORMAT.extract({}, old_ctx)
240+
self.assertNotIn("current-span", new_ctx)
241+
for key, value in old_ctx.items():
242+
self.assertIn(key, new_ctx)
243+
self.assertEqual(new_ctx[key], value)
244+
218245
def test_64bit_trace_id(self):
219246
"""64 bit trace ids should be padded to 128 bit trace ids."""
220247
trace_id_64_bit = self.serialized_trace_id[:16]
@@ -334,3 +361,12 @@ def test_fields(self):
334361
inject_fields.add(call[1][1])
335362

336363
self.assertEqual(FORMAT.fields, inject_fields)
364+
365+
def test_extract_none_context(self):
366+
"""Given no trace ID, do not modify context"""
367+
old_ctx = None
368+
369+
carrier = {}
370+
new_ctx = FORMAT.extract(carrier, old_ctx)
371+
self.assertIsNotNone(new_ctx)
372+
self.assertEqual(new_ctx["current-span"], trace_api.INVALID_SPAN)

0 commit comments

Comments
 (0)