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

Added a warning log when the existing TracerProvider and MeterProvider are overridden #856

Merged
merged 7 commits into from
Jun 30, 2020
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,
bitspradp marked this conversation as resolved.
Show resolved Hide resolved
[
(
"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