Skip to content

Fixed B3 Propagator #1750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1721](https://github.com/open-telemetry/opentelemetry-python/pull/1721))
- Update bootstrap cmd to use exact version when installing instrumentation packages.
([#1722](https://github.com/open-telemetry/opentelemetry-python/pull/1722))
- Fix B3 propagator to never return None.
([#1750](https://github.com/open-telemetry/opentelemetry-python/pull/1750))


## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def extract(
or self._trace_id_regex.fullmatch(trace_id) is None
or self._span_id_regex.fullmatch(span_id) is None
):
if context is None:
return trace.set_span_in_context(trace.INVALID_SPAN, context)
return context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also fix the composite propagator issue. While you are at it, could you also fix another issue with B3 when there is a valid propagation header. context should be passed in this set_span_in_context call.

return trace.set_span_in_context(
trace.NonRecordingSpan(
trace.SpanContext(
# trace an span ids are encoded in hex, so must be converted
trace_id=trace_id,
span_id=span_id,
is_remote=True,
trace_flags=trace.TraceFlags(options),
trace_state=trace.TraceState(),
)
)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also added tests to ensure the returned context is always derived from the provided one.


trace_id = int(trace_id, 16)
Expand All @@ -119,7 +121,8 @@ def extract(
trace_flags=trace.TraceFlags(options),
trace_state=trace.TraceState(),
)
)
),
context,
)

def inject(
Expand Down
36 changes: 36 additions & 0 deletions propagator/opentelemetry-propagator-b3/tests/test_b3_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def get_child_parent_new_carrier(old_carrier):


class TestB3Format(unittest.TestCase):
# pylint: disable=too-many-public-methods

@classmethod
def setUpClass(cls):
generator = id_generator.RandomIdGenerator()
Expand Down Expand Up @@ -215,6 +217,31 @@ def test_flags_and_sampling(self):

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

def test_derived_ctx_is_returned_for_success(self):
"""Ensure returned context is derived from the given context."""
old_ctx = {"k1": "v1"}
new_ctx = FORMAT.extract(
{
FORMAT.TRACE_ID_KEY: self.serialized_trace_id,
FORMAT.SPAN_ID_KEY: self.serialized_span_id,
FORMAT.FLAGS_KEY: "1",
},
old_ctx,
)
self.assertIn("current-span", new_ctx)
for key, value in old_ctx.items():
self.assertIn(key, new_ctx)
self.assertEqual(new_ctx[key], value)

def test_derived_ctx_is_returned_for_failure(self):
"""Ensure returned context is derived from the given context."""
old_ctx = {"k2": "v2"}
new_ctx = FORMAT.extract({}, old_ctx)
self.assertNotIn("current-span", new_ctx)
for key, value in old_ctx.items():
self.assertIn(key, new_ctx)
self.assertEqual(new_ctx[key], value)

def test_64bit_trace_id(self):
"""64 bit trace ids should be padded to 128 bit trace ids."""
trace_id_64_bit = self.serialized_trace_id[:16]
Expand Down Expand Up @@ -334,3 +361,12 @@ def test_fields(self):
inject_fields.add(call[1][1])

self.assertEqual(FORMAT.fields, inject_fields)

def test_extract_none_context(self):
"""Given no trace ID, do not modify context"""
old_ctx = None

carrier = {}
new_ctx = FORMAT.extract(carrier, old_ctx)
self.assertIsNotNone(new_ctx)
self.assertEqual(new_ctx["current-span"], trace_api.INVALID_SPAN)