From 37a9b54c3378c2b9382ccd3da58030ccda3442ae Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 23 Dec 2024 11:25:07 +0100 Subject: [PATCH 1/2] opentelemetry-sdk: don't print warnings if SDK is disabled --- .../src/opentelemetry/sdk/_logs/_internal/__init__.py | 1 - .../src/opentelemetry/sdk/metrics/_internal/__init__.py | 1 - opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 1 - opentelemetry-sdk/tests/logs/test_handler.py | 7 +------ opentelemetry-sdk/tests/logs/test_logs.py | 6 +----- 5 files changed, 2 insertions(+), 14 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py index c2db81687a..d277b5cae0 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py @@ -670,7 +670,6 @@ def get_logger( attributes: Optional[Attributes] = None, ) -> Logger: if self._disabled: - warnings.warn("SDK is disabled.") return NoOpLogger( name, version=version, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py index 1ef8dcfaa6..334dd01b10 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py @@ -537,7 +537,6 @@ def get_meter( attributes: Optional[Attributes] = None, ) -> Meter: if self._disabled: - _logger.warning("SDK is disabled.") return NoOpMeter(name, version=version, schema_url=schema_url) if self._shutdown: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index c40a7a7070..2e4448387c 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -1228,7 +1228,6 @@ def get_tracer( attributes: typing.Optional[types.Attributes] = None, ) -> "trace_api.Tracer": if self._disabled: - logger.warning("SDK is disabled.") return NoOpTracer() if not instrumenting_module_name: # Reject empty strings too. instrumenting_module_name = "" diff --git a/opentelemetry-sdk/tests/logs/test_handler.py b/opentelemetry-sdk/tests/logs/test_handler.py index f6daa1b22c..7f8763bb00 100644 --- a/opentelemetry-sdk/tests/logs/test_handler.py +++ b/opentelemetry-sdk/tests/logs/test_handler.py @@ -15,7 +15,6 @@ import logging import os import unittest -import warnings from unittest.mock import Mock, patch from opentelemetry._logs import NoOpLoggerProvider, SeverityNumber @@ -290,11 +289,7 @@ def test_handler_root_logger_with_disabled_sdk_does_not_go_into_recursion_error( processor, logger = set_up_test_logging( logging.NOTSET, root_logger=True ) - with warnings.catch_warnings(record=True) as cw: - logger.warning("hello") - - self.assertEqual(len(cw), 1) - self.assertEqual("SDK is disabled.", str(cw[0].message)) + logger.warning("hello") self.assertEqual(processor.emit_count(), 0) diff --git a/opentelemetry-sdk/tests/logs/test_logs.py b/opentelemetry-sdk/tests/logs/test_logs.py index 0590669653..92daf4d40b 100644 --- a/opentelemetry-sdk/tests/logs/test_logs.py +++ b/opentelemetry-sdk/tests/logs/test_logs.py @@ -15,7 +15,6 @@ # pylint: disable=protected-access import unittest -import warnings from unittest.mock import Mock, patch from opentelemetry.sdk._logs import LoggerProvider @@ -70,12 +69,9 @@ def test_get_logger(self): @patch.dict("os.environ", {OTEL_SDK_DISABLED: "true"}) def test_get_logger_with_sdk_disabled(self): - with warnings.catch_warnings(record=True) as cw: - logger = LoggerProvider().get_logger(Mock()) + logger = LoggerProvider().get_logger(Mock()) self.assertIsInstance(logger, NoOpLogger) - self.assertEqual(len(cw), 1) - self.assertEqual("SDK is disabled.", str(cw[0].message)) @patch.object(Resource, "create") def test_logger_provider_init(self, resource_patch): From 14f3a5d86a01b8692973942bba82e0073bd6b7dd Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 23 Dec 2024 11:34:20 +0100 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad1d754bd8..e71d5a7b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4364](https://github.com/open-telemetry/opentelemetry-python/pull/4364)) - Add Python 3.13 support ([#4353](https://github.com/open-telemetry/opentelemetry-python/pull/4353)) +- sdk: don't log or print warnings when the SDK has been disabled + ([#4371](https://github.com/open-telemetry/opentelemetry-python/pull/4371)) ## Version 1.29.0/0.50b0 (2024-12-11)