Skip to content

docs: warn about dictConfig clearing OTel handlers in logs example#5420

Draft
avinab-neogy wants to merge 2 commits into
open-telemetry:mainfrom
avinab-neogy:docs/add-dictconfig-warning-logging
Draft

docs: warn about dictConfig clearing OTel handlers in logs example#5420
avinab-neogy wants to merge 2 commits into
open-telemetry:mainfrom
avinab-neogy:docs/add-dictconfig-warning-logging

Conversation

@avinab-neogy

@avinab-neogy avinab-neogy commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Description

Follow-up to #5344 which addressed the LoggingHandler move to contrib.

Refs #4738

This PR documents the dictConfig handler-saving pattern from point 3
of #4738. When dictConfig is called with a root section that defines
handlers, it clears existing handlers on the root logger including the
OTel handler, silently breaking telemetry. Users need to save and
restore handlers around the call.

Tested locally — confirmed that dictConfig with a root section wipes
all existing handlers.

Opening as draft to confirm scope before requesting review.
@xrmx does this belong in the logs example README or somewhere else?

Type of change

  • This change requires a documentation update

How Has This Been Tested?

Verified locally that calling dictConfig with a root section clears
existing handlers:

import logging, logging.config
handler = logging.StreamHandler()
logging.getLogger().addHandler(handler)
print('Before:', logging.getLogger().handlers)  # [StreamHandler]
logging.config.dictConfig({'version': 1, 'root': {'level': 'DEBUG', 'handlers': []}})
print('After:', logging.getLogger().handlers)   # []

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@avinab-neogy
avinab-neogy force-pushed the docs/add-dictconfig-warning-logging branch from 590cb1b to e8465b0 Compare July 16, 2026 05:02
that it may clear existing handlers on the root logger, including the
OTel handler. To avoid losing telemetry, save and restore the root
logger handlers around the ``dictConfig`` call:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DylanRussell @herin049 Thanks for pointing that out, you're both totally right!

To answer your question @herin049, I went back into my local setup and my original test was just running standard Python's dictConfig function with a vanilla StreamHandler, completely bypassing the OTel instrumentation.

I tested it again using the OpenTelemetry library, and I could see how it automatically saves and restores the handler.

import logging
import logging.config
from opentelemetry.instrumentation.logging import LoggingInstrumentor

LoggingInstrumentor().instrument()
root_logger = logging.getLogger()

print('Before dictConfig:', root_logger.handlers) # [<LoggingHandler (NOTSET)>]

logging.config.dictConfig({'version': 1, 'root': {'level': 'DEBUG', 'handlers': []}})

print('After dictConfig: ', root_logger.handlers) # [<LoggingHandler (NOTSET)>] - It does not get cleared!

That said, I originally opened this to address Point 3 from Issue #4738 :

  1. the root logger is instrumented, so handlers on it must not be cleared and if you are loading logging settings with logging/.config.dictConfig, you must save the root loggers before applying dictConfig, then re-apply missing handlers

This issue explicitly asked for this warning to be added to the docs. I'm guessing that requirement came from someone configuring the LoggingHandler manually via the core SDK, where this monkey-patch doesn't exist?

How would you prefer to handle this? I'm happy to do either of the following:

  1. Close this PR, and we can update Point 3 on the original issue to say it's no longer an issue when using the instrumentation library.
  2. Keep the PR open but rewrite the warning to clarify that it only applies when configuring the LoggingHandler manually via the core SDK, since dictConfig will still clear their handlers without the instrumentation package.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just close this PR, I don't see any behavior here that needs to be documented.

@herin049

Copy link
Copy Markdown
Contributor

I have the same understanding as @DylanRussell. Do you have an example reproduction that actually involves using the logging instrumentation library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants