diff --git a/CHANGELOG.md b/CHANGELOG.md index 5807688a184..b0ee24bfd90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#2726](https://github.com/open-telemetry/opentelemetry-python/pull/2726)) - fix: frozenset object has no attribute items ([#2727](https://github.com/open-telemetry/opentelemetry-python/pull/2727)) +- Support logs SDK auto instrumentation enable/disable with env + ([#2728](https://github.com/open-telemetry/opentelemetry-python/pull/2728)) - fix: update entry point object references for metrics ([#2731](https://github.com/open-telemetry/opentelemetry-python/pull/2731)) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index efa6f31f894..8cb1d0f412f 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -18,6 +18,7 @@ """ import logging +import os from abc import ABC, abstractmethod from os import environ from typing import Dict, Optional, Sequence, Tuple, Type @@ -36,6 +37,9 @@ set_log_emitter_provider, ) from opentelemetry.sdk._logs.export import BatchLogProcessor, LogExporter +from opentelemetry.sdk.environment_variables import ( + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, +) from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter @@ -185,7 +189,11 @@ def _initialize_components(auto_instrumentation_version): id_generator_name = _get_id_generator() id_generator = _import_id_generator(id_generator_name) _init_tracing(trace_exporters, id_generator, auto_instrumentation_version) - _init_logging(log_exporters, auto_instrumentation_version) + logging_enabled = os.getenv( + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false" + ) + if logging_enabled.strip().lower() == "true": + _init_logging(log_exporters, auto_instrumentation_version) class _BaseConfigurator(ABC): diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py b/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py index 145489a683e..be105c3fbb8 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py @@ -407,6 +407,20 @@ LogEmitterProvider is used. """ +_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED = ( + "OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED" +) +""" +.. envvar:: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED + +The :envvar:`OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable allows users to +enable/disabe the logging SDK auto instrumentation. +Default: False + +Note: Logs SDK and its related settings are experimental. +""" + + OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = ( "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE" ) diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index 1905e6fc384..1b007d3b165 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -29,6 +29,7 @@ _import_id_generator, _init_logging, _init_tracing, + _initialize_components, ) from opentelemetry.sdk._logs import LoggingHandler from opentelemetry.sdk._logs.export import ConsoleLogExporter @@ -277,6 +278,31 @@ def test_logging_init_exporter(self): logging.getLogger(__name__).error("hello") self.assertTrue(provider.processor.exporter.export_called) + @patch.dict( + environ, + {"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"}, + ) + @patch("opentelemetry.sdk._configuration._init_tracing") + @patch("opentelemetry.sdk._configuration._init_logging") + def test_logging_init_disable_default(self, logging_mock, tracing_mock): + _initialize_components("auto-version") + self.assertEqual(logging_mock.call_count, 0) + self.assertEqual(tracing_mock.call_count, 1) + + @patch.dict( + environ, + { + "OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service", + "OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True", + }, + ) + @patch("opentelemetry.sdk._configuration._init_tracing") + @patch("opentelemetry.sdk._configuration._init_logging") + def test_logging_init_enable_env(self, logging_mock, tracing_mock): + _initialize_components("auto-version") + self.assertEqual(logging_mock.call_count, 1) + self.assertEqual(tracing_mock.call_count, 1) + class TestExporterNames(TestCase): def test_otlp_exporter_overwrite(self):