Skip to content

Commit

Permalink
feat: Allow disabling of the crash-handler backend at runtime. (#717)
Browse files Browse the repository at this point in the history
Co-authored-by: Mischan Toosarani-Hausberger <[email protected]>
  • Loading branch information
supervacuus and supervacuus authored May 20, 2022
1 parent 0c52b8f commit 0a498e3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ The example currently supports the following commends:
- `capture-multiple`: Captures a number of events.
- `sleep`: Introduces a 10 second sleep.
- `add-stacktrace`: Adds the current thread stacktrace to the captured event.
- `disable-backend`: Disables the build-configured crash-handler backend.
4 changes: 4 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ main(int argc, char **argv)
{
sentry_options_t *options = sentry_options_new();

if (has_arg(argc, argv, "disable-backend")) {
sentry_options_set_backend(options, NULL);
}

// this is an example. for real usage, make sure to set this explicitly to
// an app specific cache location.
sentry_options_set_database_path(options, ".sentry-native");
Expand Down
24 changes: 24 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,20 @@ SENTRY_API void sentry_transport_free(sentry_transport_t *transport);
SENTRY_API sentry_transport_t *sentry_new_function_transport(
void (*func)(const sentry_envelope_t *envelope, void *data), void *data);

/**
* This represents an interface for user-defined backends.
*
* Backends are responsible to handle crashes. They are maintained at runtime
* via various life-cycle hooks from the sentry-core.
*
* At this point none of those interfaces are exposed in the API including
* creation and destruction. The main use-case of the backend in the API at this
* point is to disable it via `sentry_options_set_backend` at runtime before it
* is initialized.
*/
struct sentry_backend_s;
typedef struct sentry_backend_s sentry_backend_t;

/* -- Options APIs -- */

/**
Expand Down Expand Up @@ -1050,6 +1064,16 @@ SENTRY_API void sentry_options_set_shutdown_timeout(
*/
SENTRY_API uint64_t sentry_options_get_shutdown_timeout(sentry_options_t *opts);

/**
* Sets a user-defined backend.
*
* Since creation and destruction of backends is not exposed in the API, this
* can only be used to set the backend to `NULL`, which disables the backend in
* the initialization.
*/
SENTRY_API void sentry_options_set_backend(
sentry_options_t *opts, sentry_backend_t *backend);

/* -- Global APIs -- */

/**
Expand Down
7 changes: 7 additions & 0 deletions src/sentry_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,10 @@ sentry_options_get_traces_sample_rate(sentry_options_t *opts)
{
return opts->traces_sample_rate;
}

void
sentry_options_set_backend(sentry_options_t *opts, sentry_backend_t *backend)
{
sentry__backend_free(opts->backend);
opts->backend = backend;
}
21 changes: 21 additions & 0 deletions tests/test_integration_crashpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,24 @@ def test_crashpad_dump_inflight(cmake, httpserver):

# we trigger 10 normal events, and 1 crash
assert len(httpserver.log) >= 11


def test_disable_backend(cmake, httpserver):
tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "crashpad"})

env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver))

with httpserver.wait(timeout=5, raise_assertions=False) as waiting:
child = run(
tmp_path, "sentry_example", ["disable-backend", "log", "crash"], env=env
)
# we crash so process should return non-zero
assert child.returncode

# crashpad is disabled, and we are only crashing, so we expect the wait to timeout
assert waiting.result is False

run(tmp_path, "sentry_example", ["log", "no-setup"], check=True, env=env)

# crashpad is disabled, and we are only crashing, so we expect no requests
assert len(httpserver.log) == 0

0 comments on commit 0a498e3

Please sign in to comment.