Skip to content

fix(tracing): lazy-import OpenTelemetry SDK only when tracing is enabled#69860

Open
mangodxd wants to merge 1 commit into
saltstack:3007.xfrom
mangodxd:fix/lazy-otel-import
Open

fix(tracing): lazy-import OpenTelemetry SDK only when tracing is enabled#69860
mangodxd wants to merge 1 commit into
saltstack:3007.xfrom
mangodxd:fix/lazy-otel-import

Conversation

@mangodxd

Copy link
Copy Markdown

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

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.
@mangodxd
mangodxd requested a review from a team as a code owner July 23, 2026 18:04
@dwoz

dwoz commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

@mangodxd Is this covered by #69856

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.

2 participants