From 9a057ed739ea94ec2b1324f0297db0b480b2a911 Mon Sep 17 00:00:00 2001 From: "Roberts, Benjamin" Date: Tue, 5 Oct 2021 14:10:24 +0100 Subject: [PATCH 1/5] fix the parental relationship for follows_from reference --- CHANGELOG.md | 2 ++ .../opentelemetry/shim/opentracing_shim/__init__.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 835601d58ce..951b93faad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2145](https://github.com/open-telemetry/opentelemetry-python/pull/2145)) - Add `schema_url` to `TracerProvider.get_tracer` ([#2154](https://github.com/open-telemetry/opentelemetry-python/pull/2154)) +- Fix parental trace relationship for opentracing `follows_from` reference + () ## [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 5777bd0edc9..becd7acb1b4 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,7 @@ 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 +649,18 @@ 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(ref) + + if valid_links and parent is None: + parent = NonRecordingSpan(valid_links[0].referenced_context.unwrap()) + + 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 From 68c689c96e6a2f460ec365c0b3cb87581e3cc1d0 Mon Sep 17 00:00:00 2001 From: "Roberts, Benjamin" Date: Tue, 5 Oct 2021 14:31:58 +0100 Subject: [PATCH 2/5] add a test asserting that follows from is used as trace parent --- .../tests/test_shim.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/shim/opentelemetry-opentracing-shim/tests/test_shim.py b/shim/opentelemetry-opentracing-shim/tests/test_shim.py index e27f7797340..99394ad2169 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.""" From 3d8aa24abd06f850d3219445d912a5fed7ed20e1 Mon Sep 17 00:00:00 2001 From: "Roberts, Benjamin" Date: Tue, 5 Oct 2021 15:09:41 +0100 Subject: [PATCH 3/5] update changelog with PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 951b93faad5..57d84dde77a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `schema_url` to `TracerProvider.get_tracer` ([#2154](https://github.com/open-telemetry/opentelemetry-python/pull/2154)) - Fix parental trace relationship for opentracing `follows_from` reference - () + ([#2180](https://github.com/open-telemetry/opentelemetry-python/pull/2180)) ## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26 From 9807595950c2781f608327db10b86a6638e49534 Mon Sep 17 00:00:00 2001 From: "Roberts, Benjamin" Date: Tue, 5 Oct 2021 17:12:25 +0100 Subject: [PATCH 4/5] blacken code --- .../src/opentelemetry/shim/opentracing_shim/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 becd7acb1b4..c318f54a91f 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.get_span_context() 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( @@ -658,7 +661,9 @@ def start_span( valid_links.append(ref) if valid_links and parent is None: - parent = NonRecordingSpan(valid_links[0].referenced_context.unwrap()) + parent = NonRecordingSpan( + valid_links[0].referenced_context.unwrap() + ) parent_span_context = set_span_in_context(parent) From dab2542d0e23f76ac3601d698d77798d27b79752 Mon Sep 17 00:00:00 2001 From: "Roberts, Benjamin" Date: Thu, 14 Oct 2021 16:15:18 +0100 Subject: [PATCH 5/5] Use valid links when starting span --- .../opentelemetry/shim/opentracing_shim/__init__.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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 c318f54a91f..9bc9ee89f1f 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 @@ -652,18 +652,14 @@ def start_span( if isinstance(parent, OtelSpanContext): parent = NonRecordingSpan(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(ref) + valid_links.append(Link(ref.referenced_context.unwrap())) if valid_links and parent is None: - parent = NonRecordingSpan( - valid_links[0].referenced_context.unwrap() - ) + parent = NonRecordingSpan(valid_links[0].context) parent_span_context = set_span_in_context(parent) @@ -677,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, )