Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support logs SDK auto instrumentation enable/disable with env #2728

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 it's related settings are experimental.
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
"""


OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE = (
"OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE"
)
Expand Down
26 changes: 26 additions & 0 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down