Skip to content

Commit cf6b995

Browse files
authored
[Core] Add tracing impl detection function caching (#43338)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent 93d8c26 commit cf6b995

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 1.35.2 (Unreleased)
3+
## 1.36.0 (Unreleased)
44

55
### Features Added
66

@@ -15,7 +15,8 @@
1515
### Other Changes
1616

1717
- Updated `BearerTokenCredentialPolicy` and `AsyncBearerTokenCredentialPolicy` to set the `enable_cae` parameter to `True` by default. This change enables Continuous Access Evaluation (CAE) for all token requests made through these policies. #42941
18-
- Removed `six` as a dependency since it was unused.
18+
- Removed `six` as a dependency since it was unused. #39962
19+
- Added caching to the tracing implementation detection function to prevent potential performance issues from repeated import attempts. #43338
1920

2021
## 1.35.1 (2025-09-11)
2122

sdk/core/azure-core/azure/core/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
# regenerated.
1010
# --------------------------------------------------------------------------
1111

12-
VERSION = "1.35.2"
12+
VERSION = "1.36.0"

sdk/core/azure-core/azure/core/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class ODataV4Format:
195195
}
196196
197197
:param dict json_object: A Python dict representing a ODataV4 JSON
198-
:ivar str ~.code: Its value is a service-defined error code.
198+
:ivar str code: Its value is a service-defined error code.
199199
This code serves as a sub-status for the HTTP error code specified in the response.
200200
:ivar str message: Human-readable, language-dependent representation of the error.
201201
:ivar str target: The target of the particular error (for example, the name of the property in error).

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from __future__ import annotations
2929
from collections import namedtuple
3030
from enum import Enum
31+
from functools import cache
3132
import logging
3233
import os
3334
from typing import (
@@ -207,6 +208,7 @@ def _get_opentelemetry_span() -> Optional[Type[AbstractSpan]]:
207208
}
208209

209210

211+
@cache
210212
def convert_tracing_impl(value: Optional[Union[str, Type[AbstractSpan]]]) -> Optional[Type[AbstractSpan]]:
211213
"""Convert a string to AbstractSpan
212214

sdk/core/azure-core/tests/test_settings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,32 @@ def test_convert_azure_cloud(self):
174174
with pytest.raises(ValueError):
175175
m.convert_azure_cloud(10)
176176

177+
def test_convert_tracing_impl_bad(self):
178+
with pytest.raises(ValueError):
179+
m.convert_tracing_impl("foo")
180+
181+
def test_convert_tracing_impl_caching(self):
182+
m.convert_tracing_impl.cache_clear()
183+
with patch.dict(
184+
m._tracing_implementation_dict,
185+
{
186+
"opentelemetry": MagicMock(wraps=m._get_opentelemetry_span),
187+
"opencensus": MagicMock(wraps=m._get_opencensus_span),
188+
},
189+
) as patched_dict:
190+
mock_method = patched_dict["opentelemetry"]
191+
impl1 = m.convert_tracing_impl("opentelemetry")
192+
impl2 = m.convert_tracing_impl("opentelemetry")
193+
assert impl1 is impl2
194+
assert mock_method.call_count == 1
195+
196+
mock_method = patched_dict["opencensus"]
197+
impl1 = m.convert_tracing_impl("opencensus")
198+
impl2 = m.convert_tracing_impl("opencensus")
199+
assert impl1 is impl2
200+
assert mock_method.call_count == 1
201+
m.convert_tracing_impl.cache_clear()
202+
177203

178204
_standard_settings = ["log_level", "tracing_enabled"]
179205

0 commit comments

Comments
 (0)