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

DRAFT: Add configuration of log level and formatting for Auto Instrumentation #4203

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#4094](https://github.com/open-telemetry/opentelemetry-python/pull/4094))
- Implement events sdk
([#4176](https://github.com/open-telemetry/opentelemetry-python/pull/4176))
- Enable configuration of logging format and level in auto-instrumentation
([#4203](https://github.com/open-telemetry/opentelemetry-python/pull/4203))

## Version 1.27.0/0.48b0 (2024-08-28)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL,
OTEL_TRACES_SAMPLER,
OTEL_TRACES_SAMPLER_ARG,
OTEL_LOG_LEVEL,
OTEL_PYTHON_LOG_FORMAT,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
Expand Down Expand Up @@ -238,6 +240,15 @@ def _init_logging(
exporters: Dict[str, Type[LogExporter]],
resource: Resource = None,
):
log_format = environ.get(OTEL_PYTHON_LOG_FORMAT, logging.BASIC_FORMAT)
levels = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
}
log_level = levels.get(environ.get(OTEL_LOG_LEVEL, "info").lower(), logging.INFO)

provider = LoggerProvider(resource=resource)
set_logger_provider(provider)

Expand All @@ -249,7 +260,11 @@ def _init_logging(

handler = LoggingHandler(level=logging.NOTSET, logger_provider=provider)

logging.getLogger().addHandler(handler)
logging.basicConfig(
xrmx marked this conversation as resolved.
Show resolved Hide resolved
format=log_format,
level=log_level,
handlers=(handler,)
)


def _import_exporters(
Expand Down Expand Up @@ -444,7 +459,8 @@ class _OTelSDKConfigurator(_BaseConfigurator):

Initializes several crucial OTel SDK components (i.e. TracerProvider,
MeterProvider, Processors...) according to a default implementation. Other
Configurators can subclass and slightly alter this initialization.
Configurators can subclass and slightly alter
this initialization.

NOTE: This class should not be instantiated nor should it become an entry
point on the `opentelemetry-sdk` package. Instead, distros should subclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
The :envvar:`OTEL_LOG_LEVEL` environment variable sets the log level used by the SDK logger
Default: "info"
"""
# TODO: OTEL_LOG_LEVEL vs OTEL_PYTHON_LOG_LEVEL
# TODO: add docstring
OTEL_PYTHON_LOG_FORMAT = "OTEL_PYTHON_LOG_FORMAT"
OTEL_PYTHON_LOG_LEVEL = "OTEL_PYTHON_LOG_LEVEL"

OTEL_TRACES_SAMPLER = "OTEL_TRACES_SAMPLER"
"""
Expand Down