From 92f221c7fcb6573195673df935b179597cfb4b6e Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Mon, 23 Dec 2024 20:25:17 +0100 Subject: [PATCH 1/2] opentelemetry-sdk: clarify Resource.create usage (#4372) * opentelemetry-sdk: clarify Resource.create usage After #3181 ResourceDetector instances cannot call Resource.create otherwise when using auto-instrumentation you'll go into an infinite loop trying to load resource detectors. * Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py Co-authored-by: Aaron Abbott * Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py Co-authored-by: Aaron Abbott * More precise instructions --------- Co-authored-by: Aaron Abbott --- .../src/opentelemetry/sdk/resources/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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() From 29aad2efa30a35757ea78d90e4d3fd517bc00468 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Tue, 24 Dec 2024 10:12:37 +0100 Subject: [PATCH 2/2] opentelemetry-sdk: don't print warnings if SDK is disabled (#4371) * opentelemetry-sdk: don't print warnings if SDK is disabled * Add changelog --- CHANGELOG.md | 2 ++ .../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 +----- 6 files changed, 4 insertions(+), 14 deletions(-) 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) 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):