1010import opentelemetry .trace as trace_api
1111from opentelemetry import propagate
1212from opentelemetry .baggage .propagation import W3CBaggagePropagator
13- from opentelemetry .exporter .otlp .proto .http .trace_exporter import OTLPSpanExporter
1413from opentelemetry .propagators .composite import CompositePropagator
1514from opentelemetry .sdk .resources import Resource
1615from opentelemetry .sdk .trace import TracerProvider as SDKTracerProvider
@@ -41,46 +40,57 @@ def get_otel_resource() -> Resource:
4140class StrandsTelemetry :
4241 """OpenTelemetry configuration and setup for Strands applications.
4342
44- It automatically initializes a tracer provider with text map propagators.
45- Trace exporters (console, OTLP) can be set up individually using dedicated methods.
43+ Automatically initializes a tracer provider with text map propagators.
44+ Trace exporters (console, OTLP) can be set up individually using dedicated methods
45+ that support method chaining for convenient configuration.
46+
47+ Args:
48+ tracer_provider: Optional pre-configured SDKTracerProvider. If None,
49+ a new one will be created and set as the global tracer provider.
4650
4751 Environment Variables:
4852 Environment variables are handled by the underlying OpenTelemetry SDK:
4953 - OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint URL
5054 - OTEL_EXPORTER_OTLP_HEADERS: Headers for OTLP requests
5155
52- Example:
53- Basic setup with console exporter:
54- >>> telemetry = StrandsTelemetry()
55- >>> telemetry.setup_console_exporter()
56+ Examples:
57+ Quick setup with method chaining:
58+ >>> StrandsTelemetry().setup_console_exporter().setup_otlp_exporter()
5659
57- Setup with OTLP exporter:
58- >>> telemetry = StrandsTelemetry()
59- >>> telemetry.setup_otlp_exporter()
60+ Using a custom tracer provider:
61+ >>> StrandsTelemetry(tracer_provider=my_provider).setup_console_exporter()
6062
61- Setup with both exporters :
63+ Step-by-step configuration :
6264 >>> telemetry = StrandsTelemetry()
6365 >>> telemetry.setup_console_exporter()
6466 >>> telemetry.setup_otlp_exporter()
6567
6668 Note:
67- The tracer provider is automatically initialized upon instantiation.
68- Exporters must be explicitly configured using the setup methods.
69- Failed exporter configurations are logged but do not raise exceptions.
69+ - The tracer provider is automatically initialized upon instantiation
70+ - When no tracer_provider is provided, the instance sets itself as the global provider
71+ - Exporters must be explicitly configured using the setup methods
72+ - Failed exporter configurations are logged but do not raise exceptions
73+ - All setup methods return self to enable method chaining
7074 """
7175
7276 def __init__ (
7377 self ,
78+ tracer_provider : SDKTracerProvider | None = None ,
7479 ) -> None :
7580 """Initialize the StrandsTelemetry instance.
7681
77- Automatically sets up the OpenTelemetry infrastructure.
82+ Args:
83+ tracer_provider: Optional pre-configured tracer provider.
84+ If None, a new one will be created and set as global.
7885
7986 The instance is ready to use immediately after initialization, though
8087 trace exporters must be configured separately using the setup methods.
8188 """
82- self .resource = get_otel_resource ()
83- self ._initialize_tracer ()
89+ if tracer_provider :
90+ self .tracer_provider = tracer_provider
91+ else :
92+ self .resource = get_otel_resource ()
93+ self ._initialize_tracer ()
8494
8595 def _initialize_tracer (self ) -> None :
8696 """Initialize the OpenTelemetry tracer."""
@@ -102,21 +112,25 @@ def _initialize_tracer(self) -> None:
102112 )
103113 )
104114
105- def setup_console_exporter (self ) -> None :
115+ def setup_console_exporter (self ) -> "StrandsTelemetry" :
106116 """Set up console exporter for the tracer provider."""
107117 try :
108118 logger .info ("enabling console export" )
109119 console_processor = SimpleSpanProcessor (ConsoleSpanExporter ())
110120 self .tracer_provider .add_span_processor (console_processor )
111121 except Exception as e :
112122 logger .exception ("error=<%s> | Failed to configure console exporter" , e )
123+ return self
113124
114- def setup_otlp_exporter (self ) -> None :
125+ def setup_otlp_exporter (self ) -> "StrandsTelemetry" :
115126 """Set up OTLP exporter for the tracer provider."""
127+ from opentelemetry .exporter .otlp .proto .http .trace_exporter import OTLPSpanExporter
128+
116129 try :
117130 otlp_exporter = OTLPSpanExporter ()
118131 batch_processor = BatchSpanProcessor (otlp_exporter )
119132 self .tracer_provider .add_span_processor (batch_processor )
120133 logger .info ("OTLP exporter configured" )
121134 except Exception as e :
122135 logger .exception ("error=<%s> | Failed to configure OTLP exporter" , e )
136+ return self
0 commit comments