Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before_send_transaction #1840

Merged
merged 8 commits into from
Jan 18, 2023
1 change: 1 addition & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
EventProcessor = Callable[[Event, Hint], Optional[Event]]
ErrorProcessor = Callable[[Event, ExcInfo], Optional[Event]]
BreadcrumbProcessor = Callable[[Breadcrumb, BreadcrumbHint], Optional[Breadcrumb]]
TransactionProcessor = Callable[[Event, Hint], Optional[Event]]

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

Expand Down
13 changes: 13 additions & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ def _prepare_event(
)
event = new_event # type: ignore

before_send_transaction = self.options["before_send_transaction"]
if before_send_transaction is not None and event.get("type") == "transaction":
new_event = None
with capture_internal_exceptions():
new_event = before_send_transaction(event, hint or {})
if new_event is None:
logger.info("before send transaction dropped event (%s)", event)
if self.transport:
self.transport.record_lost_event(
"before_send_transaction", data_category="error"
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
)
event = new_event # type: ignore

return event

def _is_ignored_error(self, event, hint):
Expand Down
2 changes: 2 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Event,
EventProcessor,
TracesSampler,
TransactionProcessor,
)

# Experiments are feature flags to enable and disable certain unstable SDK
Expand Down Expand Up @@ -117,6 +118,7 @@ def __init__(
_experiments={}, # type: Experiments # noqa: B006
proxy_headers=None, # type: Optional[Dict[str, str]]
instrumenter=INSTRUMENTER.SENTRY, # type: Optional[str]
before_send_transaction=None, # type: Optional[TransactionProcessor]
):
# type: (...) -> None
pass
Expand Down
60 changes: 59 additions & 1 deletion tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,65 @@ def test_event_id(sentry_init, capture_events):
assert Hub.current.last_event_id() == event_id


def test_option_callback(sentry_init, capture_events, monkeypatch):
def test_option_before_send(sentry_init, capture_events, monkeypatch):
def before_send(event, hint):
event["extra"] = {"before_send_called": True}
return event

def do_this():
try:
raise ValueError("aha!")
except Exception:
capture_exception()

sentry_init(before_send=before_send)
events = capture_events()

do_this()

(event,) = events
assert event["extra"] == {"before_send_called": True}

sentry_init()
events = capture_events()

do_this()

(event,) = events
assert "before_send_called" not in event["extra"]


def test_option_before_send_transaction(sentry_init, capture_events, monkeypatch):
def before_send_transaction(event, hint):
assert event["type"] == "transaction"
event["extra"] = {"before_send_transaction_called": True}
return event

sentry_init(
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
before_send_transaction=before_send_transaction,
traces_sample_rate=1.0,
)
events = capture_events()
transaction = start_transaction(name="foo")
transaction.finish()

(event,) = events
assert event["transaction"] == "foo"
assert event["extra"] == {"before_send_transaction_called": True}

sentry_init(
traces_sample_rate=1.0,
)
events = capture_events()
transaction = start_transaction(name="foo")
transaction.finish()

(event,) = events
assert event["transaction"] == "foo"
assert "before_send_transaction_called" not in event["extra"]


def test_option_before_breadcrumb(sentry_init, capture_events, monkeypatch):
drop_events = False
drop_breadcrumbs = False
reports = []
Expand Down