forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverless.py
More file actions
50 lines (39 loc) · 1.21 KB
/
serverless.py
File metadata and controls
50 lines (39 loc) · 1.21 KB
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
import functools
import sys
from sentry_sdk.hub import Hub
from sentry_sdk.utils import event_from_exception
from sentry_sdk._compat import reraise
def serverless_function(f=None, flush=True):
def wrapper(f):
@functools.wraps(f)
def inner(*args, **kwargs):
with Hub(Hub.current) as hub:
with hub.configure_scope() as scope:
scope.clear_breadcrumbs()
try:
return f(*args, **kwargs)
except Exception:
_capture_and_reraise()
finally:
if flush:
_flush_client()
return inner
if f is None:
return wrapper
else:
return wrapper(f)
def _capture_and_reraise():
exc_info = sys.exc_info()
hub = Hub.current
if hub is not None and hub.client is not None:
event, hint = event_from_exception(
exc_info,
client_options=hub.client.options,
mechanism={"type": "serverless", "handled": False},
)
hub.capture_event(event, hint=hint)
reraise(*exc_info)
def _flush_client():
hub = Hub.current
if hub is not None:
hub.flush()