Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tracing decorator with late configuration #2754

Merged
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ 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.0rc2-0.32b0...HEAD)


- Fix tracing decorator with late configuration
([#2754](https://github.com/open-telemetry/opentelemetry-python/pull/2754))


## [1.12.0rc2-0.32b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc2-0.32b0) - 2022-07-04



- Fix instrument name and unit regexes
([#2796](https://github.com/open-telemetry/opentelemetry-python/pull/2796))
- Add optional sessions parameter to all Exporters leveraging requests.Session
([#2783](https://github.com/open-telemetry/opentelemetry-python/pull/2783))
([#2783](https://github.com/open-telemetry/opentelemetry-python/pull/2783)
- Add min/max fields to Histogram
([#2759](https://github.com/open-telemetry/opentelemetry-python/pull/2759))
- `opentelemetry-exporter-otlp-proto-http` Add support for OTLP/HTTP log exporter
Expand Down
6 changes: 4 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 29 additions & 1 deletion opentelemetry-api/tests/trace/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
# pylint: disable=W0212,W0222,W0221
import typing
import unittest
from contextlib import contextmanager

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):
Expand All @@ -35,6 +40,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): # type: ignore
with trace.use_span(self.start_span(*args, **kwargs)) as span: # type: ignore
yield span


class TestSpan(NonRecordingSpan):
pass
Expand Down Expand Up @@ -73,3 +83,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() -> Span:
return trace.get_current_span()

# call function before configuring tracing provider, should
# return INVALID_SPAN from the NoOpTracer
self.assertEqual(my_function(), trace.INVALID_SPAN)

# configure tracing provider
trace.set_tracer_provider(TestProvider())
# call function again, we should now be getting a TestSpan
self.assertIsInstance(my_function(), TestSpan)