1515from __future__ import annotations
1616
1717import logging
18- from typing import TYPE_CHECKING , Dict , Optional
18+ from typing import Dict , Optional
1919
2020from opentelemetry import trace
2121from opentelemetry .sdk ._configuration ._exceptions import ConfigurationError
22- from opentelemetry .sdk ._configuration .models import (
23- BatchSpanProcessor as BatchSpanProcessorConfig ,
24- )
2522from opentelemetry .sdk ._configuration .models import (
2623 OtlpGrpcExporter as OtlpGrpcExporterConfig ,
2724)
3431from opentelemetry .sdk ._configuration .models import (
3532 Sampler as SamplerConfig ,
3633)
37- from opentelemetry .sdk ._configuration .models import (
38- SimpleSpanProcessor as SimpleSpanProcessorConfig ,
39- )
4034from opentelemetry .sdk ._configuration .models import (
4135 SpanExporter as SpanExporterConfig ,
4236)
5145)
5246from opentelemetry .sdk .resources import Resource
5347from opentelemetry .sdk .trace import (
54- SpanLimits ,
55- TracerProvider ,
5648 _DEFAULT_OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT ,
5749 _DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT ,
5850 _DEFAULT_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT ,
5951 _DEFAULT_OTEL_SPAN_EVENT_COUNT_LIMIT ,
6052 _DEFAULT_OTEL_SPAN_LINK_COUNT_LIMIT ,
53+ SpanLimits ,
54+ TracerProvider ,
55+ )
56+ from opentelemetry .sdk .trace .export import (
57+ BatchSpanProcessor ,
58+ ConsoleSpanExporter ,
59+ SimpleSpanProcessor ,
60+ SpanExporter ,
6161)
6262from opentelemetry .sdk .trace .sampling import (
6363 ALWAYS_OFF ,
6767 TraceIdRatioBased ,
6868)
6969
70- if TYPE_CHECKING :
71- from opentelemetry .sdk .trace .export import SpanExporter
72-
7370_logger = logging .getLogger (__name__ )
7471
7572# Default sampler per the OTel spec: parent_based with always_on root.
@@ -105,51 +102,58 @@ def _parse_headers(
105102 return result
106103
107104
108- def _create_otlp_http_span_exporter (config : OtlpHttpExporterConfig ) -> "SpanExporter" :
105+ def _create_otlp_http_span_exporter (
106+ config : OtlpHttpExporterConfig ,
107+ ) -> SpanExporter :
109108 """Create an OTLP HTTP span exporter from config."""
110109 try :
111- from opentelemetry .exporter .otlp .proto .http .trace_exporter import ( # type: ignore[import-untyped]
112- OTLPSpanExporter ,
113- )
114- from opentelemetry .exporter .otlp .proto .http import ( # type: ignore[import-untyped]
110+ # pylint: disable=import-outside-toplevel,no-name-in-module
111+ from opentelemetry .exporter .otlp .proto .http import ( # type: ignore[import-untyped] # noqa: PLC0415
115112 Compression ,
116113 )
114+ from opentelemetry .exporter .otlp .proto .http .trace_exporter import ( # type: ignore[import-untyped] # noqa: PLC0415
115+ OTLPSpanExporter ,
116+ )
117117 except ImportError as exc :
118118 raise ConfigurationError (
119119 "otlp_http span exporter requires 'opentelemetry-exporter-otlp-proto-http'. "
120120 "Install it with: pip install opentelemetry-exporter-otlp-proto-http"
121121 ) from exc
122122
123- compression = _map_compression_http (config .compression , Compression )
123+ compression = _map_compression (config .compression , Compression )
124124 headers = _parse_headers (config .headers , config .headers_list )
125125 timeout = (config .timeout / 1000.0 ) if config .timeout is not None else None
126126
127- return OTLPSpanExporter (
127+ return OTLPSpanExporter ( # type: ignore[return-value]
128128 endpoint = config .endpoint ,
129129 headers = headers ,
130130 timeout = timeout ,
131- compression = compression ,
131+ compression = compression , # type: ignore[arg-type]
132132 )
133133
134134
135- def _map_compression_http (
135+ def _map_compression (
136136 value : Optional [str ], compression_enum : type
137137) -> Optional [object ]:
138- """Map a compression string to the HTTP Compression enum value."""
138+ """Map a compression string to the given Compression enum value."""
139139 if value is None or value .lower () == "none" :
140140 return None
141141 if value .lower () == "gzip" :
142- return compression_enum .Gzip
142+ return compression_enum .Gzip # type: ignore[attr-defined]
143143 raise ConfigurationError (
144144 f"Unsupported compression value '{ value } '. Supported values: 'gzip', 'none'."
145145 )
146146
147147
148- def _create_otlp_grpc_span_exporter (config : OtlpGrpcExporterConfig ) -> "SpanExporter" :
148+ def _create_otlp_grpc_span_exporter (
149+ config : OtlpGrpcExporterConfig ,
150+ ) -> SpanExporter :
149151 """Create an OTLP gRPC span exporter from config."""
150152 try :
151- import grpc # type: ignore[import-untyped]
152- from opentelemetry .exporter .otlp .proto .grpc .trace_exporter import ( # type: ignore[import-untyped]
153+ # pylint: disable=import-outside-toplevel,no-name-in-module
154+ import grpc # type: ignore[import-untyped] # noqa: PLC0415
155+
156+ from opentelemetry .exporter .otlp .proto .grpc .trace_exporter import ( # type: ignore[import-untyped] # noqa: PLC0415
153157 OTLPSpanExporter ,
154158 )
155159 except ImportError as exc :
@@ -158,79 +162,55 @@ def _create_otlp_grpc_span_exporter(config: OtlpGrpcExporterConfig) -> "SpanExpo
158162 "Install it with: pip install opentelemetry-exporter-otlp-proto-grpc"
159163 ) from exc
160164
161- compression = _map_compression_grpc (config .compression , grpc )
165+ compression = _map_compression (config .compression , grpc . Compression )
162166 headers = _parse_headers (config .headers , config .headers_list )
163167 timeout = (config .timeout / 1000.0 ) if config .timeout is not None else None
164168
165- return OTLPSpanExporter (
169+ return OTLPSpanExporter ( # type: ignore[return-value]
166170 endpoint = config .endpoint ,
167171 headers = headers ,
168172 timeout = timeout ,
169- compression = compression ,
170- )
171-
172-
173- def _map_compression_grpc (
174- value : Optional [str ], grpc_module : object
175- ) -> Optional [object ]:
176- """Map a compression string to the gRPC Compression enum value."""
177- if value is None or value .lower () == "none" :
178- return None
179- if value .lower () == "gzip" :
180- return grpc_module .Compression .Gzip # type: ignore[attr-defined]
181- raise ConfigurationError (
182- f"Unsupported compression value '{ value } '. Supported values: 'gzip', 'none'."
173+ compression = compression , # type: ignore[arg-type]
183174 )
184175
185176
186- def _create_span_exporter (config : SpanExporterConfig ) -> " SpanExporter" :
177+ def _create_span_exporter (config : SpanExporterConfig ) -> SpanExporter :
187178 """Create a span exporter from config."""
188179 if config .otlp_http is not None :
189180 return _create_otlp_http_span_exporter (config .otlp_http )
190181 if config .otlp_grpc is not None :
191182 return _create_otlp_grpc_span_exporter (config .otlp_grpc )
192183 if config .console is not None :
193- from opentelemetry .sdk .trace .export import ConsoleSpanExporter
194-
195184 return ConsoleSpanExporter ()
196185 raise ConfigurationError (
197186 "No exporter type specified in span exporter config. "
198187 "Supported types: otlp_http, otlp_grpc, console."
199188 )
200189
201190
202- def _create_span_processor (config : SpanProcessorConfig ) -> object :
191+ def _create_span_processor (
192+ config : SpanProcessorConfig ,
193+ ) -> BatchSpanProcessor | SimpleSpanProcessor :
203194 """Create a span processor from config."""
204- from opentelemetry .sdk .trace .export import (
205- BatchSpanProcessor ,
206- SimpleSpanProcessor ,
207- )
208-
209195 if config .batch is not None :
210- return _create_batch_span_processor (config .batch , BatchSpanProcessor )
196+ exporter = _create_span_exporter (config .batch .exporter )
197+ return BatchSpanProcessor (
198+ exporter ,
199+ max_queue_size = config .batch .max_queue_size ,
200+ schedule_delay_millis = config .batch .schedule_delay ,
201+ max_export_batch_size = config .batch .max_export_batch_size ,
202+ export_timeout_millis = config .batch .export_timeout ,
203+ )
211204 if config .simple is not None :
212- exporter = _create_span_exporter (config .simple .exporter )
213- return SimpleSpanProcessor (exporter )
205+ return SimpleSpanProcessor (
206+ _create_span_exporter (config .simple .exporter )
207+ )
214208 raise ConfigurationError (
215209 "No processor type specified in span processor config. "
216210 "Supported types: batch, simple."
217211 )
218212
219213
220- def _create_batch_span_processor (
221- config : BatchSpanProcessorConfig , batch_cls : type
222- ) -> object :
223- """Build a BatchSpanProcessor from config."""
224- exporter = _create_span_exporter (config .exporter )
225- return batch_cls (
226- exporter ,
227- max_queue_size = config .max_queue_size ,
228- schedule_delay_millis = config .schedule_delay ,
229- max_export_batch_size = config .max_export_batch_size ,
230- export_timeout_millis = config .export_timeout ,
231- )
232-
233-
234214def _create_sampler (config : SamplerConfig ) -> Sampler :
235215 """Create a sampler from config."""
236216 if config .always_on is not None :
@@ -250,8 +230,10 @@ def _create_sampler(config: SamplerConfig) -> Sampler:
250230
251231def _create_parent_based_sampler (config : ParentBasedSamplerConfig ) -> Sampler :
252232 """Create a ParentBased sampler from config, applying SDK defaults for absent delegates."""
253- root = _create_sampler (config .root ) if config .root is not None else ALWAYS_ON
254- kwargs = {"root" : root }
233+ root = (
234+ _create_sampler (config .root ) if config .root is not None else ALWAYS_ON
235+ )
236+ kwargs : dict = {"root" : root }
255237 if config .remote_parent_sampled is not None :
256238 kwargs ["remote_parent_sampled" ] = _create_sampler (
257239 config .remote_parent_sampled
@@ -268,7 +250,7 @@ def _create_parent_based_sampler(config: ParentBasedSamplerConfig) -> Sampler:
268250 kwargs ["local_parent_not_sampled" ] = _create_sampler (
269251 config .local_parent_not_sampled
270252 )
271- return ParentBased (** kwargs ) # type: ignore[arg-type]
253+ return ParentBased (** kwargs )
272254
273255
274256def _create_span_limits (config : SpanLimitsConfig ) -> SpanLimits :
@@ -303,9 +285,7 @@ def _create_span_limits(config: SpanLimitsConfig) -> SpanLimits:
303285 if config .link_attribute_count_limit is not None
304286 else _DEFAULT_OTEL_LINK_ATTRIBUTE_COUNT_LIMIT
305287 ),
306- max_attribute_length = (
307- config .attribute_value_length_limit
308- ),
288+ max_attribute_length = config .attribute_value_length_limit ,
309289 )
310290
311291
@@ -351,9 +331,7 @@ def create_tracer_provider(
351331
352332 if config is not None :
353333 for proc_config in config .processors :
354- provider .add_span_processor ( # type: ignore[arg-type]
355- _create_span_processor (proc_config )
356- )
334+ provider .add_span_processor (_create_span_processor (proc_config ))
357335
358336 return provider
359337
0 commit comments