From 1a52e9df1c10a42da7562f8b4d11f43576ed0aed Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 17 Jan 2022 11:30:53 -0800 Subject: [PATCH] Add NoOpTracer and NoOpTracerProvider classes (#2363) * Add _NoOpTracer and _NoOpTracerProvider classes Add `_NoOpTracer` and `_NoOpTracerProvider`. Marking `_DefaultTracer` and `_DefaultTracerProvider` as deprecated. Fixes #2362 * update changelog * update class * fix lint --- CHANGELOG.md | 2 ++ opentelemetry-api/setup.cfg | 2 +- .../src/opentelemetry/trace/__init__.py | 26 ++++++++++++++++--- .../tests/test_implementation.py | 3 +-- opentelemetry-api/tests/trace/test_globals.py | 5 ++-- opentelemetry-api/tests/trace/test_proxy.py | 4 +-- opentelemetry-api/tests/trace/test_tracer.py | 3 +-- 7 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85fd2794c0d..24283462137 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2323](https://github.com/open-telemetry/opentelemetry-python/pull/2323)) - Complete metric exporter format and update OTLP exporter ([#2364](https://github.com/open-telemetry/opentelemetry-python/pull/2364)) +- [api] Add `NoOpTracer` and `NoOpTracerProvider`. Marking `_DefaultTracer` and `_DefaultTracerProvider` as deprecated. + ([#2363](https://github.com/open-telemetry/opentelemetry-python/pull/2363)) ## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17 diff --git a/opentelemetry-api/setup.cfg b/opentelemetry-api/setup.cfg index 3c10d644810..6a0fa67a704 100644 --- a/opentelemetry-api/setup.cfg +++ b/opentelemetry-api/setup.cfg @@ -54,7 +54,7 @@ where = src opentelemetry_context = contextvars_context = opentelemetry.context.contextvars_context:ContextVarsRuntimeContext opentelemetry_tracer_provider = - default_tracer_provider = opentelemetry.trace:_DefaultTracerProvider + default_tracer_provider = opentelemetry.trace:NoOpTracerProvider opentelemetry_meter_provider = default_meter_provider = opentelemetry._metrics:_DefaultMeterProvider opentelemetry_propagator = diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 26df821cbc3..56927bf6640 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -82,6 +82,8 @@ from logging import getLogger from typing import Iterator, Optional, Sequence, cast +from deprecated import deprecated + from opentelemetry import context as context_api from opentelemetry.attributes import BoundedAttributes # type: ignore from opentelemetry.context.context import Context @@ -216,7 +218,7 @@ def get_tracer( """ -class _DefaultTracerProvider(TracerProvider): +class NoOpTracerProvider(TracerProvider): """The default TracerProvider, used when no implementation is available. All operations are no-op. @@ -229,7 +231,15 @@ def get_tracer( schema_url: typing.Optional[str] = None, ) -> "Tracer": # pylint:disable=no-self-use,unused-argument - return _DefaultTracer() + return NoOpTracer() + + +@deprecated(version="1.9.0", reason="You should use NoOpTracerProvider") # type: ignore +class _DefaultTracerProvider(NoOpTracerProvider): + """The default TracerProvider, used when no implementation is available. + + All operations are no-op. + """ class ProxyTracerProvider(TracerProvider): @@ -393,7 +403,7 @@ def __init__( self._instrumenting_library_version = instrumenting_library_version self._schema_url = schema_url self._real_tracer: Optional[Tracer] = None - self._noop_tracer = _DefaultTracer() + self._noop_tracer = NoOpTracer() @property def _tracer(self) -> Tracer: @@ -416,7 +426,7 @@ def start_as_current_span(self, *args, **kwargs) -> Span: # type: ignore return self._tracer.start_as_current_span(*args, **kwargs) # type: ignore -class _DefaultTracer(Tracer): +class NoOpTracer(Tracer): """The default Tracer, used when no Tracer implementation is available. All operations are no-op. @@ -453,6 +463,14 @@ def start_as_current_span( yield INVALID_SPAN +@deprecated(version="1.9.0", reason="You should use NoOpTracer") # type: ignore +class _DefaultTracer(NoOpTracer): + """The default Tracer, used when no Tracer implementation is available. + + All operations are no-op. + """ + + _TRACER_PROVIDER_SET_ONCE = Once() _TRACER_PROVIDER: Optional[TracerProvider] = None _PROXY_TRACER_PROVIDER = ProxyTracerProvider() diff --git a/opentelemetry-api/tests/test_implementation.py b/opentelemetry-api/tests/test_implementation.py index 1871ea951e1..913efbffb3b 100644 --- a/opentelemetry-api/tests/test_implementation.py +++ b/opentelemetry-api/tests/test_implementation.py @@ -33,8 +33,7 @@ def test_tracer(self): trace.TracerProvider() # type:ignore def test_default_tracer(self): - # pylint: disable=protected-access - tracer_provider = trace._DefaultTracerProvider() + tracer_provider = trace.NoOpTracerProvider() tracer = tracer_provider.get_tracer(__name__) with tracer.start_span("test") as span: self.assertEqual( diff --git a/opentelemetry-api/tests/trace/test_globals.py b/opentelemetry-api/tests/trace/test_globals.py index 421b72d65fe..0ead559f862 100644 --- a/opentelemetry-api/tests/trace/test_globals.py +++ b/opentelemetry-api/tests/trace/test_globals.py @@ -83,11 +83,10 @@ def do_concurrently() -> Mock: class TestTracer(unittest.TestCase): def setUp(self): - # pylint: disable=protected-access - self.tracer = trace._DefaultTracer() + self.tracer = trace.NoOpTracer() def test_get_current_span(self): - """_DefaultTracer's start_span will also + """NoOpTracer's start_span will also be retrievable via get_current_span """ self.assertEqual(trace.get_current_span(), trace.INVALID_SPAN) diff --git a/opentelemetry-api/tests/trace/test_proxy.py b/opentelemetry-api/tests/trace/test_proxy.py index da1d60c74e1..b361540b9d3 100644 --- a/opentelemetry-api/tests/trace/test_proxy.py +++ b/opentelemetry-api/tests/trace/test_proxy.py @@ -21,7 +21,7 @@ from opentelemetry.trace.span import INVALID_SPAN_CONTEXT, NonRecordingSpan -class TestProvider(trace._DefaultTracerProvider): +class TestProvider(trace.NoOpTracerProvider): def get_tracer( self, instrumenting_module_name: str, @@ -31,7 +31,7 @@ def get_tracer( return TestTracer() -class TestTracer(trace._DefaultTracer): +class TestTracer(trace.NoOpTracer): def start_span(self, *args, **kwargs): return TestSpan(INVALID_SPAN_CONTEXT) diff --git a/opentelemetry-api/tests/trace/test_tracer.py b/opentelemetry-api/tests/trace/test_tracer.py index 382aeec743b..774746d41a6 100644 --- a/opentelemetry-api/tests/trace/test_tracer.py +++ b/opentelemetry-api/tests/trace/test_tracer.py @@ -19,8 +19,7 @@ class TestTracer(unittest.TestCase): def setUp(self): - # pylint: disable=protected-access - self.tracer = trace._DefaultTracer() + self.tracer = trace.NoOpTracer() def test_start_span(self): with self.tracer.start_span("") as span: