Skip to content

Commit 5321d0c

Browse files
authored
feat(native): Provide documentation for on_crash (#5355)
1 parent f341d2e commit 5321d0c

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/includes/configuration/before-send/native.mdx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,60 @@ int main(void) {
1717
1818
The callback is executed in the same thread as the call to `sentry_capture_event`. Work performed by the function may thus block the executing thread. For this reason, consider avoiding heavy work in `before_send`.
1919
20+
21+
#### Using `on_crash` (starting with version 0.4.19)
22+
23+
The `before_send` callback implementation in `sentry-native` makes it hard to distinguish between normal events
24+
and crashes. For this reason, we introduced another callback, `on_crash`, which - at this point - only exists
25+
in `sentry_native`:
26+
27+
```c
28+
#include <sentry.h>
29+
30+
static sentry_value_t
31+
crash_cleanup(
32+
const sentry_ucontext_t *uctx, // provides the user-space context of the crash
33+
sentry_value_t event, // used the same way as in `before_send`
34+
void *closure // user-data that you can provide at configuration time
35+
)
36+
{
37+
// Do contextual clean-up before the crash is sent to sentry's backend infrastructure
38+
39+
/* ... */
40+
41+
// tell the backend to retain the event (+ dump)
42+
// or to discard it, you could free the event and return a `null`:
43+
// sentry_value_decref(event);
44+
// return sentry_value_new_null();
45+
return event;
46+
}
47+
48+
int main(void) {
49+
sentry_options_t *options = sentry_options_new();
50+
sentry_options_set_on_crash(options, crash_cleanup, NULL);
51+
sentry_init(options);
52+
53+
/* ... */
54+
}
55+
```
56+
57+
The `on_crash` callback replaces `before_send` as a callback for crash events only. They can
58+
be defined simultaneously, where the SDK prevents `before_send` from being invoked for crash
59+
events. This allows for better differentiation between crashes and other events and
60+
gradual migration from existing `before_send` implementations:
61+
62+
- If you have a `before_send` implementation and do not define an `on_crash`
63+
callback `before_send` will receive both normal and crash events as before
64+
- If you only want to pre-process normal events with `before_send`, then
65+
you can define an "empty" `on_crash` callback that returns the
66+
passed-in event and does nothing else.
67+
- If you are not interested in pre-processing normal events but only want
68+
to act on crashes, then only define an `on_crash` callback with the option
69+
to filter (available for all backends) or enrich (only for `inproc`) the
70+
crash event.
71+
2072
<Alert level="warning" title="Not Supported in Crashpad on macOS">
2173

22-
The Crashpad backend on macOS doesn't currently support notifying the crashing process and thus can't correctly terminate sessions or call the registered `before_send` hook. It will also lose any events queued for sending at the time of the crash.
74+
The Crashpad backend on macOS doesn't currently support notifying the crashing process and thus can't correctly terminate sessions or call the registered `before_send` or `on_crash` hooks. It will also lose any events queued for sending at the time of the crash.
2375

2476
</Alert>

0 commit comments

Comments
 (0)