3131import logging
3232import os
3333import sys
34- from typing import Type , Optional , Callable , cast , Union , Dict , TYPE_CHECKING
34+ from typing import Type , Optional , Callable , cast , Union , Dict
3535from azure .core .tracing import AbstractSpan
3636
37- if TYPE_CHECKING :
38- try :
39- # pylint:disable=unused-import
40- from azure .core .tracing .ext .opencensus_span import (
41- OpenCensusSpan ,
42- ) # pylint:disable=redefined-outer-name
43- except ImportError :
44- pass
4537
4638__all__ = ("settings" , "Settings" )
4739
@@ -115,8 +107,8 @@ def convert_logging(value: Union[str, int]) -> int:
115107 return level
116108
117109
118- def get_opencensus_span () -> Optional [Type [AbstractSpan ]]:
119- """Returns the OpenCensusSpan if opencensus is installed else returns None"""
110+ def _get_opencensus_span () -> Optional [Type [AbstractSpan ]]:
111+ """Returns the OpenCensusSpan if the opencensus tracing plugin is installed else returns None"""
120112 try :
121113 from azure .core .tracing .ext .opencensus_span import ( # pylint:disable=redefined-outer-name
122114 OpenCensusSpan ,
@@ -127,14 +119,33 @@ def get_opencensus_span() -> Optional[Type[AbstractSpan]]:
127119 return None
128120
129121
130- def get_opencensus_span_if_opencensus_is_imported () -> Optional [Type [AbstractSpan ]]:
122+ def _get_opentelemetry_span () -> Optional [Type [AbstractSpan ]]:
123+ """Returns the OpenTelemetrySpan if the opentelemetry tracing plugin is installed else returns None"""
124+ try :
125+ from azure .core .tracing .ext .opentelemetry_span import ( # pylint:disable=redefined-outer-name
126+ OpenTelemetrySpan ,
127+ )
128+
129+ return OpenTelemetrySpan
130+ except ImportError :
131+ return None
132+
133+
134+ def _get_opencensus_span_if_opencensus_is_imported () -> Optional [Type [AbstractSpan ]]:
131135 if "opencensus" not in sys .modules :
132136 return None
133- return get_opencensus_span ()
137+ return _get_opencensus_span ()
138+
139+
140+ def _get_opentelemetry_span_if_opentelemetry_is_imported () -> Optional [Type [AbstractSpan ]]:
141+ if "opentelemetry" not in sys .modules :
142+ return None
143+ return _get_opentelemetry_span ()
134144
135145
136146_tracing_implementation_dict : Dict [str , Callable [[], Optional [Type [AbstractSpan ]]]] = {
137- "opencensus" : get_opencensus_span
147+ "opencensus" : _get_opencensus_span ,
148+ "opentelemetry" : _get_opentelemetry_span ,
138149}
139150
140151
@@ -145,6 +156,7 @@ def convert_tracing_impl(value: Union[str, Type[AbstractSpan]]) -> Optional[Type
145156 understands the following strings, ignoring case:
146157
147158 * "opencensus"
159+ * "opentelemetry"
148160
149161 :param value: the value to convert
150162 :type value: string
@@ -153,7 +165,9 @@ def convert_tracing_impl(value: Union[str, Type[AbstractSpan]]) -> Optional[Type
153165
154166 """
155167 if value is None :
156- return get_opencensus_span_if_opencensus_is_imported ()
168+ return (
169+ _get_opentelemetry_span_if_opentelemetry_is_imported () or _get_opencensus_span_if_opencensus_is_imported ()
170+ )
157171
158172 if not isinstance (value , str ):
159173 value = cast (Type [AbstractSpan ], value )
0 commit comments