1
1
from __future__ import annotations
2
2
3
3
import logging
4
+ import sys
5
+ from collections .abc import Callable
4
6
from typing import Any , Sequence
5
7
6
8
import sentry_sdk
9
11
10
12
from asphalt .exceptions .api import ExceptionReporter
11
13
14
+ if sys .version_info >= (3 , 10 ):
15
+ from typing import TypeAlias
16
+ else :
17
+ from typing_extensions import TypeAlias
18
+
12
19
logger = logging .getLogger (__name__ )
13
20
21
+ Event : TypeAlias = "dict[str, Any]"
22
+ Hint : TypeAlias = "dict[str, Any]"
23
+ Breadcrumb : TypeAlias = "dict[str, Any]"
24
+ BreadcrumbHint : TypeAlias = "dict[str, Any]"
25
+ EventProcessor : TypeAlias = "Callable[[Event, Hint], Event | None]"
26
+ BreadcrumbProcessor : TypeAlias = "Callable[[Breadcrumb, BreadcrumbHint], Breadcrumb | None]"
27
+
14
28
15
29
class SentryExceptionReporter (ExceptionReporter ):
16
30
"""
@@ -31,7 +45,11 @@ class SentryExceptionReporter(ExceptionReporter):
31
45
should be a mapping of keyword arguments to their values.
32
46
33
47
The extras passed to this backend are passed to :func:`sentry_sdk.capture_exception` as keyword
34
- arguments.
48
+ arguments. Two such options have been special cased and can be looked up as a
49
+ ``module:varname`` reference:
50
+
51
+ - ``before_send``
52
+ - ``before_breadcrumb``
35
53
36
54
For more information, see the `Sentry SDK documentation`_.
37
55
@@ -40,8 +58,22 @@ class SentryExceptionReporter(ExceptionReporter):
40
58
"""
41
59
42
60
def __init__ (
43
- self , integrations : Sequence [Integration | dict [str , Any ]] = (), ** options
61
+ self ,
62
+ integrations : Sequence [Integration | dict [str , Any ]] = (),
63
+ before_send : EventProcessor | str | None = None ,
64
+ before_breadcrumb : BreadcrumbProcessor | str | None = None ,
65
+ ** options ,
44
66
) -> None :
67
+ if isinstance (before_send , str ):
68
+ _before_send : EventProcessor | None = resolve_reference (before_send )
69
+ else :
70
+ _before_send = before_send
71
+
72
+ if isinstance (before_breadcrumb , str ):
73
+ _before_breadcrumb : BreadcrumbProcessor | None = resolve_reference (before_breadcrumb )
74
+ else :
75
+ _before_breadcrumb = before_breadcrumb
76
+
45
77
options .setdefault ("environment" , "development" if __debug__ else "production" )
46
78
47
79
integrations_ : list [Integration ] = []
@@ -54,7 +86,12 @@ def __init__(
54
86
55
87
integrations_ .append (integration )
56
88
57
- sentry_sdk .init (integrations = integrations_ , ** options )
89
+ sentry_sdk .init (
90
+ integrations = integrations_ ,
91
+ before_send = _before_send ,
92
+ before_breadcrumb = _before_breadcrumb ,
93
+ ** options ,
94
+ )
58
95
59
96
def report_exception (
60
97
self ,
0 commit comments