forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
89 lines (73 loc) · 2.9 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
from typing import Optional
from functools import wraps
import inspect
import sentry_sdk
logger = logging.getLogger(__name__)
def sentry_traced(
func=None,
*,
transaction_name: Optional[str] = None,
op: Optional[str] = None,
):
"""
Function decorator to start a child under the existing current transaction or
under a new transaction, if it doesn't exist.
Args:
transaction_name: the name of the new transaction if no transaction already
exists. If a transaction is found in current scope, this name is ignored.
If no transaction is found and no transaction name is provided, no action
is taken.
op: the name of the child. Defaults to the decorated function name.
Returns:
a decorated function executing within a Sentry transaction child
Usage:
@sentry_traced
def my_function():
...
@sentry_traced(transaction_name="new_tx")
async def my_async_function():
...
@sentry_trace(op="child_name")
def my_function():
...
"""
def start_child_decorator(func):
def _transaction_and_op():
# Set transaction
transaction = sentry_sdk.Hub.current.scope.transaction
# If no current transaction we create one
if transaction_name and transaction is None:
transaction = sentry_sdk.start_transaction(name=transaction_name)
# Child name - defaults to the decorated function name
op_ = op or func.__name__
return transaction, op_
# Asynchronous case
if inspect.iscoroutinefunction(func):
@wraps(func)
async def func_with_tracing(*args, **kwargs):
transaction, op_ = _transaction_and_op()
# If no transaction, do nothing
if transaction is None:
return await func(*args, **kwargs)
# If we have a transaction, we decorate the function!
with transaction.start_child(op=op_):
return await func(*args, **kwargs)
# Synchronous case
else:
@wraps(func)
def func_with_tracing(*args, **kwargs):
transaction, op_ = _transaction_and_op()
# If no transaction, do nothing
if transaction is None:
return func(*args, **kwargs)
# If we have a transaction, we decorate the function!
with transaction.start_child(op=op_):
return func(*args, **kwargs)
return func_with_tracing
# This patterns allows usage of both @sentry_traced and @sentry_traced(...)
# See https://stackoverflow.com/questions/52126071/decorator-with-arguments-avoid-parenthesis-when-no-arguments/52126278
if func:
return start_child_decorator(func)
else:
return start_child_decorator