Skip to content

Commit 5bd6b6f

Browse files
authored
[Core][OTel] Update settings to include opentelemetry (Azure#29095)
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
1 parent 10d6d41 commit 5bd6b6f

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

sdk/core/azure-core-tracing-opentelemetry/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ with azure-core tracing. This includes (not exhaustive list), azure-storage-blob
2525
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
2626
settings.tracing_implementation = OpenTelemetrySpan
2727
```
28+
* Alternatively, if you have the latest version of `azure-core` installed, you can also set the following environment variable to
29+
enable tracing with OpenTelemetry:
30+
31+
```bash
32+
AZURE_SDK_TRACING_IMPLEMENTATION=opentelemetry
33+
```
2834

2935
## Examples
3036

sdk/core/azure-core/azure/core/settings.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@
3131
import logging
3232
import os
3333
import sys
34-
from typing import Type, Optional, Callable, cast, Union, Dict, TYPE_CHECKING
34+
from typing import Type, Optional, Callable, cast, Union, Dict
3535
from 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

Comments
 (0)