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
3 changes: 3 additions & 0 deletions codecov.yml
Expand Up @@ -7,3 +7,6 @@ coverage:
python:
target: 90%
comment: false
ignore:
- "tests"
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved
- "sentry_sdk/_types.py"
1 change: 1 addition & 0 deletions sentry_sdk/_types.py
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
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", data_category="transaction"
)
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
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
74 changes: 73 additions & 1 deletion tests/test_basics.py
Expand Up @@ -91,7 +91,79 @@ 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):
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}


def test_option_before_send_discard(sentry_init, capture_events):
def before_send_discard(event, hint):
return None

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

sentry_init(before_send=before_send_discard)
events = capture_events()

do_this()

assert len(events) == 0


def test_option_before_send_transaction(sentry_init, capture_events):
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}


def test_option_before_send_transaction_discard(sentry_init, capture_events):
def before_send_transaction_discard(event, hint):
return None

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

assert len(events) == 0


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