From 734cc1a4b33345ca25fd0ba1747fb4a2927d592a Mon Sep 17 00:00:00 2001 From: Olivier Ceyral Date: Fri, 10 Jun 2022 12:48:56 +0200 Subject: [PATCH 1/4] Fix tracing decorator with late config --- .../src/opentelemetry/trace/__init__.py | 6 +++-- opentelemetry-api/tests/trace/test_proxy.py | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index be0d8933b7e..46be6e3c713 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -434,8 +434,10 @@ def _tracer(self) -> Tracer: def start_span(self, *args, **kwargs) -> Span: # type: ignore return self._tracer.start_span(*args, **kwargs) # type: ignore - def start_as_current_span(self, *args, **kwargs) -> Span: # type: ignore - return self._tracer.start_as_current_span(*args, **kwargs) # type: ignore + @contextmanager #type: ignore + def start_as_current_span(self, *args, **kwargs) -> Iterator[Span]: # type: ignore + with self._tracer.start_as_current_span(*args, **kwargs) as span: # type: ignore + yield span class NoOpTracer(Tracer): diff --git a/opentelemetry-api/tests/trace/test_proxy.py b/opentelemetry-api/tests/trace/test_proxy.py index b361540b9d3..637df996e34 100644 --- a/opentelemetry-api/tests/trace/test_proxy.py +++ b/opentelemetry-api/tests/trace/test_proxy.py @@ -15,6 +15,7 @@ # pylint: disable=W0212,W0222,W0221 import typing import unittest +from contextlib import contextmanager from opentelemetry import trace from opentelemetry.test.globals_test import TraceGlobalsTest @@ -35,6 +36,11 @@ class TestTracer(trace.NoOpTracer): def start_span(self, *args, **kwargs): return TestSpan(INVALID_SPAN_CONTEXT) + @contextmanager + def start_as_current_span(self, *args, **kwargs): + with trace.use_span(self.start_span(*args, **kwargs)) as span: + yield span + class TestSpan(NonRecordingSpan): pass @@ -73,3 +79,21 @@ def test_proxy_tracer(self): # creates real spans with tracer.start_span("") as span: self.assertIsInstance(span, TestSpan) + + def test_late_config(self): + # get a tracer and instrument a function as we would at the + # root of a module + tracer = trace.get_tracer("test") + + @tracer.start_as_current_span("span") + def my_function(): + return trace.get_current_span() + + # call function before configuring tracing, should return + # INVALID_SPAN from the NoOpTracer + self.assertEqual(my_function(), trace.INVALID_SPAN) + + # configure tracing + trace.set_tracer_provider(TestProvider()) + # call function again, we should now be getting a TestSpan + self.assertIsInstance(my_function(), TestSpan) From 7960d2e6d3c2ddb7c0deef41672e92106d54d6c9 Mon Sep 17 00:00:00 2001 From: Olivier Ceyral Date: Fri, 10 Jun 2022 13:12:52 +0200 Subject: [PATCH 2/4] Update Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e33019f119e..21937afafdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.12.0rc1-0.31b0...HEAD) +- Fix tracing decorator with late configuration + ([#2754](https://github.com/open-telemetry/opentelemetry-python/pull/2754)) - Fix type hints for textmap `Getter` and `Setter` ([#2657](https://github.com/open-telemetry/opentelemetry-python/pull/2657)) - Fix LogEmitterProvider.force_flush hanging randomly From 305b7bd7dd21f2a366052626076a0748fc0089c4 Mon Sep 17 00:00:00 2001 From: Olivier Ceyral Date: Tue, 28 Jun 2022 18:04:46 +0200 Subject: [PATCH 3/4] fix lint, mypy --- .../src/opentelemetry/trace/__init__.py | 2 +- opentelemetry-api/tests/trace/test_proxy.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 46be6e3c713..53bb40f0e2d 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -434,7 +434,7 @@ def _tracer(self) -> Tracer: def start_span(self, *args, **kwargs) -> Span: # type: ignore return self._tracer.start_span(*args, **kwargs) # type: ignore - @contextmanager #type: ignore + @contextmanager # type: ignore def start_as_current_span(self, *args, **kwargs) -> Iterator[Span]: # type: ignore with self._tracer.start_as_current_span(*args, **kwargs) as span: # type: ignore yield span diff --git a/opentelemetry-api/tests/trace/test_proxy.py b/opentelemetry-api/tests/trace/test_proxy.py index 637df996e34..7e53a901775 100644 --- a/opentelemetry-api/tests/trace/test_proxy.py +++ b/opentelemetry-api/tests/trace/test_proxy.py @@ -19,7 +19,7 @@ from opentelemetry import trace from opentelemetry.test.globals_test import TraceGlobalsTest -from opentelemetry.trace.span import INVALID_SPAN_CONTEXT, NonRecordingSpan +from opentelemetry.trace.span import INVALID_SPAN_CONTEXT, NonRecordingSpan, Span class TestProvider(trace.NoOpTracerProvider): @@ -37,8 +37,8 @@ def start_span(self, *args, **kwargs): return TestSpan(INVALID_SPAN_CONTEXT) @contextmanager - def start_as_current_span(self, *args, **kwargs): - with trace.use_span(self.start_span(*args, **kwargs)) as span: + def start_as_current_span(self, *args, **kwargs): # type: ignore + with trace.use_span(self.start_span(*args, **kwargs)) as span: # type: ignore yield span @@ -86,14 +86,14 @@ def test_late_config(self): tracer = trace.get_tracer("test") @tracer.start_as_current_span("span") - def my_function(): + def my_function() -> Span: return trace.get_current_span() - # call function before configuring tracing, should return - # INVALID_SPAN from the NoOpTracer + # call function before configuring tracing provider, should + # return INVALID_SPAN from the NoOpTracer self.assertEqual(my_function(), trace.INVALID_SPAN) - # configure tracing + # configure tracing provider trace.set_tracer_provider(TestProvider()) # call function again, we should now be getting a TestSpan self.assertIsInstance(my_function(), TestSpan) From 2d66d903cb0e96a3142090e41459dcaf720665a1 Mon Sep 17 00:00:00 2001 From: Olivier Ceyral Date: Tue, 28 Jun 2022 22:26:46 +0200 Subject: [PATCH 4/4] black --- opentelemetry-api/tests/trace/test_proxy.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/trace/test_proxy.py b/opentelemetry-api/tests/trace/test_proxy.py index 7e53a901775..e48a2157aec 100644 --- a/opentelemetry-api/tests/trace/test_proxy.py +++ b/opentelemetry-api/tests/trace/test_proxy.py @@ -19,7 +19,11 @@ from opentelemetry import trace from opentelemetry.test.globals_test import TraceGlobalsTest -from opentelemetry.trace.span import INVALID_SPAN_CONTEXT, NonRecordingSpan, Span +from opentelemetry.trace.span import ( + INVALID_SPAN_CONTEXT, + NonRecordingSpan, + Span, +) class TestProvider(trace.NoOpTracerProvider):