From 4541883d8e4c099a64d129b372f67ca08debb605 Mon Sep 17 00:00:00 2001 From: Brian Saville Date: Tue, 14 Jun 2022 15:01:07 -0600 Subject: [PATCH 1/3] Fix #2348, add handling for non recording span in jaeger exporter --- CHANGELOG.md | 2 ++ .../src/opentelemetry/propagators/jaeger/__init__.py | 3 ++- .../tests/test_jaeger_propagator.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) 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..4d7b3cb49e8 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,8 @@ 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..85f5b5dfbbe 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -230,3 +230,15 @@ 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""" + tracer = trace.TracerProvider().get_tracer("sdk_tracer_provider") + 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: + self.fail(f'Injecting failed for NonRecordingSpan with {exc}') + From 0224a5c6cec54d7ed3d135a6bc040b7ddca75fb6 Mon Sep 17 00:00:00 2001 From: Brian Saville Date: Tue, 14 Jun 2022 20:31:55 -0600 Subject: [PATCH 2/3] Fix pylint errors --- .../tests/test_jaeger_propagator.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index 85f5b5dfbbe..5493c3a21d3 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -233,12 +233,10 @@ def test_extract_invalid_uber_trace_id_header_to_implicit_ctx(self): def test_non_recording_span_does_not_crash(self): """Make sure propagator does not crash when working with NonRecordingSpan""" - tracer = trace.TracerProvider().get_tracer("sdk_tracer_provider") 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: + except Exception as exc: # pylint: disable=broad-except self.fail(f'Injecting failed for NonRecordingSpan with {exc}') - From 08a3d17796bbe6313d7f29d7e7d4ca6168bdc9d8 Mon Sep 17 00:00:00 2001 From: Brian Saville Date: Wed, 15 Jun 2022 15:53:51 -0600 Subject: [PATCH 3/3] Changes for black --- .../src/opentelemetry/propagators/jaeger/__init__.py | 4 +++- .../tests/test_jaeger_propagator.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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 4d7b3cb49e8..201d8bf3d3d 100644 --- a/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py +++ b/propagator/opentelemetry-propagator-jaeger/src/opentelemetry/propagators/jaeger/__init__.py @@ -82,7 +82,9 @@ def inject( return # Non-recording spans do not have a parent - span_parent_id = span.parent.span_id if span.is_recording() and span.parent else 0 + 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 5493c3a21d3..81187389a16 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -239,4 +239,4 @@ def test_non_recording_span_does_not_crash(self): try: FORMAT.inject({}, setter=mock_setter) except Exception as exc: # pylint: disable=broad-except - self.fail(f'Injecting failed for NonRecordingSpan with {exc}') + self.fail(f"Injecting failed for NonRecordingSpan with {exc}")