29
29
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
31
31
import functools
32
+ from types import TracebackType
33
+ from typing import Optional , Type
32
34
33
35
from elasticapm .conf .constants import LABEL_RE
34
- from elasticapm .traces import DroppedSpan , capture_span , error_logger , execution_context
36
+ from elasticapm .traces import SpanType , capture_span , execution_context
35
37
from elasticapm .utils import get_name_from_func
36
38
37
39
@@ -46,34 +48,13 @@ async def decorated(*args, **kwds):
46
48
47
49
return decorated
48
50
49
- async def __aenter__ (self ):
50
- transaction = execution_context .get_transaction ()
51
- if transaction and transaction .is_sampled :
52
- return transaction .begin_span (
53
- self .name ,
54
- self .type ,
55
- context = self .extra ,
56
- leaf = self .leaf ,
57
- labels = self .labels ,
58
- span_subtype = self .subtype ,
59
- span_action = self .action ,
60
- sync = False ,
61
- start = self .start ,
62
- )
51
+ async def __aenter__ (self ) -> Optional [SpanType ]:
52
+ return self .handle_enter (False )
63
53
64
- async def __aexit__ (self , exc_type , exc_val , exc_tb ):
65
- transaction = execution_context .get_transaction ()
66
- if transaction and transaction .is_sampled :
67
- try :
68
- span = transaction .end_span (self .skip_frames )
69
- if exc_val and not isinstance (span , DroppedSpan ):
70
- try :
71
- exc_val ._elastic_apm_span_id = span .id
72
- except AttributeError :
73
- # could happen if the exception has __slots__
74
- pass
75
- except LookupError :
76
- error_logger .debug ("ended non-existing span %s of type %s" , self .name , self .type )
54
+ async def __aexit__ (
55
+ self , exc_type : Optional [Type [BaseException ]], exc_val : Optional [BaseException ], exc_tb : Optional [TracebackType ]
56
+ ):
57
+ self .handle_exit (exc_type , exc_val , exc_tb )
77
58
78
59
79
60
async def set_context (data , key = "custom" ):
0 commit comments