Skip to content

Commit 080bc02

Browse files
szokeasaurusrexantonpirkerlizokmsentrivana
authored
docs(python): Improve before_send docs (#11289)
* docs(python): Improve `before_send` docs Improve the `before_send` Python docs by: - Making the docs specific to Python, rather than general to all languages - Using clearer, more precise language - Providing a more concrete example Also, move the content from `platform-includes/configuration/before-send/python.mdx` directly into `docs/platforms/python/configuration/filtering/index.mdx`, as it seems to only be used in that one place. Closes #8474 * Apply suggestions from code review Co-authored-by: Anton Pirker <[email protected]> * Update docs/platforms/python/configuration/filtering/index.mdx Co-authored-by: Liza Mock <[email protected]> * Update docs/platforms/python/configuration/filtering/index.mdx Co-authored-by: Ivana Kellyer <[email protected]> --------- Co-authored-by: Anton Pirker <[email protected]> Co-authored-by: Liza Mock <[email protected]> Co-authored-by: Ivana Kellyer <[email protected]>
1 parent 343ad0e commit 080bc02

File tree

2 files changed

+34
-17
lines changed
  • docs/platforms/python/configuration/filtering
  • platform-includes/configuration/before-send

2 files changed

+34
-17
lines changed

docs/platforms/python/configuration/filtering/index.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,41 @@ Configure your SDK to filter error events by using the <PlatformIdentifier name=
1616

1717
### Using <PlatformIdentifier name="before-send" />
1818

19-
All Sentry SDKs support the <PlatformIdentifier name="before-send" /> callback method. Because it's called immediately before the event is sent to the server, this is your last chance to decide not to send data or to edit it. <PlatformIdentifier name="before-send" /> receives the event object as a parameter, which you can use to either modify the event’s data or drop it completely by returning `null`, based on custom logic and the data available on the event.
19+
The `before_send` callback allows you to modify the event payload before the SDK sends the event to Sentry. You can also stop the event from being sent by returning `None`.
2020

21-
<PlatformContent includePath="configuration/before-send/" />
21+
The callback receives two arguments: the event payload and a [hint](#event-hints). The callback can return either the event payload to send to Sentry or `None` if the event should be dropped.
22+
23+
Any modifications to the event payload done in the callback, including adding data to the event and modifying or deleting existing event fields, will be reflected in Sentry.
24+
25+
#### Example `before_send`
26+
27+
Suppose that you wish prevent all errors of type `ZeroDivisionError` from being sent to Sentry and that you want to set an additional data attribute called `"foo"` to `"bar"` on all other events. You can achieve this behavior with the following `before_send` callback.
28+
29+
```python
30+
import sentry_sdk
31+
from sentry_sdk.types import Event, Hint
32+
33+
def my_before_send(event: Event, hint: Hint) -> Event:
34+
# Filter out all ZeroDivisionError events.
35+
# Note that the exception type is available in the hint,
36+
# but we should handle the case where the exception info
37+
# is missing.
38+
if hint.get("exc_info", [None])[0] == ZeroDivisionError:
39+
return None
40+
41+
# We can set extra data on the event's "extra" field.
42+
event["extra"]["foo"] = "bar"
43+
44+
# We have modified the event as desired, so return the event.
45+
# The SDK will then send the returned event to Sentry.
46+
return event
47+
48+
sentry_sdk.init(
49+
# ...
50+
51+
before_send=my_before_send,
52+
)
53+
```
2254

2355
Note also that breadcrumbs can be filtered, as discussed in [our Breadcrumbs documentation](/product/error-monitoring/breadcrumbs/).
2456

platform-includes/configuration/before-send/python.mdx

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)