Skip to content

Commit

Permalink
Add NoOpTracer and NoOpTracerProvider classes (#2363)
Browse files Browse the repository at this point in the history
* Add _NoOpTracer and _NoOpTracerProvider classes

Add `_NoOpTracer` and `_NoOpTracerProvider`. Marking `_DefaultTracer` and `_DefaultTracerProvider` as deprecated.

Fixes #2362

* update changelog

* update class

* fix lint
  • Loading branch information
Alex Boten authored Jan 17, 2022
1 parent fb1063c commit 1a52e9d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
26 changes: 22 additions & 4 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-api/tests/test_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions opentelemetry-api/tests/trace/test_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/tests/trace/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-api/tests/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1a52e9d

Please sign in to comment.