Filtering and Sampling Events

Adding Sentry to your app gives you a great deal of very valuable information about errors and performance you wouldn't otherwise get. And lots of information is good -- as long as it's the right information, at a reasonable volume.

The Sentry SDKs have several configuration options to help you control this, allowing you to both filter out events you don't want and to take a representative sample of those you do.

Note:: The Sentry UI also offers methods to filter events, by using Inbound Filters. We recommend filtering at the client level though, because it removes the overhead of sending events you don't actually want.

Filtering Error Events

Configure your SDK to filter error events by using the beforeSend callback method and configuring, enabling, or disabling integrations.

Using beforeSend

All Sentry SDKs support the beforeSend callback method. beforeSend is called immediately before the event is sent to the server, so it’s the final place where you can edit its data. It receives the event object as a parameter, so you can use that to modify the event’s data or drop it completely (by returning null) based on custom logic and the data available on the event.

Copied
#include <sentry.h>

sentry_value_t strip_sensitive_data(sentry_value_t event, void *hint) {
  /* modify event here or return NULL to discard the event */
  return event;
}

int main(void) {
  sentry_options_t *options = sentry_options_new();
  sentry_options_set_before_send(options, strip_sensitive_data, NULL);
  sentry_init(options);

  /* ... */
}

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.

Note also that breadcrumbs can be filtered, as discussed in our Breadcrumbs documentation.

Event Hints

The before-send callback is passed both the event and a second argument, hint, that holds one or more hints.

Typically a hint holds the original exception so that additional data can be extracted or grouping is affected. In this example, the fingerprint is forced to a common value if an exception of a certain type has been caught:

Copied
#include <string.h>
#include <sentry.h>

sentry_value_t before_send(sentry_value_t event, void *hint) {
  /* sentry_value_t functions handle NULL automatically */
  sentry_value_t exceptions = sentry_value_get_by_key(event, "exception");
  sentry_value_t values = sentry_value_get_by_key(exceptions, "values");
  sentry_value_t exception = sentry_value_get_by_index(values, 0);
  sentry_value_t type = sentry_value_get_by_key(exception, "type");
  const char *type_str = sentry_value_as_string(type);

  /* use the data passed during initialization */
  const char *custom_error = (const char *)hint;

  if (type_str && strcmp(type_str, custom_error) == 0) {
    sentry_value_t fingerprint = sentry_value_new_list();
    sentry_value_append(fingerprint, sentry_value_new_string("custom-error"));
    sentry_value_set_by_key(event, "fingerprint", fingerprint);
  }

  return event;
}

int main(void) {
  sentry_options_t *options = sentry_options_new();
  sentry_options_set_before_send(options, before_send, (void *)"CustomError");
  sentry_init(options);

  /* ... */
}

When the SDK creates an event or breadcrumb for transmission, that transmission is typically created from some sort of source object. For instance, an error event is typically created from a log record or exception instance. For better customization, SDKs send these objects to certain callbacks (beforeSendbeforeBreadcrumb or the event processor system in the SDK).

Sampling Error Events

To send a representative sample of your errors to Sentry, set the sample_rate option in your SDK configuration to a number between 0 (0% of errors sent) and 1 (100% of errors sent). This is a static rate, which will apply equally to all errors. For example, to sample 25% of your errors:

Copied
sentry_options_t * options = sentry_options_new();
sentry_options_set_sample_rate(0.25);
sentry_init(options);

Note: The error sample rate is not dynamic; changing it requires re-deployment. In addition, setting an SDK sample rate limits visibility into the source of events. Setting a rate limit for your project (which only drops events when volume is high) may better suit your needs.

You can edit this page on GitHub.