2525import os
2626from abc import ABC , abstractmethod
2727from os import environ
28- from typing import Any , Callable , Mapping , Optional , Sequence , Type , Union
28+ from typing import (
29+ Any ,
30+ Callable ,
31+ Mapping ,
32+ MutableMapping ,
33+ Optional ,
34+ Sequence ,
35+ Type ,
36+ TypeVar ,
37+ Union ,
38+ )
2939
30- from grpc import ChannelCredentials # pylint: disable=import-error
31- from requests import Session
3240from typing_extensions import Literal
3341
3442from opentelemetry ._events import set_event_logger_provider
7179from opentelemetry .trace import set_tracer_provider
7280from opentelemetry .util ._importlib_metadata import entry_points
7381
82+ try :
83+ from grpc import ChannelCredentials
84+
85+ _GRPC_IMPORTED = True
86+ except ImportError :
87+ _GRPC_IMPORTED = False
88+
89+ try :
90+ from requests import Session
91+
92+ _REQUESTS_IMPORTED = True
93+ except ImportError :
94+ _REQUESTS_IMPORTED = False
95+
96+ T = TypeVar ("T" )
97+
7498_EXPORTER_OTLP = "otlp"
7599_EXPORTER_OTLP_PROTO_GRPC = "otlp_proto_grpc"
76100_EXPORTER_OTLP_PROTO_HTTP = "otlp_proto_http"
112136 Type [MetricReader ],
113137 Type [LogExporter ],
114138 ],
115- Mapping [str , Any ],
139+ MutableMapping [str , Any ],
116140]
117141
118142
119143def _load_credential_from_envvar (
120144 environment_variable : str ,
121145) -> Optional [
122146 tuple [
123- Literal ["credentials" , "session" ], Union [ChannelCredentials , Session ]
147+ Literal ["credentials" , "session" ],
148+ Union ["ChannelCredentials" , "Session" ],
124149 ]
125150]:
126151 credential_env = os .getenv (environment_variable )
127152 if credential_env :
128153 credentials = _import_config_component (
129154 credential_env , "opentelemetry_otlp_credential_provider"
130155 )()
131- if isinstance (credentials , ChannelCredentials ):
156+ if _GRPC_IMPORTED and isinstance (credentials , ChannelCredentials ):
132157 return ("credentials" , credentials )
133- elif isinstance (credentials , Session ):
158+
159+ if _REQUESTS_IMPORTED and isinstance (credentials , Session ):
134160 return ("session" , credentials )
135- else :
136- raise RuntimeError (
137- f" { credential_env } is neither a grpc.ChannelCredentials or requests.Session type."
138- )
161+ raise RuntimeError (
162+ f" { credential_env } is neither a grpc.ChannelCredentials or requests.Session type."
163+ )
164+ return None
139165
140166
141167def _import_config_component (
@@ -247,17 +273,15 @@ def _get_exporter_names(
247273
248274def _init_exporter (
249275 signal_type : Literal ["traces" , "metrics" , "logs" ],
250- exporter_args : Mapping [str , Any ],
251- exporter_class : Union [
252- Type [SpanExporter ], Type [MetricExporter ], Type [LogExporter ]
253- ],
276+ exporter_args : MutableMapping [str , Any ],
277+ exporter_class : Type [T ],
254278 otlp_credential_param_for_all_signal_types : Optional [
255279 tuple [
256280 Literal ["credentials" , "session" ],
257- Union [ChannelCredentials , Session ],
281+ Union [" ChannelCredentials" , " Session" ],
258282 ]
259283 ] = None ,
260- ) -> Union [ SpanExporter , MetricExporter , LogExporter ] :
284+ ) -> T :
261285 # Per signal type envvar should take precedence over all signal type env var.
262286 otlp_credential_param = (
263287 _load_credential_from_envvar (
@@ -273,6 +297,7 @@ def _init_exporter(
273297 ).parameters and (
274298 "opentelemetry.exporter.otlp.proto.http" in str (exporter_class )
275299 or "opentelemetry.exporter.otlp.proto.grpc" in str (exporter_class )
300+ or "tests.test_configurator" in str (exporter_class )
276301 ):
277302 exporter_args [credential_key ] = credential
278303 return exporter_class (** exporter_args )
@@ -285,7 +310,10 @@ def _init_tracing(
285310 resource : Resource | None = None ,
286311 exporter_args_map : ExporterArgsMap | None = None ,
287312 otlp_credential_param : Optional [
288- tuple [str , Union [ChannelCredentials , Session ]]
313+ tuple [
314+ Literal ["credentials" , "session" ],
315+ Union ["ChannelCredentials" , "Session" ],
316+ ]
289317 ] = None ,
290318):
291319 provider = TracerProvider (
@@ -314,10 +342,13 @@ def _init_metrics(
314342 exporters_or_readers : dict [
315343 str , Union [Type [MetricExporter ], Type [MetricReader ]]
316344 ],
317- resource : Resource = None ,
345+ resource : Resource | None = None ,
318346 exporter_args_map : ExporterArgsMap | None = None ,
319347 otlp_credential_param : Optional [
320- tuple [str , Union [ChannelCredentials , Session ]]
348+ tuple [
349+ Literal ["credentials" , "session" ],
350+ Union ["ChannelCredentials" , "Session" ],
351+ ]
321352 ] = None ,
322353):
323354 metric_readers = []
@@ -349,7 +380,10 @@ def _init_logging(
349380 setup_logging_handler : bool = True ,
350381 exporter_args_map : ExporterArgsMap | None = None ,
351382 otlp_credential_param : Optional [
352- tuple [str , Union [ChannelCredentials , Session ]]
383+ tuple [
384+ Literal ["credentials" , "session" ],
385+ Union ["ChannelCredentials" , "Session" ],
386+ ]
353387 ] = None ,
354388):
355389 provider = LoggerProvider (resource = resource )
@@ -510,7 +544,7 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator:
510544 raise RuntimeError (f"{ id_generator_name } is not an IdGenerator" )
511545
512546
513- def _initialize_components (
547+ def _initialize_components ( # pylint: disable=too-many-locals
514548 auto_instrumentation_version : str | None = None ,
515549 trace_exporter_names : list [str ] | None = None ,
516550 metric_exporter_names : list [str ] | None = None ,
0 commit comments