fix(tracing): lazy-import OpenTelemetry SDK only when tracing is enabled#69860
Open
mangodxd wants to merge 1 commit into
Open
fix(tracing): lazy-import OpenTelemetry SDK only when tracing is enabled#69860mangodxd wants to merge 1 commit into
mangodxd wants to merge 1 commit into
Conversation
Module-level try/except imports of the entire OpenTelemetry SDK ran on every import of salt.utils.tracing, regardless of whether tracing.enabled is true (default: false). Since tracing is imported by every daemon entry point (master, minion, channel, event, reactor, rest_cherrypy), every Python process paid the OTel import cost. Move the imports into a _lazy_init_otel() function called only when configure() receives tracing.enabled = True.
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lazy-import OpenTelemetry SDK in tracing module — avoids paying OTel import cost in every daemon process when tracing is disabled
Only import OTel symbols when configure() is called with tracing.enabled = True, which is the default-false path every daemon takes. The module-level try/except was running all OTel imports unconditionally at import time.
The _lazy_init_otel() guard is called only from configure() after the tracing.enabled check, so a disabled-tracing process never touches the opentelemetry packages.
No behavioral change when tracing is enabled — OTel is still imported before any span is created, and ImportError is still handled the same way.
VERIFY
Unit tests for tracing
pytest tests/pytests/unit/utils/test_tracing.py -x -v
Manual: confirm OTel is NOT imported at module load
python -c "import sys; from salt.utils.tracing import _OTEL_AVAILABLE; print('OTEL_AVAILABLE at import:', _OTEL_AVAILABLE); print('opentelemetry in sys.modules:', any('opentelemetry' in m for m in sys.modules))"
Should print: OTEL_AVAILABLE at import: False / opentelemetry in sys.modules: False
Fixes #69855