Skip to content

Commit 3f38f79

Browse files
Add before_send_transaction (#1840)
* Added before_send_transaction Co-authored-by: Neel Shah <[email protected]>
1 parent 43ca991 commit 3f38f79

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ coverage:
77
python:
88
target: 90%
99
comment: false
10+
ignore:
11+
- "tests"
12+
- "sentry_sdk/_types.py"

sentry_sdk/_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
EventProcessor = Callable[[Event, Hint], Optional[Event]]
3131
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
3232
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]
33+
TransactionProcessor = Callable[[Event, Hint], Optional[Event]]
3334

3435
TracesSampler = Callable[[SamplingContext], Union[float, int, bool]]
3536

sentry_sdk/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,19 @@ def _prepare_event(
248248
)
249249
event = new_event # type: ignore
250250

251+
before_send_transaction = self.options["before_send_transaction"]
252+
if before_send_transaction is not None and event.get("type") == "transaction":
253+
new_event = None
254+
with capture_internal_exceptions():
255+
new_event = before_send_transaction(event, hint or {})
256+
if new_event is None:
257+
logger.info("before send transaction dropped event (%s)", event)
258+
if self.transport:
259+
self.transport.record_lost_event(
260+
"before_send", data_category="transaction"
261+
)
262+
event = new_event # type: ignore
263+
251264
return event
252265

253266
def _is_ignored_error(self, event, hint):

sentry_sdk/consts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Event,
2121
EventProcessor,
2222
TracesSampler,
23+
TransactionProcessor,
2324
)
2425

2526
# Experiments are feature flags to enable and disable certain unstable SDK
@@ -117,6 +118,7 @@ def __init__(
117118
_experiments={}, # type: Experiments # noqa: B006
118119
proxy_headers=None, # type: Optional[Dict[str, str]]
119120
instrumenter=INSTRUMENTER.SENTRY, # type: Optional[str]
121+
before_send_transaction=None, # type: Optional[TransactionProcessor]
120122
):
121123
# type: (...) -> None
122124
pass

tests/test_basics.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,79 @@ def test_event_id(sentry_init, capture_events):
9191
assert Hub.current.last_event_id() == event_id
9292

9393

94-
def test_option_callback(sentry_init, capture_events, monkeypatch):
94+
def test_option_before_send(sentry_init, capture_events):
95+
def before_send(event, hint):
96+
event["extra"] = {"before_send_called": True}
97+
return event
98+
99+
def do_this():
100+
try:
101+
raise ValueError("aha!")
102+
except Exception:
103+
capture_exception()
104+
105+
sentry_init(before_send=before_send)
106+
events = capture_events()
107+
108+
do_this()
109+
110+
(event,) = events
111+
assert event["extra"] == {"before_send_called": True}
112+
113+
114+
def test_option_before_send_discard(sentry_init, capture_events):
115+
def before_send_discard(event, hint):
116+
return None
117+
118+
def do_this():
119+
try:
120+
raise ValueError("aha!")
121+
except Exception:
122+
capture_exception()
123+
124+
sentry_init(before_send=before_send_discard)
125+
events = capture_events()
126+
127+
do_this()
128+
129+
assert len(events) == 0
130+
131+
132+
def test_option_before_send_transaction(sentry_init, capture_events):
133+
def before_send_transaction(event, hint):
134+
assert event["type"] == "transaction"
135+
event["extra"] = {"before_send_transaction_called": True}
136+
return event
137+
138+
sentry_init(
139+
before_send_transaction=before_send_transaction,
140+
traces_sample_rate=1.0,
141+
)
142+
events = capture_events()
143+
transaction = start_transaction(name="foo")
144+
transaction.finish()
145+
146+
(event,) = events
147+
assert event["transaction"] == "foo"
148+
assert event["extra"] == {"before_send_transaction_called": True}
149+
150+
151+
def test_option_before_send_transaction_discard(sentry_init, capture_events):
152+
def before_send_transaction_discard(event, hint):
153+
return None
154+
155+
sentry_init(
156+
before_send_transaction=before_send_transaction_discard,
157+
traces_sample_rate=1.0,
158+
)
159+
events = capture_events()
160+
transaction = start_transaction(name="foo")
161+
transaction.finish()
162+
163+
assert len(events) == 0
164+
165+
166+
def test_option_before_breadcrumb(sentry_init, capture_events, monkeypatch):
95167
drop_events = False
96168
drop_breadcrumbs = False
97169
reports = []

0 commit comments

Comments
 (0)