feat(telemetry): add a switch to disable instrumentation via OTEL_SDK_DISABLED / enabled (#1059)#2931
Open
maxmilian wants to merge 1 commit into
Open
feat(telemetry): add a switch to disable instrumentation via OTEL_SDK_DISABLED / enabled (#1059)#2931maxmilian wants to merge 1 commit into
maxmilian wants to merge 1 commit into
Conversation
…_DISABLED / enabled (strands-agents#1059) StrandsTelemetry had no way to turn instrumentation off, so applications that already run their own OpenTelemetry setup still got Strands' tracer/meter providers registered globally and its spans/metrics emitted — even when they didn't want them (see strands-agents#1059, e.g. metrics that a downstream CloudWatch pipeline can't flush). Add an `enabled` switch to StrandsTelemetry that defaults to honoring the OTel-standard `OTEL_SDK_DISABLED=true` environment variable, with an explicit `StrandsTelemetry(enabled=False)` override for programmatic control. When disabled, no global tracer/meter provider is registered and the `setup_*` methods are no-ops, leaving the host application's OpenTelemetry setup untouched. The default (enabled) path is unchanged, so this is backward compatible. Scoped to `telemetry/config.py` (the location @zastrowm suggested on the issue); the separate ThreadingInstrumentor opt-in work in strands-agents#2167 is untouched.
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.
Description
StrandsTelemetryhad no way to turn instrumentation off. Applications that already run their own OpenTelemetry setup still get Strands' tracer/meter providers registered globally and its spans/metrics emitted — even when they don't want them (#1059; one reporter's downstream CloudWatch pipeline couldn't flush the Strands metrics and accumulated them like a leak).This adds an
enabledswitch toStrandsTelemetrythat defaults to honoring the OTel-standardOTEL_SDK_DISABLED=trueenvironment variable, with an explicitStrandsTelemetry(enabled=False)override for programmatic control (the explicit arg always wins; the env var is consulted only whenenabledis leftNone).When disabled:
__init__does not create or register a global tracer provider and does not set global propagators;setup_console_exporter/setup_otlp_exporter/setup_meterare no-ops (returnself, build nothing, register no meter provider).The default (no env var, no arg) path is unchanged and fully backward compatible —
__init__is additive (new keyword-onlyenabledarg defaulting toNone).This is the
StrandsTelemetry-level toggle @zastrowm suggested on the issue.Scope (please confirm)
This gates provider/exporter registration via
StrandsTelemetry. It intentionally does not changeTracer/get_traceragent-span creation or the unconditionalThreadingInstrumentor().instrument()intelemetry/tracer.py. So for a host that never instantiatesStrandsTelemetry(relying entirely on its own OTel), Strands spans on the global provider and the threading instrumentor are still active —OTEL_SDK_DISABLEDwould not stop those. I kept this PR focused on the location you pointed to and steered clear of the separateThreadingInstrumentoropt-in work in #2167. Happy to follow up with span-creation gating inTracerif you'd like that covered here too — let me know your preference.Related Issues
Relates to #1059 (gates the
StrandsTelemetryportion; see Scope note above).Documentation PR
The public API is documented inline (the
StrandsTelemetrydocstring now coversOTEL_SDK_DISABLEDand theenabledarg). I can add a short note to the conversation-management / observability page undersite/in a follow-up if you'd like it surfaced in the user guide.Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
hatch run preparehatchwasn't available in my environment, so I ran the underlying checks directly in a venv with.[dev,otel]installed (fromstrands-py/):pytest tests/strands/telemetry/test_config.py→ 23 passedruff format --check src/strands/telemetry/config.py tests/strands/telemetry/test_config.py→ cleanruff check ...→ All checks passedmypy src/strands/telemetry/config.py→ Success, no issuesThe new tests cover the env-var path, the explicit-flag path, explicit-flag-overrides-env, that disabling leaves a host-provided provider unregistered, that
setup_*are no-ops when disabled, andOTEL_SDK_DISABLEDparsing (case-insensitive / trimmed / only"true"disables). Red/green verified: the new behavior tests fail againstmain(noenabledswitch) and pass on this branch.Checklist
site/user-guide note as follow-up — see Documentation PR section)StrandsTelemetrydocstring)By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.