From b289cd9e17dd6fba9c4d1e9f4d848a0b34e54781 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 14 Jul 2020 12:02:30 -0600 Subject: [PATCH 1/9] Implement get_attribute and set_attribute Fixes #239 --- opentelemetry-api/src/opentelemetry/trace/span.py | 10 ++++++++++ .../src/opentelemetry/sdk/trace/__init__.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index d207ecf565b..c45f92a4e2c 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -37,6 +37,13 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: Sets a single Attribute with the key and value passed as arguments. """ + @abc.abstractmethod + def get_attribute(self, key: str) -> types.AttributeValue: + """Gets an Attribute. + + Gets a single Attribute with the key passed as argument. + """ + @abc.abstractmethod def add_event( self, @@ -235,6 +242,9 @@ def end(self, end_time: typing.Optional[int] = None) -> None: def set_attribute(self, key: str, value: types.AttributeValue) -> None: pass + def get_attribute(self, key: str) -> types.AttributeValue: + pass + def add_event( self, name: str, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index b76ba49ad4a..f83c0c9f775 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -553,6 +553,10 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: with self._lock: self.attributes[key] = value + def get_attribute(self, key: str) -> types.AttributeValue: + + return self.attributes[key] + @staticmethod def _filter_attribute_values(attributes: types.Attributes): if attributes: From 3bf35515a84be1e0df29360a833770a9e1183e2b Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Thu, 16 Jul 2020 18:47:56 -0600 Subject: [PATCH 2/9] Revert "Implement get_attribute and set_attribute" This reverts commit de3417252cea82f089e49bd681c35dff07463222. --- opentelemetry-api/src/opentelemetry/trace/span.py | 10 ---------- .../src/opentelemetry/sdk/trace/__init__.py | 4 ---- 2 files changed, 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index c45f92a4e2c..d207ecf565b 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -37,13 +37,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: Sets a single Attribute with the key and value passed as arguments. """ - @abc.abstractmethod - def get_attribute(self, key: str) -> types.AttributeValue: - """Gets an Attribute. - - Gets a single Attribute with the key passed as argument. - """ - @abc.abstractmethod def add_event( self, @@ -242,9 +235,6 @@ def end(self, end_time: typing.Optional[int] = None) -> None: def set_attribute(self, key: str, value: types.AttributeValue) -> None: pass - def get_attribute(self, key: str) -> types.AttributeValue: - pass - def add_event( self, name: str, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index f83c0c9f775..b76ba49ad4a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -553,10 +553,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: with self._lock: self.attributes[key] = value - def get_attribute(self, key: str) -> types.AttributeValue: - - return self.attributes[key] - @staticmethod def _filter_attribute_values(attributes: types.Attributes): if attributes: From 2cf8655c2e78a2eb0464481f590f4568297d6869 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 17 Jul 2020 15:35:22 -0600 Subject: [PATCH 3/9] Use Correlations API --- .../opentelemetry/instrumentation/opentracing_shim/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index 22c67c53ea2..9f7ed5a7b34 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -91,6 +91,7 @@ from deprecated import deprecated from opentelemetry import propagators +from opentelemetry.correlationcontext import set_correlation, get_correlation from opentelemetry.context import Context from opentelemetry.correlationcontext import get_correlation, set_correlation from opentelemetry.instrumentation.opentracing_shim import util From 3748270c07e2da72efab85723400d66d5b2509c5 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 20 Jul 2020 15:51:20 -0600 Subject: [PATCH 4/9] Fix lint --- .../opentelemetry/instrumentation/opentracing_shim/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index 9f7ed5a7b34..22c67c53ea2 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -91,7 +91,6 @@ from deprecated import deprecated from opentelemetry import propagators -from opentelemetry.correlationcontext import set_correlation, get_correlation from opentelemetry.context import Context from opentelemetry.correlationcontext import get_correlation, set_correlation from opentelemetry.instrumentation.opentracing_shim import util From da38ef0012ce49098924729ddba5de0753490416 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 17 Jul 2020 01:35:21 -0600 Subject: [PATCH 5/9] Store ScopeShim in context This uses the OpenTelemetry context management mechanism to store a ScopeShim object in order to make active return the same object as the one returned by start_active_span. WIP: there is one failing test case, apparently the context does not get cleared before running every test case. This seems related to the order in which test cases are being run. --- .../opentracing_shim/__init__.py | 22 ++++++------ .../tests/test_shim.py | 34 +++++++++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index 22c67c53ea2..a1b5203b006 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -93,8 +93,9 @@ from opentelemetry import propagators from opentelemetry.context import Context from opentelemetry.correlationcontext import get_correlation, set_correlation -from opentelemetry.instrumentation.opentracing_shim import util -from opentelemetry.instrumentation.opentracing_shim.version import __version__ +from opentelemetry.context import attach, detach, get_value, set_value +from opentelemetry.ext.opentracing_shim import util +from opentelemetry.ext.opentracing_shim.version import __version__ from opentelemetry.trace import ( INVALID_SPAN_CONTEXT, DefaultSpan, @@ -327,6 +328,7 @@ class ScopeShim(opentracing.Scope): def __init__(self, manager, span, span_cm=None): super().__init__(manager, span) self._span_cm = span_cm + self._token = attach(set_value("scope_shim", self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -392,6 +394,8 @@ def close(self): else: self._span.unwrap().end() + detach(self._token) + class ScopeManagerShim(opentracing.ScopeManager): """Implements :class:`opentracing.ScopeManager` by setting and getting the @@ -460,14 +464,12 @@ def active(self): if span.get_context() == INVALID_SPAN_CONTEXT: return None - span_context = SpanContextShim(span.get_context()) - wrapped_span = SpanShim(self._tracer, span_context, span) - return ScopeShim(self, span=wrapped_span) - # TODO: The returned `ScopeShim` instance here always ends the - # corresponding span, regardless of the `finish_on_close` value used - # when activating the span. This is because here we return a *new* - # `ScopeShim` rather than returning a saved instance of `ScopeShim`. - # https://github.com/open-telemetry/opentelemetry-python/pull/211/files#r335398792 + try: + return get_value("scope_shim") + except KeyError: + span_context = SpanContextShim(span.get_context()) + wrapped_span = SpanShim(self._tracer, span_context, span) + return ScopeShim(self, span=wrapped_span) @property def tracer(self): diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py index 8b46a4bcb3b..6d68945a10e 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py @@ -63,7 +63,7 @@ def test_shim_type(self): def test_start_active_span(self): """Test span creation and activation using `start_active_span()`.""" - with self.shim.start_active_span("TestSpan") as scope: + with self.shim.start_active_span("TestSpxn") as scope: # Verify correct type of Scope and Span objects. self.assertIsInstance(scope, opentracing.Scope) self.assertIsInstance(scope.span, opentracing.Span) @@ -91,7 +91,7 @@ def test_start_active_span(self): def test_start_span(self): """Test span creation using `start_span()`.""" - with self.shim.start_span("TestSpan") as span: + with self.shim.start_span("TestSpzn") as span: # Verify correct type of Span object. self.assertIsInstance(span, opentracing.Span) @@ -107,7 +107,7 @@ def test_start_span(self): def test_start_span_no_contextmanager(self): """Test `start_span()` without a `with` statement.""" - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSpTn") # Verify span is started. self.assertIsNotNone(span.unwrap().start_time) @@ -120,7 +120,7 @@ def test_start_span_no_contextmanager(self): def test_explicit_span_finish(self): """Test `finish()` method on `Span` objects.""" - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSpcn") # Verify span hasn't ended. self.assertIsNone(span.unwrap().end_time) @@ -134,7 +134,9 @@ def test_explicit_start_time(self): """Test `start_time` argument.""" now = time.time() - with self.shim.start_active_span("TestSpan", start_time=now) as scope: + from ipdb import set_trace + set_trace() + with self.shim.start_active_span("TestSp5n", start_time=now) as scope: result = util.time_seconds_from_ns(scope.span.unwrap().start_time) # Tolerate inaccuracies of less than a microsecond. See Note: # https://open-telemetry.github.io/opentelemetry-python/opentelemetry.instrumentation.opentracing_shim.html @@ -145,7 +147,7 @@ def test_explicit_start_time(self): def test_explicit_end_time(self): """Test `end_time` argument of `finish()` method.""" - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSpmn") now = time.time() span.finish(now) @@ -159,7 +161,7 @@ def test_explicit_end_time(self): def test_explicit_span_activation(self): """Test manual activation and deactivation of a span.""" - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSp99n") # Verify no span is currently active. self.assertIsNone(self.shim.active_span) @@ -180,7 +182,7 @@ def test_start_active_span_finish_on_close(self): """Test `finish_on_close` argument of `start_active_span()`.""" with self.shim.start_active_span( - "TestSpan", finish_on_close=True + "TestSp88n", finish_on_close=True ) as scope: # Verify span hasn't ended. self.assertIsNone(scope.span.unwrap().end_time) @@ -189,7 +191,7 @@ def test_start_active_span_finish_on_close(self): self.assertIsNotNone(scope.span.unwrap().end_time) with self.shim.start_active_span( - "TestSpan", finish_on_close=False + "TestSp23n", finish_on_close=False ) as scope: # Verify span hasn't ended. self.assertIsNone(scope.span.unwrap().end_time) @@ -202,7 +204,7 @@ def test_start_active_span_finish_on_close(self): def test_activate_finish_on_close(self): """Test `finish_on_close` argument of `activate()`.""" - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSp533n") with self.shim.scope_manager.activate( span, finish_on_close=True @@ -216,7 +218,7 @@ def test_activate_finish_on_close(self): # Verify span has ended. self.assertIsNotNone(span.unwrap().end_time) - span = self.shim.start_span("TestSpan") + span = self.shim.start_span("TestSp009n") with self.shim.scope_manager.activate( span, finish_on_close=False @@ -402,13 +404,13 @@ def test_tags(self): def test_span_tracer(self): """Test the `tracer` property on `Span` objects.""" - with self.shim.start_active_span("TestSpan") as scope: + with self.shim.start_active_span("TestSp423n") as scope: self.assertEqual(scope.span.tracer, self.shim) def test_log_kv(self): """Test the `log_kv()` method on `Span` objects.""" - with self.shim.start_span("TestSpan") as span: + with self.shim.start_span("TestSp44444n") as span: span.log_kv({"foo": "bar"}) self.assertEqual(span.unwrap().events[0].attributes["foo"], "bar") # Verify timestamp was generated automatically. @@ -430,7 +432,7 @@ def test_log_kv(self): def test_log(self): """Test the deprecated `log` method on `Span` objects.""" - with self.shim.start_span("TestSpan") as span: + with self.shim.start_span("TestSp3232n") as span: with self.assertWarns(DeprecationWarning): span.log(event="foo", payload="bar") @@ -441,7 +443,7 @@ def test_log(self): def test_log_event(self): """Test the deprecated `log_event` method on `Span` objects.""" - with self.shim.start_span("TestSpan") as span: + with self.shim.start_span("TestSpun") as span: with self.assertWarns(DeprecationWarning): span.log_event("foo", "bar") @@ -532,6 +534,8 @@ def test_extract_empty_context_returns_invalid_context(self): try: carrier = {} + from ipdb import set_trace + set_trace() ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier) self.assertEqual(ctx.unwrap(), trace.INVALID_SPAN_CONTEXT) finally: From 98308889809a7d53970a954e41b1ee9d00cd955d Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 17 Jul 2020 10:22:26 -0600 Subject: [PATCH 6/9] Add remaining fixes Fixes #242 --- .../opentracing_shim/__init__.py | 9 ++-- .../tests/test_shim.py | 54 ++++++++++++------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index a1b5203b006..bbcd1b159b3 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -91,9 +91,8 @@ from deprecated import deprecated from opentelemetry import propagators -from opentelemetry.context import Context -from opentelemetry.correlationcontext import get_correlation, set_correlation -from opentelemetry.context import attach, detach, get_value, set_value +from opentelemetry.correlationcontext import set_correlation, get_correlation +from opentelemetry.context import Context, attach, get_value, set_value from opentelemetry.ext.opentracing_shim import util from opentelemetry.ext.opentracing_shim.version import __version__ from opentelemetry.trace import ( @@ -328,7 +327,7 @@ class ScopeShim(opentracing.Scope): def __init__(self, manager, span, span_cm=None): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value("scope_shim", self)) + attach(set_value("scope_shim", self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -394,8 +393,6 @@ def close(self): else: self._span.unwrap().end() - detach(self._token) - class ScopeManagerShim(opentracing.ScopeManager): """Implements :class:`opentracing.ScopeManager` by setting and getting the diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py index 6d68945a10e..445ce6ce161 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py @@ -63,7 +63,7 @@ def test_shim_type(self): def test_start_active_span(self): """Test span creation and activation using `start_active_span()`.""" - with self.shim.start_active_span("TestSpxn") as scope: + with self.shim.start_active_span("TestSpan0") as scope: # Verify correct type of Scope and Span objects. self.assertIsInstance(scope, opentracing.Scope) self.assertIsInstance(scope.span, opentracing.Span) @@ -91,7 +91,7 @@ def test_start_active_span(self): def test_start_span(self): """Test span creation using `start_span()`.""" - with self.shim.start_span("TestSpzn") as span: + with self.shim.start_span("TestSpan1") as span: # Verify correct type of Span object. self.assertIsInstance(span, opentracing.Span) @@ -107,7 +107,7 @@ def test_start_span(self): def test_start_span_no_contextmanager(self): """Test `start_span()` without a `with` statement.""" - span = self.shim.start_span("TestSpTn") + span = self.shim.start_span("TestSpan2") # Verify span is started. self.assertIsNotNone(span.unwrap().start_time) @@ -120,7 +120,7 @@ def test_start_span_no_contextmanager(self): def test_explicit_span_finish(self): """Test `finish()` method on `Span` objects.""" - span = self.shim.start_span("TestSpcn") + span = self.shim.start_span("TestSpan3") # Verify span hasn't ended. self.assertIsNone(span.unwrap().end_time) @@ -134,9 +134,7 @@ def test_explicit_start_time(self): """Test `start_time` argument.""" now = time.time() - from ipdb import set_trace - set_trace() - with self.shim.start_active_span("TestSp5n", start_time=now) as scope: + with self.shim.start_active_span("TestSpan4", start_time=now) as scope: result = util.time_seconds_from_ns(scope.span.unwrap().start_time) # Tolerate inaccuracies of less than a microsecond. See Note: # https://open-telemetry.github.io/opentelemetry-python/opentelemetry.instrumentation.opentracing_shim.html @@ -147,7 +145,7 @@ def test_explicit_start_time(self): def test_explicit_end_time(self): """Test `end_time` argument of `finish()` method.""" - span = self.shim.start_span("TestSpmn") + span = self.shim.start_span("TestSpan5") now = time.time() span.finish(now) @@ -161,7 +159,7 @@ def test_explicit_end_time(self): def test_explicit_span_activation(self): """Test manual activation and deactivation of a span.""" - span = self.shim.start_span("TestSp99n") + span = self.shim.start_span("TestSpan6") # Verify no span is currently active. self.assertIsNone(self.shim.active_span) @@ -182,7 +180,7 @@ def test_start_active_span_finish_on_close(self): """Test `finish_on_close` argument of `start_active_span()`.""" with self.shim.start_active_span( - "TestSp88n", finish_on_close=True + "TestSpan7", finish_on_close=True ) as scope: # Verify span hasn't ended. self.assertIsNone(scope.span.unwrap().end_time) @@ -191,7 +189,7 @@ def test_start_active_span_finish_on_close(self): self.assertIsNotNone(scope.span.unwrap().end_time) with self.shim.start_active_span( - "TestSp23n", finish_on_close=False + "TestSpan8", finish_on_close=False ) as scope: # Verify span hasn't ended. self.assertIsNone(scope.span.unwrap().end_time) @@ -204,7 +202,7 @@ def test_start_active_span_finish_on_close(self): def test_activate_finish_on_close(self): """Test `finish_on_close` argument of `activate()`.""" - span = self.shim.start_span("TestSp533n") + span = self.shim.start_span("TestSpan9") with self.shim.scope_manager.activate( span, finish_on_close=True @@ -218,7 +216,7 @@ def test_activate_finish_on_close(self): # Verify span has ended. self.assertIsNotNone(span.unwrap().end_time) - span = self.shim.start_span("TestSp009n") + span = self.shim.start_span("TestSpan10") with self.shim.scope_manager.activate( span, finish_on_close=False @@ -404,13 +402,13 @@ def test_tags(self): def test_span_tracer(self): """Test the `tracer` property on `Span` objects.""" - with self.shim.start_active_span("TestSp423n") as scope: + with self.shim.start_active_span("TestSpan11") as scope: self.assertEqual(scope.span.tracer, self.shim) def test_log_kv(self): """Test the `log_kv()` method on `Span` objects.""" - with self.shim.start_span("TestSp44444n") as span: + with self.shim.start_span("TestSpan12") as span: span.log_kv({"foo": "bar"}) self.assertEqual(span.unwrap().events[0].attributes["foo"], "bar") # Verify timestamp was generated automatically. @@ -432,7 +430,7 @@ def test_log_kv(self): def test_log(self): """Test the deprecated `log` method on `Span` objects.""" - with self.shim.start_span("TestSp3232n") as span: + with self.shim.start_span("TestSpan13") as span: with self.assertWarns(DeprecationWarning): span.log(event="foo", payload="bar") @@ -443,7 +441,7 @@ def test_log(self): def test_log_event(self): """Test the deprecated `log_event` method on `Span` objects.""" - with self.shim.start_span("TestSpun") as span: + with self.shim.start_span("TestSpan14") as span: with self.assertWarns(DeprecationWarning): span.log_event("foo", "bar") @@ -534,8 +532,6 @@ def test_extract_empty_context_returns_invalid_context(self): try: carrier = {} - from ipdb import set_trace - set_trace() ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier) self.assertEqual(ctx.unwrap(), trace.INVALID_SPAN_CONTEXT) finally: @@ -561,6 +557,7 @@ def test_extract_binary(self): self.shim.extract(opentracing.Format.BINARY, bytearray()) def test_baggage(self): + """Test SpanShim baggage being set and being immutable""" span_context_shim = SpanContextShim( trace.SpanContext(1234, 5678, is_remote=False) @@ -576,3 +573,22 @@ def test_baggage(self): span_shim.set_baggage_item(1, 2) self.assertTrue(span_shim.get_baggage_item(1), 2) + + def test_active(self): + """Test that the active property and start_active_span return the same + object""" + + # Verify no span is currently active. + self.assertIsNone(self.shim.active_span) + + with self.shim.start_active_span("TestSpan15") as scope: + # Verify span is active. + self.assertEqual( + self.shim.active_span.context.unwrap(), + scope.span.context.unwrap(), + ) + + self.assertIs(self.shim.scope_manager.active, scope) + + # Verify no span is active. + self.assertIsNone(self.shim.active_span) From 39316e47df017528002c19d2a042131cd255de8d Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 17 Jul 2020 20:54:37 -0600 Subject: [PATCH 7/9] Fix lint --- .../opentelemetry/instrumentation/opentracing_shim/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index bbcd1b159b3..aca64343bef 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -91,8 +91,8 @@ from deprecated import deprecated from opentelemetry import propagators -from opentelemetry.correlationcontext import set_correlation, get_correlation from opentelemetry.context import Context, attach, get_value, set_value +from opentelemetry.correlationcontext import get_correlation, set_correlation from opentelemetry.ext.opentracing_shim import util from opentelemetry.ext.opentracing_shim.version import __version__ from opentelemetry.trace import ( From ca2929849ed3ccab45bad7119988abd260024a0a Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 31 Jul 2020 14:34:20 -0600 Subject: [PATCH 8/9] Calling detach at the right place --- .../instrumentation/opentracing_shim/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index aca64343bef..9b9e549a39d 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -91,7 +91,7 @@ from deprecated import deprecated from opentelemetry import propagators -from opentelemetry.context import Context, attach, get_value, set_value +from opentelemetry.context import Context, attach, detach, get_value, set_value from opentelemetry.correlationcontext import get_correlation, set_correlation from opentelemetry.ext.opentracing_shim import util from opentelemetry.ext.opentracing_shim.version import __version__ @@ -327,7 +327,7 @@ class ScopeShim(opentracing.Scope): def __init__(self, manager, span, span_cm=None): super().__init__(manager, span) self._span_cm = span_cm - attach(set_value("scope_shim", self)) + self._token = attach(set_value("scope_shim", self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -383,6 +383,8 @@ def close(self): *finish_on_close* when activating the span. """ + detach(self._token) + if self._span_cm is not None: # We don't have error information to pass to `__exit__()` so we # pass `None` in all arguments. If the OpenTelemetry tracer From 1e36e25bdfe4a03093724bf133712e73bdf4b7d8 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 5 Aug 2020 11:59:41 -0600 Subject: [PATCH 9/9] Use proper naming --- .../instrumentation/opentracing_shim/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index 9b9e549a39d..6c76444e6ee 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -93,8 +93,8 @@ from opentelemetry import propagators from opentelemetry.context import Context, attach, detach, get_value, set_value from opentelemetry.correlationcontext import get_correlation, set_correlation -from opentelemetry.ext.opentracing_shim import util -from opentelemetry.ext.opentracing_shim.version import __version__ +from opentelemetry.instrumentation.opentracing_shim import util +from opentelemetry.instrumentation.opentracing_shim.version import __version__ from opentelemetry.trace import ( INVALID_SPAN_CONTEXT, DefaultSpan,