From e43a89921280b8fe9255c7e052f88ff4cb873062 Mon Sep 17 00:00:00 2001 From: Pradeep Date: Thu, 25 Jun 2020 19:01:01 +0100 Subject: [PATCH 1/4] Added a warning log when the existing TracerProvider and MeterProvider are overridden --- .../src/opentelemetry/metrics/__init__.py | 4 ++++ .../src/opentelemetry/trace/__init__.py | 4 ++++ .../tests/metrics/test_globals.py | 20 +++++++++++++++++++ opentelemetry-api/tests/trace/test_globals.py | 15 ++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 opentelemetry-api/tests/metrics/test_globals.py 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..8ca58a163db --- /dev/null +++ b/opentelemetry-api/tests/metrics/test_globals.py @@ -0,0 +1,20 @@ +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..c81d9d6d978 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 class TestGlobals(unittest.TestCase): @@ -20,6 +22,19 @@ 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): From 8755f346ec2a2180e6cd0ec4d60a13827c516cf3 Mon Sep 17 00:00:00 2001 From: Pradeep Date: Thu, 25 Jun 2020 22:52:49 +0100 Subject: [PATCH 2/4] Fixing mypy issues by adding #type ignore --- opentelemetry-api/tests/metrics/test_globals.py | 1 + opentelemetry-api/tests/trace/test_globals.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/tests/metrics/test_globals.py b/opentelemetry-api/tests/metrics/test_globals.py index 8ca58a163db..cf149bbf758 100644 --- a/opentelemetry-api/tests/metrics/test_globals.py +++ b/opentelemetry-api/tests/metrics/test_globals.py @@ -1,3 +1,4 @@ +# type:ignore import unittest from logging import WARNING diff --git a/opentelemetry-api/tests/trace/test_globals.py b/opentelemetry-api/tests/trace/test_globals.py index c81d9d6d978..c44d885e6c5 100644 --- a/opentelemetry-api/tests/trace/test_globals.py +++ b/opentelemetry-api/tests/trace/test_globals.py @@ -3,8 +3,7 @@ from unittest.mock import patch from opentelemetry import context, trace -from opentelemetry.sdk.trace import TracerProvider - +from opentelemetry.sdk.trace import TracerProvider # type:ignore class TestGlobals(unittest.TestCase): def setUp(self): From 452250c3ceb0acc386cd14d5eb471b9b90a0e925 Mon Sep 17 00:00:00 2001 From: Pradeep Date: Thu, 25 Jun 2020 22:59:38 +0100 Subject: [PATCH 3/4] Fixing black indentation with extra newline --- opentelemetry-api/tests/trace/test_globals.py | 1 + 1 file changed, 1 insertion(+) diff --git a/opentelemetry-api/tests/trace/test_globals.py b/opentelemetry-api/tests/trace/test_globals.py index c44d885e6c5..4f61f8cc2cf 100644 --- a/opentelemetry-api/tests/trace/test_globals.py +++ b/opentelemetry-api/tests/trace/test_globals.py @@ -5,6 +5,7 @@ from opentelemetry import context, trace from opentelemetry.sdk.trace import TracerProvider # type:ignore + class TestGlobals(unittest.TestCase): def setUp(self): self._patcher = patch("opentelemetry.trace._TRACER_PROVIDER") From 11fda162f2d75b7fe23d14298c2003398e34b5ac Mon Sep 17 00:00:00 2001 From: Pradeep Date: Fri, 26 Jun 2020 20:06:03 +0100 Subject: [PATCH 4/4] Fixing multi line string by enclosing it in braces and also added the changelog --- opentelemetry-api/CHANGELOG.md | 2 ++ opentelemetry-api/tests/metrics/test_globals.py | 6 ++++-- opentelemetry-api/tests/trace/test_globals.py | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) 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/tests/metrics/test_globals.py b/opentelemetry-api/tests/metrics/test_globals.py index cf149bbf758..9b9cfb94d90 100644 --- a/opentelemetry-api/tests/metrics/test_globals.py +++ b/opentelemetry-api/tests/metrics/test_globals.py @@ -15,7 +15,9 @@ def test_meter_provider_override_warning(self): self.assertEqual( test.output, [ - "WARNING:opentelemetry.metrics:Overriding current " - "MeterProvider" + ( + "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 4f61f8cc2cf..5042dd2b726 100644 --- a/opentelemetry-api/tests/trace/test_globals.py +++ b/opentelemetry-api/tests/trace/test_globals.py @@ -30,8 +30,10 @@ def test_tracer_provider_override_warning(self): self.assertEqual( test.output, [ - "WARNING:opentelemetry.trace:Overriding current " - "TracerProvider" + ( + "WARNING:opentelemetry.trace:Overriding current " + "TracerProvider" + ) ], )