Skip to content

Commit 03c6aa7

Browse files
committed
Add remaining fixes
Fixes open-telemetry#242
1 parent 82685c2 commit 03c6aa7

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@
9191
from deprecated import deprecated
9292

9393
from opentelemetry import propagators
94-
from opentelemetry.context import Context
95-
from opentelemetry.context import attach, detach, get_value, set_value
94+
from opentelemetry.context import Context, attach, get_value, set_value
9695
from opentelemetry.ext.opentracing_shim import util
9796
from opentelemetry.ext.opentracing_shim.version import __version__
9897
from opentelemetry.trace import (
@@ -328,7 +327,7 @@ class ScopeShim(opentracing.Scope):
328327
def __init__(self, manager, span, span_cm=None):
329328
super().__init__(manager, span)
330329
self._span_cm = span_cm
331-
self._token = attach(set_value("scope_shim", self))
330+
attach(set_value("scope_shim", self))
332331

333332
# TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We
334333
# need to get rid of `manager.tracer` for this.
@@ -394,8 +393,6 @@ def close(self):
394393
else:
395394
self._span.unwrap().end()
396395

397-
detach(self._token)
398-
399396

400397
class ScopeManagerShim(opentracing.ScopeManager):
401398
"""Implements :class:`opentracing.ScopeManager` by setting and getting the

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

+35-19
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_shim_type(self):
6363
def test_start_active_span(self):
6464
"""Test span creation and activation using `start_active_span()`."""
6565

66-
with self.shim.start_active_span("TestSpxn") as scope:
66+
with self.shim.start_active_span("TestSpan0") as scope:
6767
# Verify correct type of Scope and Span objects.
6868
self.assertIsInstance(scope, opentracing.Scope)
6969
self.assertIsInstance(scope.span, opentracing.Span)
@@ -91,7 +91,7 @@ def test_start_active_span(self):
9191
def test_start_span(self):
9292
"""Test span creation using `start_span()`."""
9393

94-
with self.shim.start_span("TestSpzn") as span:
94+
with self.shim.start_span("TestSpan1") as span:
9595
# Verify correct type of Span object.
9696
self.assertIsInstance(span, opentracing.Span)
9797

@@ -107,7 +107,7 @@ def test_start_span(self):
107107
def test_start_span_no_contextmanager(self):
108108
"""Test `start_span()` without a `with` statement."""
109109

110-
span = self.shim.start_span("TestSpTn")
110+
span = self.shim.start_span("TestSpan2")
111111

112112
# Verify span is started.
113113
self.assertIsNotNone(span.unwrap().start_time)
@@ -120,7 +120,7 @@ def test_start_span_no_contextmanager(self):
120120
def test_explicit_span_finish(self):
121121
"""Test `finish()` method on `Span` objects."""
122122

123-
span = self.shim.start_span("TestSpcn")
123+
span = self.shim.start_span("TestSpan3")
124124

125125
# Verify span hasn't ended.
126126
self.assertIsNone(span.unwrap().end_time)
@@ -134,9 +134,7 @@ def test_explicit_start_time(self):
134134
"""Test `start_time` argument."""
135135

136136
now = time.time()
137-
from ipdb import set_trace
138-
set_trace()
139-
with self.shim.start_active_span("TestSp5n", start_time=now) as scope:
137+
with self.shim.start_active_span("TestSpan4", start_time=now) as scope:
140138
result = util.time_seconds_from_ns(scope.span.unwrap().start_time)
141139
# Tolerate inaccuracies of less than a microsecond. See Note:
142140
# https://open-telemetry.github.io/opentelemetry-python/opentelemetry.ext.opentracing_shim.html
@@ -147,7 +145,7 @@ def test_explicit_start_time(self):
147145
def test_explicit_end_time(self):
148146
"""Test `end_time` argument of `finish()` method."""
149147

150-
span = self.shim.start_span("TestSpmn")
148+
span = self.shim.start_span("TestSpan5")
151149
now = time.time()
152150
span.finish(now)
153151

@@ -161,7 +159,7 @@ def test_explicit_end_time(self):
161159
def test_explicit_span_activation(self):
162160
"""Test manual activation and deactivation of a span."""
163161

164-
span = self.shim.start_span("TestSp99n")
162+
span = self.shim.start_span("TestSpan6")
165163

166164
# Verify no span is currently active.
167165
self.assertIsNone(self.shim.active_span)
@@ -182,7 +180,7 @@ def test_start_active_span_finish_on_close(self):
182180
"""Test `finish_on_close` argument of `start_active_span()`."""
183181

184182
with self.shim.start_active_span(
185-
"TestSp88n", finish_on_close=True
183+
"TestSpan7", finish_on_close=True
186184
) as scope:
187185
# Verify span hasn't ended.
188186
self.assertIsNone(scope.span.unwrap().end_time)
@@ -191,7 +189,7 @@ def test_start_active_span_finish_on_close(self):
191189
self.assertIsNotNone(scope.span.unwrap().end_time)
192190

193191
with self.shim.start_active_span(
194-
"TestSp23n", finish_on_close=False
192+
"TestSpan8", finish_on_close=False
195193
) as scope:
196194
# Verify span hasn't ended.
197195
self.assertIsNone(scope.span.unwrap().end_time)
@@ -204,7 +202,7 @@ def test_start_active_span_finish_on_close(self):
204202
def test_activate_finish_on_close(self):
205203
"""Test `finish_on_close` argument of `activate()`."""
206204

207-
span = self.shim.start_span("TestSp533n")
205+
span = self.shim.start_span("TestSpan9")
208206

209207
with self.shim.scope_manager.activate(
210208
span, finish_on_close=True
@@ -218,7 +216,7 @@ def test_activate_finish_on_close(self):
218216
# Verify span has ended.
219217
self.assertIsNotNone(span.unwrap().end_time)
220218

221-
span = self.shim.start_span("TestSp009n")
219+
span = self.shim.start_span("TestSpan10")
222220

223221
with self.shim.scope_manager.activate(
224222
span, finish_on_close=False
@@ -404,13 +402,13 @@ def test_tags(self):
404402
def test_span_tracer(self):
405403
"""Test the `tracer` property on `Span` objects."""
406404

407-
with self.shim.start_active_span("TestSp423n") as scope:
405+
with self.shim.start_active_span("TestSpan11") as scope:
408406
self.assertEqual(scope.span.tracer, self.shim)
409407

410408
def test_log_kv(self):
411409
"""Test the `log_kv()` method on `Span` objects."""
412410

413-
with self.shim.start_span("TestSp44444n") as span:
411+
with self.shim.start_span("TestSpan12") as span:
414412
span.log_kv({"foo": "bar"})
415413
self.assertEqual(span.unwrap().events[0].attributes["foo"], "bar")
416414
# Verify timestamp was generated automatically.
@@ -432,7 +430,7 @@ def test_log_kv(self):
432430
def test_log(self):
433431
"""Test the deprecated `log` method on `Span` objects."""
434432

435-
with self.shim.start_span("TestSp3232n") as span:
433+
with self.shim.start_span("TestSpan13") as span:
436434
with self.assertWarns(DeprecationWarning):
437435
span.log(event="foo", payload="bar")
438436

@@ -443,7 +441,7 @@ def test_log(self):
443441
def test_log_event(self):
444442
"""Test the deprecated `log_event` method on `Span` objects."""
445443

446-
with self.shim.start_span("TestSpun") as span:
444+
with self.shim.start_span("TestSpan14") as span:
447445
with self.assertWarns(DeprecationWarning):
448446
span.log_event("foo", "bar")
449447

@@ -534,8 +532,6 @@ def test_extract_empty_context_returns_invalid_context(self):
534532
try:
535533
carrier = {}
536534

537-
from ipdb import set_trace
538-
set_trace()
539535
ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier)
540536
self.assertEqual(ctx.unwrap(), trace.INVALID_SPAN_CONTEXT)
541537
finally:
@@ -561,6 +557,7 @@ def test_extract_binary(self):
561557
self.shim.extract(opentracing.Format.BINARY, bytearray())
562558

563559
def test_baggage(self):
560+
"""Test SpanShim baggage being set and being immutable"""
564561

565562
span_context_shim = SpanContextShim(
566563
trace.SpanContext(1234, 5678, is_remote=False)
@@ -579,3 +576,22 @@ def test_baggage(self):
579576
span_shim.set_baggage_item(1, 2)
580577

581578
self.assertTrue(span_shim.get_baggage_item(1), 2)
579+
580+
def test_active(self):
581+
"""Test that the active property and start_active_span return the same
582+
object"""
583+
584+
# Verify no span is currently active.
585+
self.assertIsNone(self.shim.active_span)
586+
587+
with self.shim.start_active_span("TestSpan15") as scope:
588+
# Verify span is active.
589+
self.assertEqual(
590+
self.shim.active_span.context.unwrap(),
591+
scope.span.context.unwrap(),
592+
)
593+
594+
self.assertIs(self.shim.scope_manager.active, scope)
595+
596+
# Verify no span is active.
597+
self.assertIsNone(self.shim.active_span)

0 commit comments

Comments
 (0)