diff --git a/CHANGELOG.md b/CHANGELOG.md index a352b3d583..419afb8696 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4353](https://github.com/open-telemetry/opentelemetry-python/pull/4353)) - Add support for `explicit_bucket_boundaries` advisory for Histograms ([#4361](https://github.com/open-telemetry/opentelemetry-python/pull/4361)) +- 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) 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 0d7915a78c..1d5511a6ae 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py @@ -561,7 +561,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/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index a52573ac1c..2f95652de7 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -23,7 +23,8 @@ these attributes can be included in the Resource.* Resource objects are created with `Resource.create`, which accepts attributes -(key-values). Resources should NOT be created via constructor, and working with +(key-values). Resources should NOT be created via constructor except by `ResourceDetector` +instances which can't use `Resource.create` to avoid infinite loops. Working with `Resource` objects should only be done via the Resource API methods. Resource attributes can also be passed at process invocation in the :envvar:`OTEL_RESOURCE_ATTRIBUTES` environment variable. You should register @@ -175,6 +176,8 @@ def create( ) -> "Resource": """Creates a new `Resource` from attributes. + `ResourceDetector` instances should not call this method. + Args: attributes: Optional zero or more key-value pairs. schema_url: Optional URL pointing to the schema @@ -316,6 +319,7 @@ def __init__(self, raise_on_error: bool = False) -> None: @abc.abstractmethod def detect(self) -> "Resource": + """Don't call `Resource.create` here to avoid an infinite loop, instead instantiate `Resource` directly""" raise NotImplementedError() 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):