Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/azure/core/tracing/common.py: 32%

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

34 statements  

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

2# 

3# Copyright (c) Microsoft Corporation. All rights reserved. 

4# 

5# The MIT License (MIT) 

6# 

7# Permission is hereby granted, free of charge, to any person obtaining a copy 

8# of this software and associated documentation files (the ""Software""), to deal 

9# in the Software without restriction, including without limitation the rights 

10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

11# copies of the Software, and to permit persons to whom the Software is 

12# furnished to do so, subject to the following conditions: 

13# 

14# The above copyright notice and this permission notice shall be included in 

15# all copies or substantial portions of the Software. 

16# 

17# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

23# THE SOFTWARE. 

24# 

25# -------------------------------------------------------------------------- 

26"""Common functions shared by both the sync and the async decorators.""" 

27from contextlib import contextmanager 

28from typing import Any, Optional, Callable, Type, Generator 

29import warnings 

30 

31from ._abstract_span import AbstractSpan 

32from ..settings import settings 

33 

34 

35__all__ = [ 

36 "change_context", 

37 "with_current_context", 

38] 

39 

40 

41def get_function_and_class_name(func: Callable, *args: object) -> str: 

42 """ 

43 Given a function and its unamed arguments, returns class_name.function_name. It assumes the first argument 

44 is `self`. If there are no arguments then it only returns the function name. 

45 

46 :param func: the function passed in 

47 :type func: callable 

48 :param args: List of arguments passed into the function 

49 :type args: list 

50 :return: The function name with the class name 

51 :rtype: str 

52 """ 

53 try: 

54 return func.__qualname__ 

55 except AttributeError: 

56 if args: 

57 return "{}.{}".format(args[0].__class__.__name__, func.__name__) # pylint: disable=protected-access 

58 return func.__name__ 

59 

60 

61@contextmanager 

62def change_context(span: Optional[AbstractSpan]) -> Generator: 

63 """Execute this block inside the given context and restore it afterwards. 

64 

65 This does not start and ends the span, but just make sure all code is executed within 

66 that span. 

67 

68 If span is None, no-op. 

69 

70 :param span: A span 

71 :type span: AbstractSpan 

72 :rtype: contextmanager 

73 :return: A context manager that will run the given span in the new context 

74 """ 

75 span_impl_type: Optional[Type[AbstractSpan]] = settings.tracing_implementation() 

76 if span_impl_type is None or span is None: 

77 yield 

78 else: 

79 try: 

80 with span_impl_type.change_context(span): 

81 yield 

82 except AttributeError: 

83 # This plugin does not support "change_context" 

84 warnings.warn( 

85 'Your tracing plugin should be updated to support "change_context"', 

86 DeprecationWarning, 

87 ) 

88 original_span = span_impl_type.get_current_span() 

89 try: 

90 span_impl_type.set_current_span(span) 

91 yield 

92 finally: 

93 span_impl_type.set_current_span(original_span) 

94 

95 

96def with_current_context(func: Callable) -> Any: 

97 """Passes the current spans to the new context the function will be run in. 

98 

99 :param func: The function that will be run in the new context 

100 :type func: callable 

101 :return: The func wrapped with correct context 

102 :rtype: callable 

103 """ 

104 span_impl_type: Optional[Type[AbstractSpan]] = settings.tracing_implementation() 

105 if span_impl_type is None: 

106 return func 

107 

108 return span_impl_type.with_current_context(func)