Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b25d74

Browse files
authored
Fix opentracing shim references (open-telemetry#2180)
1 parent 6b38689 commit 9b25d74

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Propagation: only warn about oversized baggage headers when headers exist
1616
([#2212](https://github.com/open-telemetry/opentelemetry-python/pull/2212))
1717

18+
- Fix parental trace relationship for opentracing `follows_from` reference
19+
([#2180](https://github.com/open-telemetry/opentelemetry-python/pull/2180))
20+
21+
1822
## [1.6.0-0.25b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.0-0.25b0) - 2021-10-13
1923

2024

@@ -51,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5155
- Add name to `BatchSpanProcessor` worker thread
5256
([#2186](https://github.com/open-telemetry/opentelemetry-python/pull/2186))
5357

58+
5459
## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26
5560

5661

shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,10 @@ def start_active_span(
592592

593593
current_span = get_current_span()
594594

595-
if child_of is None and current_span is not INVALID_SPAN_CONTEXT:
595+
if (
596+
child_of is None
597+
and current_span.get_span_context() is not INVALID_SPAN_CONTEXT
598+
):
596599
child_of = SpanShim(None, None, current_span)
597600

598601
span = self.start_span(
@@ -649,12 +652,16 @@ def start_span(
649652
if isinstance(parent, OtelSpanContext):
650653
parent = NonRecordingSpan(parent)
651654

652-
parent_span_context = set_span_in_context(parent)
653-
654-
links = []
655+
valid_links = []
655656
if references:
656657
for ref in references:
657-
links.append(Link(ref.referenced_context.unwrap()))
658+
if ref.referenced_context.unwrap() is not INVALID_SPAN_CONTEXT:
659+
valid_links.append(Link(ref.referenced_context.unwrap()))
660+
661+
if valid_links and parent is None:
662+
parent = NonRecordingSpan(valid_links[0].context)
663+
664+
parent_span_context = set_span_in_context(parent)
658665

659666
# The OpenTracing API expects time values to be `float` values which
660667
# represent the number of seconds since the epoch. OpenTelemetry
@@ -666,7 +673,7 @@ def start_span(
666673
span = self._otel_tracer.start_span(
667674
operation_name,
668675
context=parent_span_context,
669-
links=links,
676+
links=valid_links,
670677
attributes=tags,
671678
start_time=start_time_ns,
672679
)

shim/opentelemetry-opentracing-shim/tests/test_shim.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,24 @@ def test_references(self):
389389
parent.context.unwrap(),
390390
)
391391

392+
def test_follows_from_references(self):
393+
"""Test span creation using the `references` argument with a follows from relationship."""
394+
395+
with self.shim.start_span("ParentSpan") as parent:
396+
ref = opentracing.follows_from(parent.context)
397+
398+
with self.shim.start_active_span(
399+
"FollowingSpan", references=[ref]
400+
) as child:
401+
self.assertEqual(
402+
child.span.unwrap().links[0].context,
403+
parent.context.unwrap(),
404+
)
405+
self.assertEqual(
406+
child.span.unwrap().parent,
407+
parent.context.unwrap(),
408+
)
409+
392410
def test_set_operation_name(self):
393411
"""Test `set_operation_name()` method."""
394412

0 commit comments

Comments
 (0)