strands.telemetry.config
OpenTelemetry configuration and setup utilities for Strands agents.
This module provides centralized configuration and initialization functionality for OpenTelemetry components and other telemetry infrastructure shared across Strands applications.
get_otel_resource
Section titled “get_otel_resource”def get_otel_resource() -> ResourceDefined in: src/strands/telemetry/config.py:27
Create a standard OpenTelemetry resource with service information.
Returns:
Resource object with standard service information.
StrandsTelemetry
Section titled “StrandsTelemetry”class StrandsTelemetry()Defined in: src/strands/telemetry/config.py:47
OpenTelemetry configuration and setup for Strands applications.
Automatically initializes a tracer provider with text map propagators. Trace exporters (console, OTLP) can be set up individually using dedicated methods that support method chaining for convenient configuration.
Arguments:
-
tracer_provider- Optional pre-configured SDKTracerProvider. If None, a new one will be created and set as the global tracer provider.Environment Variables: Environment variables are handled by the underlying OpenTelemetry SDK:
- OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint URL
- OTEL_EXPORTER_OTLP_HEADERS: Headers for OTLP requests
- OTEL_SERVICE_NAME: Overrides resource service name
Examples:
Quick setup with method chaining:
StrandsTelemetry().setup_console_exporter().setup_otlp_exporter()
Using a custom tracer provider:
StrandsTelemetry(tracer_provider=my_provider).setup_console_exporter()
Step-by-step configuration:
telemetry = StrandsTelemetry() telemetry.setup_console_exporter() telemetry.setup_otlp_exporter()
To setup global meter provider
telemetry.setup_meter(enable_console_exporter=True, enable_otlp_exporter=True) # default are False
Notes:
- The tracer provider is automatically initialized upon instantiation
- When no tracer_provider is provided, the instance sets itself as the global provider
- Exporters must be explicitly configured using the setup methods
- Failed exporter configurations are logged but do not raise exceptions
- All setup methods return self to enable method chaining
__init__
Section titled “__init__”def __init__(tracer_provider: SDKTracerProvider | None = None) -> NoneDefined in: src/strands/telemetry/config.py:87
Initialize the StrandsTelemetry instance.
Arguments:
-
tracer_provider- Optional pre-configured tracer provider. If None, a new one will be created and set as global.The instance is ready to use immediately after initialization, though trace exporters must be configured separately using the setup methods.
setup_console_exporter
Section titled “setup_console_exporter”def setup_console_exporter(**kwargs: Any) -> "StrandsTelemetry"Defined in: src/strands/telemetry/config.py:126
Set up console exporter for the tracer provider.
Arguments:
**kwargs- Optional keyword arguments passed directly to OpenTelemetry’s ConsoleSpanExporter initializer.
Returns:
-
self- Enables method chaining.This method configures a SimpleSpanProcessor with a ConsoleSpanExporter, allowing trace data to be output to the console. Any additional keyword arguments provided will be forwarded to the ConsoleSpanExporter.
setup_otlp_exporter
Section titled “setup_otlp_exporter”def setup_otlp_exporter(**kwargs: Any) -> "StrandsTelemetry"Defined in: src/strands/telemetry/config.py:148
Set up OTLP exporter for the tracer provider.
Arguments:
**kwargs- Optional keyword arguments passed directly to OpenTelemetry’s OTLPSpanExporter initializer.
Returns:
-
self- Enables method chaining.This method configures a BatchSpanProcessor with an OTLPSpanExporter, allowing trace data to be exported to an OTLP endpoint. Any additional keyword arguments provided will be forwarded to the OTLPSpanExporter.
setup_meter
Section titled “setup_meter”def setup_meter(enable_console_exporter: bool = False, enable_otlp_exporter: bool = False) -> "StrandsTelemetry"Defined in: src/strands/telemetry/config.py:173
Initialize the OpenTelemetry Meter.