Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/azure/core/instrumentation.py: 33%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

21 statements  

1# ------------------------------------ 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# ------------------------------------ 

5from typing import Optional, Union, Mapping, TYPE_CHECKING 

6from functools import lru_cache 

7 

8if TYPE_CHECKING: 

9 from .tracing.opentelemetry import OpenTelemetryTracer 

10 

11 

12def _get_tracer_impl(): 

13 # Check if OpenTelemetry is available/installed. 

14 try: 

15 from .tracing.opentelemetry import OpenTelemetryTracer 

16 

17 return OpenTelemetryTracer 

18 except ImportError: 

19 return None 

20 

21 

22@lru_cache 

23def _get_tracer_cached( 

24 library_name: Optional[str], 

25 library_version: Optional[str], 

26 schema_url: Optional[str], 

27 attributes_key: Optional[frozenset], 

28) -> Optional["OpenTelemetryTracer"]: 

29 tracer_impl = _get_tracer_impl() 

30 if tracer_impl: 

31 # Convert attributes_key back to dict if needed 

32 attributes = dict(attributes_key) if attributes_key else None 

33 return tracer_impl( 

34 library_name=library_name, 

35 library_version=library_version, 

36 schema_url=schema_url, 

37 attributes=attributes, 

38 ) 

39 return None 

40 

41 

42def get_tracer( 

43 *, 

44 library_name: Optional[str] = None, 

45 library_version: Optional[str] = None, 

46 schema_url: Optional[str] = None, 

47 attributes: Optional[Mapping[str, Union[str, bool, int, float]]] = None, 

48) -> Optional["OpenTelemetryTracer"]: 

49 """Get the OpenTelemetry tracer instance if available. 

50 

51 If OpenTelemetry is not available, this method will return None. This method caches 

52 the tracer instance for each unique set of parameters. 

53 

54 :keyword library_name: The name of the library to use in the tracer. 

55 :paramtype library_name: str 

56 :keyword library_version: The version of the library to use in the tracer. 

57 :paramtype library_version: str 

58 :keyword schema_url: Specifies the Schema URL of the emitted spans. Defaults to 

59 "https://opentelemetry.io/schemas/1.23.1". 

60 :paramtype schema_url: str 

61 :keyword attributes: Attributes to add to the emitted spans. 

62 :paramtype attributes: Mapping[str, Union[str, bool, int, float]] 

63 :return: The OpenTelemetry tracer instance if available. 

64 :rtype: Optional[~azure.core.tracing.opentelemetry.OpenTelemetryTracer] 

65 """ 

66 attributes_key = frozenset(attributes.items()) if attributes else None 

67 return _get_tracer_cached(library_name, library_version, schema_url, attributes_key)