diff --git a/opentelemetry-api/CHANGELOG.md b/opentelemetry-api/CHANGELOG.md index 10c08c8574a..9a2647da26f 100644 --- a/opentelemetry-api/CHANGELOG.md +++ b/opentelemetry-api/CHANGELOG.md @@ -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 diff --git a/opentelemetry-api/src/opentelemetry/metrics/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/__init__.py index aa2988bce40..90f2f03f56b 100644 --- a/opentelemetry-api/src/opentelemetry/metrics/__init__.py +++ b/opentelemetry-api/src/opentelemetry/metrics/__init__.py @@ -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 diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index fa0bc376e7f..196cf3390cd 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -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 diff --git a/opentelemetry-api/tests/metrics/test_globals.py b/opentelemetry-api/tests/metrics/test_globals.py new file mode 100644 index 00000000000..9b9cfb94d90 --- /dev/null +++ b/opentelemetry-api/tests/metrics/test_globals.py @@ -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" + ) + ], + ) diff --git a/opentelemetry-api/tests/trace/test_globals.py b/opentelemetry-api/tests/trace/test_globals.py index 2f0f88fb280..5042dd2b726 100644 --- a/opentelemetry-api/tests/trace/test_globals.py +++ b/opentelemetry-api/tests/trace/test_globals.py @@ -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): @@ -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):