feat(logs): add OTEL_LOG_LEVEL support#5115
Conversation
22fd4b2 to
7c01660
Compare
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Thanks @grvmishra788. I think we need to move this so the main sdk/__init__.py so we don't need users to import another module.
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Thanks @grvmishra788 - looks really good. Just a few final tweaks for an errant new line and update to the changelog.
|
Hi @MikeGoldsmith! Could you please take a look at the PR now? It has been in review for a while. |
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Sorry for the delay, thanks @grvmishra788 🚀
|
@MikeGoldsmith One of the CI checks failed |
|
Ah, I see the problem - Adding We need to move it somewhere else, but then we'll lose the automatic resolution we wanted by putting it in init. Maybe put it back in _internal/init and add it to |
MikeGoldsmith
left a comment
There was a problem hiding this comment.
Moving to request changes until we fix the resolution problem.
|
Thanks for the PR! Just a heads-up: we no longer update Please add the appropriate changelog fragment for this change instead of editing |
Thanks for letting me know. switched to Towncrier changelog fragment instead. |
|
This PR and the auto-instrumentation PR (open-telemetry/opentelemetry-python-contrib#4542) should not conflict but noting that |
|
|
||
| # "warn" is accepted alongside "warning" because OTel canonical short names | ||
| # use "WARN", so users following OTel documentation will naturally try "warn". | ||
| _OTEL_LOG_LEVEL_TO_PYTHON = { |
There was a problem hiding this comment.
Maybe this should also map trace to debug. The OTEL_LOG_LEVEL spec doesn't define accepted values, but the declarative config schema includes trace. Since Python doesn't have trace, mapping trace to debug would align with the config model and with the debug startup messages PR that Tammy mentioned.
There was a problem hiding this comment.
Good call - done. "trace" now maps to DEBUG. Updated the dict, the warning message listing valid values, and the tests.
Python has no TRACE level; the OTel declarative config schema includes "trace" as a valid log level value, so mapping it to DEBUG aligns with the config model.
|
Hi @MikeGoldsmith @emdneto @pmcollins - this PR has been open for a while and I believe all the feedback has been addressed. Would appreciate a final review & merge when you get a chance. Thanks! |
Description
Fixes #1059: The
OTEL_LOG_LEVELenvironment variable has been defined in the SDK since the SDK was first written, but reading and applying it was never implemented.With this change, On SDK module load,
OTEL_LOG_LEVELis read and applied tologging.getLogger("opentelemetry.sdk")- the root SDK logger. As a result, all SDK sub-modules (trace, metrics, logs, exporters) inherit the configured level. Accepted values:debug,info,warn,warning,error,critical(case-insensitive). Invalid values emit aWARNINGto stderr.What
OTEL_LOG_LEVELcontrolsOTEL_LOG_LEVELis scoped to the SDK's own diagnostic output only. It does not filter OTel log records emitted by instrumented applications. This scope is:OTEL_LOG_LEVELis for setting the log level for the loggers used internally by SDK not for your application logs."OTEL_LOG_LEVEL: Exists in the SDK but unused. Speced out to be the level for the SDK's internal logger, not the LoggingHandler."Type of change
How Has This Been Tested?
pytest opentelemetry-sdk/tests/logs/test_logs.py— all tests pass.TestOtelLogLevelEnvVar.test_otel_log_level_to_python_mapping_accepted_valuesverifies the_OTEL_LOG_LEVEL_TO_PYTHONdict covers exactly the documented accepted values:{"debug", "info", "warn", "warning", "error", "critical"}.OTEL_LOG_LEVEL=debugbefore importing the SDK and confirm SDK debug messages appear; setOTEL_LOG_LEVEL=invalidand confirm aWARNINGis emitted with the valid values listed.Does This PR Require a Contrib Repo Change?
Checklist: