Skip to content

Commit

Permalink
Added a warning log when the existing TracerProvider and MeterProvide…
Browse files Browse the repository at this point in the history
…r are overridden (open-telemetry#856)
  • Loading branch information
bitspradp authored and cnnradams committed Jul 2, 2020
1 parent 5cd1028 commit cbe821f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Released 2020-06-10
([#764](https://github.com/open-telemetry/opentelemetry-python/pull/764))
- Add SumObserver and UpDownSumObserver in metrics
([#789](https://github.com/open-telemetry/opentelemetry-python/pull/789))
- Log a warning when replacing the global Tracer/Meter provider
([#856](https://github.com/open-telemetry/opentelemetry-python/pull/856))

## 0.8b0

Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-api/src/opentelemetry/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ def get_meter(
def set_meter_provider(meter_provider: MeterProvider) -> None:
"""Sets the current global :class:`~.MeterProvider` object."""
global _METER_PROVIDER # pylint: disable=global-statement

if _METER_PROVIDER is not None:
logger.warning("Overriding current MeterProvider")

_METER_PROVIDER = meter_provider


Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ def get_tracer(
def set_tracer_provider(tracer_provider: TracerProvider) -> None:
"""Sets the current global :class:`~.TracerProvider` object."""
global _TRACER_PROVIDER # pylint: disable=global-statement

if _TRACER_PROVIDER is not None:
logger.warning("Overriding current TracerProvider")

_TRACER_PROVIDER = tracer_provider


Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-api/tests/metrics/test_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# type:ignore
import unittest
from logging import WARNING

from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider


class TestGlobals(unittest.TestCase):
def test_meter_provider_override_warning(self):
"""metrics.set_meter_provider should throw a warning when overridden"""
metrics.set_meter_provider(MeterProvider())
with self.assertLogs(level=WARNING) as test:
metrics.set_meter_provider(MeterProvider())
self.assertEqual(
test.output,
[
(
"WARNING:opentelemetry.metrics:Overriding current "
"MeterProvider"
)
],
)
17 changes: 17 additions & 0 deletions opentelemetry-api/tests/trace/test_globals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest
from logging import WARNING
from unittest.mock import patch

from opentelemetry import context, trace
from opentelemetry.sdk.trace import TracerProvider # type:ignore


class TestGlobals(unittest.TestCase):
Expand All @@ -20,6 +22,21 @@ def test_get_tracer(self):
trace.get_tracer("foo", "var", mock_provider)
mock_provider.get_tracer.assert_called_with("foo", "var")

def test_tracer_provider_override_warning(self):
"""trace.set_tracer_provider should throw a warning when overridden"""
trace.set_tracer_provider(TracerProvider())
with self.assertLogs(level=WARNING) as test:
trace.set_tracer_provider(TracerProvider())
self.assertEqual(
test.output,
[
(
"WARNING:opentelemetry.trace:Overriding current "
"TracerProvider"
)
],
)


class TestTracer(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit cbe821f

Please sign in to comment.