From 9b25d7422432d4e14a164440a46d83b8f37afc13 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Tue, 19 Oct 2021 18:48:40 +0100 Subject: [PATCH] Fix opentracing shim references (#2180) --- CHANGELOG.md | 5 +++++ .../shim/opentracing_shim/__init__.py | 19 +++++++++++++------ .../tests/test_shim.py | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5541542e0..e51bee4b57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Propagation: only warn about oversized baggage headers when headers exist ([#2212](https://github.com/open-telemetry/opentelemetry-python/pull/2212)) +- Fix parental trace relationship for opentracing `follows_from` reference + ([#2180](https://github.com/open-telemetry/opentelemetry-python/pull/2180)) + + ## [1.6.0-0.25b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.0-0.25b0) - 2021-10-13 @@ -51,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add name to `BatchSpanProcessor` worker thread ([#2186](https://github.com/open-telemetry/opentelemetry-python/pull/2186)) + ## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26 diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 5777bd0edc..9bc9ee89f1 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -592,7 +592,10 @@ def start_active_span( current_span = get_current_span() - if child_of is None and current_span is not INVALID_SPAN_CONTEXT: + if ( + child_of is None + and current_span.get_span_context() is not INVALID_SPAN_CONTEXT + ): child_of = SpanShim(None, None, current_span) span = self.start_span( @@ -649,12 +652,16 @@ def start_span( if isinstance(parent, OtelSpanContext): parent = NonRecordingSpan(parent) - parent_span_context = set_span_in_context(parent) - - links = [] + valid_links = [] if references: for ref in references: - links.append(Link(ref.referenced_context.unwrap())) + if ref.referenced_context.unwrap() is not INVALID_SPAN_CONTEXT: + valid_links.append(Link(ref.referenced_context.unwrap())) + + if valid_links and parent is None: + parent = NonRecordingSpan(valid_links[0].context) + + parent_span_context = set_span_in_context(parent) # The OpenTracing API expects time values to be `float` values which # represent the number of seconds since the epoch. OpenTelemetry @@ -666,7 +673,7 @@ def start_span( span = self._otel_tracer.start_span( operation_name, context=parent_span_context, - links=links, + links=valid_links, attributes=tags, start_time=start_time_ns, ) diff --git a/shim/opentelemetry-opentracing-shim/tests/test_shim.py b/shim/opentelemetry-opentracing-shim/tests/test_shim.py index e27f779734..99394ad216 100644 --- a/shim/opentelemetry-opentracing-shim/tests/test_shim.py +++ b/shim/opentelemetry-opentracing-shim/tests/test_shim.py @@ -389,6 +389,24 @@ def test_references(self): parent.context.unwrap(), ) + def test_follows_from_references(self): + """Test span creation using the `references` argument with a follows from relationship.""" + + with self.shim.start_span("ParentSpan") as parent: + ref = opentracing.follows_from(parent.context) + + with self.shim.start_active_span( + "FollowingSpan", references=[ref] + ) as child: + self.assertEqual( + child.span.unwrap().links[0].context, + parent.context.unwrap(), + ) + self.assertEqual( + child.span.unwrap().parent, + parent.context.unwrap(), + ) + def test_set_operation_name(self): """Test `set_operation_name()` method."""