Skip to content

Commit 928d333

Browse files
authored
Support logs SDK auto instrumentation enable/disable with env (#2728)
1 parent f8d6795 commit 928d333

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#2726](https://github.com/open-telemetry/opentelemetry-python/pull/2726))
2020
- fix: frozenset object has no attribute items
2121
([#2727](https://github.com/open-telemetry/opentelemetry-python/pull/2727))
22+
- Support logs SDK auto instrumentation enable/disable with env
23+
([#2728](https://github.com/open-telemetry/opentelemetry-python/pull/2728))
2224
- fix: update entry point object references for metrics
2325
([#2731](https://github.com/open-telemetry/opentelemetry-python/pull/2731))
2426

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import logging
21+
import os
2122
from abc import ABC, abstractmethod
2223
from os import environ
2324
from typing import Dict, Optional, Sequence, Tuple, Type
@@ -36,6 +37,9 @@
3637
set_log_emitter_provider,
3738
)
3839
from opentelemetry.sdk._logs.export import BatchLogProcessor, LogExporter
40+
from opentelemetry.sdk.environment_variables import (
41+
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED,
42+
)
3943
from opentelemetry.sdk.resources import Resource
4044
from opentelemetry.sdk.trace import TracerProvider
4145
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter
@@ -185,7 +189,11 @@ def _initialize_components(auto_instrumentation_version):
185189
id_generator_name = _get_id_generator()
186190
id_generator = _import_id_generator(id_generator_name)
187191
_init_tracing(trace_exporters, id_generator, auto_instrumentation_version)
188-
_init_logging(log_exporters, auto_instrumentation_version)
192+
logging_enabled = os.getenv(
193+
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "false"
194+
)
195+
if logging_enabled.strip().lower() == "true":
196+
_init_logging(log_exporters, auto_instrumentation_version)
189197

190198

191199
class _BaseConfigurator(ABC):

opentelemetry-sdk/src/opentelemetry/sdk/environment_variables.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,20 @@
407407
LogEmitterProvider is used.
408408
"""
409409

410+
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED = (
411+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED"
412+
)
413+
"""
414+
.. envvar:: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
415+
416+
The :envvar:`OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable allows users to
417+
enable/disabe the logging SDK auto instrumentation.
418+
Default: False
419+
420+
Note: Logs SDK and its related settings are experimental.
421+
"""
422+
423+
410424
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = (
411425
"OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE"
412426
)

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
_import_id_generator,
3030
_init_logging,
3131
_init_tracing,
32+
_initialize_components,
3233
)
3334
from opentelemetry.sdk._logs import LoggingHandler
3435
from opentelemetry.sdk._logs.export import ConsoleLogExporter
@@ -277,6 +278,31 @@ def test_logging_init_exporter(self):
277278
logging.getLogger(__name__).error("hello")
278279
self.assertTrue(provider.processor.exporter.export_called)
279280

281+
@patch.dict(
282+
environ,
283+
{"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service"},
284+
)
285+
@patch("opentelemetry.sdk._configuration._init_tracing")
286+
@patch("opentelemetry.sdk._configuration._init_logging")
287+
def test_logging_init_disable_default(self, logging_mock, tracing_mock):
288+
_initialize_components("auto-version")
289+
self.assertEqual(logging_mock.call_count, 0)
290+
self.assertEqual(tracing_mock.call_count, 1)
291+
292+
@patch.dict(
293+
environ,
294+
{
295+
"OTEL_RESOURCE_ATTRIBUTES": "service.name=otlp-service",
296+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "True",
297+
},
298+
)
299+
@patch("opentelemetry.sdk._configuration._init_tracing")
300+
@patch("opentelemetry.sdk._configuration._init_logging")
301+
def test_logging_init_enable_env(self, logging_mock, tracing_mock):
302+
_initialize_components("auto-version")
303+
self.assertEqual(logging_mock.call_count, 1)
304+
self.assertEqual(tracing_mock.call_count, 1)
305+
280306

281307
class TestExporterNames(TestCase):
282308
def test_otlp_exporter_overwrite(self):

0 commit comments

Comments
 (0)