2626 Dict ,
2727 Generic ,
2828 List ,
29+ Literal ,
30+ NewType ,
2931 Optional ,
3032 Tuple ,
33+ Type ,
3134 TypeVar ,
3235 Union ,
3336)
5356from opentelemetry .exporter .otlp .proto .grpc import (
5457 _OTLP_GRPC_HEADERS ,
5558)
59+ from opentelemetry .proto .collector .logs .v1 .logs_service_pb2 import (
60+ ExportLogsServiceRequest ,
61+ )
62+ from opentelemetry .proto .collector .logs .v1 .logs_service_pb2_grpc import (
63+ LogsServiceStub ,
64+ )
65+ from opentelemetry .proto .collector .metrics .v1 .metrics_service_pb2 import (
66+ ExportMetricsServiceRequest ,
67+ )
68+ from opentelemetry .proto .collector .metrics .v1 .metrics_service_pb2_grpc import (
69+ MetricsServiceStub ,
70+ )
71+ from opentelemetry .proto .collector .trace .v1 .trace_service_pb2 import (
72+ ExportTraceServiceRequest ,
73+ )
74+ from opentelemetry .proto .collector .trace .v1 .trace_service_pb2_grpc import (
75+ TraceServiceStub ,
76+ )
5677from opentelemetry .proto .common .v1 .common_pb2 import ( # noqa: F401
5778 AnyValue ,
5879 ArrayValue ,
5980 KeyValue ,
6081)
6182from opentelemetry .proto .resource .v1 .resource_pb2 import Resource # noqa: F401
83+ from opentelemetry .sdk ._logs import LogData
84+ from opentelemetry .sdk ._logs .export import LogExportResult
6285from opentelemetry .sdk .environment_variables import (
6386 OTEL_EXPORTER_OTLP_CERTIFICATE ,
6487 OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE ,
6992 OTEL_EXPORTER_OTLP_INSECURE ,
7093 OTEL_EXPORTER_OTLP_TIMEOUT ,
7194)
72- from opentelemetry .sdk .metrics .export import MetricsData
95+ from opentelemetry .sdk .metrics .export import MetricExportResult , MetricsData
7396from opentelemetry .sdk .resources import Resource as SDKResource
7497from opentelemetry .sdk .trace import ReadableSpan
98+ from opentelemetry .sdk .trace .export import SpanExportResult
7599from opentelemetry .util .re import parse_env_headers
76100
77101logger = getLogger (__name__ )
78- SDKDataT = TypeVar ("SDKDataT" )
102+ SDKDataT = TypeVar (
103+ "SDKDataT" ,
104+ TypingSequence [LogData ],
105+ MetricsData ,
106+ TypingSequence [ReadableSpan ],
107+ )
79108ResourceDataT = TypeVar ("ResourceDataT" )
80109TypingResourceT = TypeVar ("TypingResourceT" )
81- ExportServiceRequestT = TypeVar ("ExportServiceRequestT" )
82- ExportResultT = TypeVar ("ExportResultT" )
110+ ExportServiceRequestT = TypeVar (
111+ "ExportServiceRequestT" ,
112+ ExportTraceServiceRequest ,
113+ ExportMetricsServiceRequest ,
114+ ExportLogsServiceRequest ,
115+ )
116+ ExportResultT = TypeVar (
117+ "ExportResultT" ,
118+ LogExportResult ,
119+ MetricExportResult ,
120+ SpanExportResult ,
121+ )
122+ ExportStubT = TypeVar (
123+ "ExportStubT" , TraceServiceStub , MetricsServiceStub , LogsServiceStub
124+ )
83125
84126_ENVIRON_TO_COMPRESSION = {
85127 None : None ,
@@ -102,7 +144,10 @@ def environ_to_compression(environ_key: str) -> Optional[Compression]:
102144 if environ_key in environ
103145 else None
104146 )
105- if environ_value not in _ENVIRON_TO_COMPRESSION :
147+ if (
148+ environ_value not in _ENVIRON_TO_COMPRESSION
149+ and environ_value is not None
150+ ):
106151 raise InvalidCompressionValueException (environ_key , environ_value )
107152 return _ENVIRON_TO_COMPRESSION [environ_value ]
108153
@@ -135,7 +180,7 @@ def _load_credentials(
135180 certificate_file : Optional [str ],
136181 client_key_file : Optional [str ],
137182 client_certificate_file : Optional [str ],
138- ) -> Optional [ ChannelCredentials ] :
183+ ) -> ChannelCredentials :
139184 root_certificates = (
140185 _read_file (certificate_file ) if certificate_file else None
141186 )
@@ -174,7 +219,7 @@ def _get_credentials(
174219
175220# pylint: disable=no-member
176221class OTLPExporterMixin (
177- ABC , Generic [SDKDataT , ExportServiceRequestT , ExportResultT ]
222+ ABC , Generic [SDKDataT , ExportServiceRequestT , ExportResultT , ExportStubT ]
178223):
179224 """OTLP span exporter
180225
@@ -189,6 +234,8 @@ class OTLPExporterMixin(
189234
190235 def __init__ (
191236 self ,
237+ stub : ExportStubT ,
238+ result : ExportResultT ,
192239 endpoint : Optional [str ] = None ,
193240 insecure : Optional [bool ] = None ,
194241 credentials : Optional [ChannelCredentials ] = None ,
@@ -199,7 +246,8 @@ def __init__(
199246 compression : Optional [Compression ] = None ,
200247 ):
201248 super ().__init__ ()
202-
249+ self ._result = result
250+ self ._stub = stub
203251 self ._endpoint = endpoint or environ .get (
204252 OTEL_EXPORTER_OTLP_ENDPOINT , "http://localhost:4317"
205253 )
@@ -208,15 +256,12 @@ def __init__(
208256
209257 if parsed_url .scheme == "https" :
210258 insecure = False
259+ insecure_exporter = environ .get (OTEL_EXPORTER_OTLP_INSECURE )
211260 if insecure is None :
212- insecure = environ .get (OTEL_EXPORTER_OTLP_INSECURE )
213- if insecure is not None :
214- insecure = insecure .lower () == "true"
261+ if insecure_exporter is not None :
262+ insecure = insecure_exporter .lower () == "true"
215263 else :
216- if parsed_url .scheme == "http" :
217- insecure = True
218- else :
219- insecure = False
264+ insecure = parsed_url .scheme == "http"
220265
221266 if parsed_url .netloc :
222267 self ._endpoint = parsed_url .netloc
@@ -257,25 +302,27 @@ def __init__(
257302 self ._channel = secure_channel (
258303 self ._endpoint , credentials , compression = compression
259304 )
260- self ._client = self ._stub (self ._channel )
305+ self ._client = self ._stub (self ._channel ) # pyright: ignore [reportCallIssue]
261306
262307 self ._export_lock = threading .Lock ()
263308 self ._shutdown = False
264309
265310 @abstractmethod
266311 def _translate_data (
267- self , data : TypingSequence [SDKDataT ]
312+ self ,
313+ data : SDKDataT ,
268314 ) -> ExportServiceRequestT :
269315 pass
270316
271317 def _export (
272- self , data : Union [TypingSequence [ReadableSpan ], MetricsData ]
318+ self ,
319+ data : SDKDataT ,
273320 ) -> ExportResultT :
274321 # After the call to shutdown, subsequent calls to Export are
275322 # not allowed and should return a Failure result.
276323 if self ._shutdown :
277324 logger .warning ("Exporter already shutdown, ignoring batch" )
278- return self ._result .FAILURE
325+ return self ._result .FAILURE # pyright: ignore [reportReturnType]
279326
280327 # FIXME remove this check if the export type for traces
281328 # gets updated to a class that represents the proto
@@ -292,7 +339,7 @@ def _export(
292339 # value will remain constant.
293340 for delay in _create_exp_backoff_generator (max_value = max_value ):
294341 if delay == max_value or self ._shutdown :
295- return self ._result .FAILURE
342+ return self ._result .FAILURE # pyright: ignore [reportReturnType]
296343
297344 with self ._export_lock :
298345 try :
@@ -302,10 +349,11 @@ def _export(
302349 timeout = self ._timeout ,
303350 )
304351
305- return self ._result .SUCCESS
352+ return self ._result .SUCCESS # pyright: ignore [reportReturnType]
306353
307354 except RpcError as error :
308- if error .code () in [
355+ code = error .code () # pyright: ignore [reportAttributeAccessIssue]
356+ if code in [
309357 StatusCode .CANCELLED ,
310358 StatusCode .DEADLINE_EXCEEDED ,
311359 StatusCode .RESOURCE_EXHAUSTED ,
@@ -314,7 +362,7 @@ def _export(
314362 StatusCode .UNAVAILABLE ,
315363 StatusCode .DATA_LOSS ,
316364 ]:
317- retry_info_bin = dict (error .trailing_metadata ()).get (
365+ retry_info_bin = dict (error .trailing_metadata ()).get ( # pyright: ignore [reportAttributeAccessIssue]
318366 "google.rpc.retryinfo-bin"
319367 )
320368 if retry_info_bin is not None :
@@ -330,7 +378,7 @@ def _export(
330378 "Transient error %s encountered while exporting "
331379 "%s to %s, retrying in %ss."
332380 ),
333- error . code () ,
381+ code ,
334382 self ._exporting ,
335383 self ._endpoint ,
336384 delay ,
@@ -342,16 +390,16 @@ def _export(
342390 "Failed to export %s to %s, error code: %s" ,
343391 self ._exporting ,
344392 self ._endpoint ,
345- error . code () ,
346- exc_info = error . code () == StatusCode .UNKNOWN ,
393+ code ,
394+ exc_info = code == StatusCode .UNKNOWN ,
347395 )
348396
349- if error . code () == StatusCode .OK :
350- return self ._result .SUCCESS
397+ if code == StatusCode .OK :
398+ return self ._result .SUCCESS # pyright: ignore [reportReturnType]
351399
352- return self ._result .FAILURE
400+ return self ._result .FAILURE # pyright: ignore [reportReturnType]
353401
354- return self ._result .FAILURE
402+ return self ._result .FAILURE # pyright: ignore [reportReturnType]
355403
356404 def shutdown (self , timeout_millis : float = 30_000 , ** kwargs ) -> None :
357405 if self ._shutdown :
0 commit comments