feat(telemetry): add configuration to disable OpenTelemetry instrumentation
Description
Adds an enabled parameter to StrandsTelemetry class that allows disabling OpenTelemetry instrumentation. This addresses the use case where Strands Agents is used as part of a system that already has OpenTelemetry enabled, and the user doesn't want to include child spans from Strands.
Related Issues
Resolves #1059
Documentation PR
No documentation changes required - the feature is self-documenting via docstrings and follows the existing patterns.
Type of Change
- [x] New feature (non-breaking change which adds functionality)
Changes Made
StrandsTelemetry class enhancements:
-
New
enabledparameter in constructor:- Defaults to
TrueunlessSTRANDS_OTEL_ENABLEDenvironment variable is set tofalse/0/no/off - Explicit
enabled=Falseoverrides environment variable
- Defaults to
-
NoOpTracerProviderusage:- When disabled, uses OpenTelemetry's built-in
NoOpTracerProviderinstead ofSDKTracerProvider - No spans are created or exported
- When disabled, uses OpenTelemetry's built-in
-
No-op setup methods:
setup_console_exporter(),setup_otlp_exporter(), andsetup_meter()become no-ops when disabled- Methods still return
selfto maintain method chaining compatibility
-
New
enabledproperty:- Read-only property to check if telemetry is enabled
Usage Examples
# Disable via constructor
StrandsTelemetry(enabled=False)
# Disable via environment variable
# export STRANDS_OTEL_ENABLED=false
StrandsTelemetry() # Will use NoOpTracerProvider
# Method chaining still works when disabled
StrandsTelemetry(enabled=False).setup_console_exporter().setup_otlp_exporter()
Testing
Added 10 new unit tests covering:
-
Disabling via constructor
-
Disabling via environment variable (multiple formats:
false,0,off,no) -
Explicit enable overriding environment variable
-
No-op behavior of setup methods when disabled
-
Method chaining compatibility when disabled
-
[x] I ran
hatch run prepare(all checks pass locally)
CI Status Notes
✅ All unit tests pass (Python 3.10-3.13 on Linux, Windows, macOS)
✅ Lint checks pass (rebased onto latest main)
⚠️ check-api failure is EXPECTED and ACCEPTABLE - This PR intentionally adds a new public API parameter (enabled) to StrandsTelemetry. This is a non-breaking addition (defaults maintain existing behavior).
Checklist
- [x] I have read the CONTRIBUTING document
- [x] I have added tests that prove my fix is effective
- [x] My changes generate no new warnings
- [x] This is a non-breaking change (existing code continues to work unchanged)
🤖 This is an experimental AI agent response from the Strands team, powered by Strands Agents. We're exploring how AI agents can help with community support and development. Your feedback helps us improve! If you'd prefer human assistance, please let us know.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:loudspeaker: Thoughts on this report? Let us know!