-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix logging configuration to prevent OpenTelemetry handler conflicts #8269
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
Fix logging configuration to prevent OpenTelemetry handler conflicts #8269
Conversation
Signed-off-by: dmavrommatis <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR replaces the global dictConfig
approach with direct logger/handler setup to avoid flushing OpenTelemetry handlers and to only clear DSPy’s own handler.
- Use
logging.Formatter
andStreamHandler
directly instead ofdictConfig
. - Name the DSPy-specific handler
"dspy_handler"
and remove only handlers with that name. - Preserve all other handlers to prevent long blocking operations.
Comments suppressed due to low confidence (1)
dspy/utils/logging_utils.py:68
- [nitpick] There are no tests verifying that existing DSPy handlers are correctly removed and new ones added; consider adding tests for multiple consecutive calls to configure_dspy_loggers to validate handler cleanup logic.
for existing_handler in logger.handlers[:]:
@dmavrommatis Thanks for the PR! Is this the correct code snippet to reproduce the slowdown of using opentelemetry together with DSPy logging?
This finishes instantly on my side. |
@chenmoneygithub you need to also have an opentelemetry-collector running and to have the environment variables so when you call To setup everything to replicate the behavior you can run
then have an example.py with
and then run it with
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification, LGTM!
Fix logging configuration to prevent OpenTelemetry handler conflicts
This PR modifies how DSPy configures its loggers to prevent conflicts with OpenTelemetry logging handlers and avoid long blocking operations during logger configuration.
Problem
The current implementation uses
logging.config.dictConfig()
which triggers a full handler cleanup through_clearExistingHandlers()
. This causes issues when OpenTelemetry logging handlers are present in the environment because:Here's an example that demonstrates the issue run with
opentelemetry-instrument
:This simple code takes 30+ seconds to run and often needs to be interrupted. Here's the stack trace showing where it blocks:
Solution
This PR replaces
dictConfig
with direct logger configuration using Python's logging API. The new implementation:Key changes:
logging.StreamHandler
andlogging.Formatter
directlyThis approach allows DSPy to:
Testing
The modified code runs instantly without delays, even in environments with OpenTelemetry handlers present. The logging output remains unchanged, maintaining backward compatibility while fixing the performance issue.