diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc30d5886a..6f387f22f9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2705](https://github.com/open-telemetry/opentelemetry-python/pull/2705)) - Add entrypoint for metrics exporter ([#2748](https://github.com/open-telemetry/opentelemetry-python/pull/2748)) +- Fix Jaeger propagator usage with NonRecordingSpan + ([#2762](https://github.com/open-telemetry/opentelemetry-python/pull/2762)) ## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17 diff --git a/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py b/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py index 9589f97619a..201d8bf3d3d 100644 --- a/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py +++ b/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py @@ -81,7 +81,10 @@ def inject( if span_context == trace.INVALID_SPAN_CONTEXT: return - span_parent_id = span.parent.span_id if span.parent else 0 + # Non-recording spans do not have a parent + span_parent_id = ( + span.parent.span_id if span.is_recording() and span.parent else 0 + ) trace_flags = span_context.trace_flags if trace_flags.sampled: trace_flags |= self.DEBUG_FLAG diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index f01e0e53da8..81187389a16 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -230,3 +230,13 @@ def test_extract_invalid_uber_trace_id_header_to_implicit_ctx(self): ctx = FORMAT.extract(carrier) self.assertDictEqual(Context(), ctx) + + def test_non_recording_span_does_not_crash(self): + """Make sure propagator does not crash when working with NonRecordingSpan""" + mock_setter = Mock() + span = trace_api.NonRecordingSpan(trace_api.SpanContext(1, 1, True)) + with trace_api.use_span(span, end_on_exit=True): + try: + FORMAT.inject({}, setter=mock_setter) + except Exception as exc: # pylint: disable=broad-except + self.fail(f"Injecting failed for NonRecordingSpan with {exc}")