Filtering events

In case you want to run custom logic on all events, you can do so in the before-send callback:

In Python a function can be used to modify the event or return a completely new one. If you return None, the event will be discarded.

import sentry_sdk

def strip_sensitive_data(event, hint):
    # modify event here
    return event

sentry_sdk.init(
    before_send=strip_sensitive_data
)

In JavaScript a function can be used to modify the event or return a completely new one. If you return null, the event will be discarded.

Sentry.init({
  beforeSend(event) {
    // Modify the event here
    if (event.user) {
      // Don't send user's email address
      delete event.user.email;
    }
    return event;
  }
});

A Func<SentryEvent, SentryEvent> can be used to mutate, discard (return null), or return a completely new event.

using Sentry;

SentrySdk.Init(o =>
{
    o.BeforeSend = sentryEvent =>
    {
        // Modify the event here:
        sentryEvent.ServerName = null; // Don't send server names.
        return sentryEvent;
    };
});
sentry::init(sentry::ClientOptions {
    before_send: Some(Arc::new(Box::new(|mut event| {
        // Modify event here
        event.server_name = None;  // Don't send server name
        Some(event)
    }))),
    ..Default::default()
});

Filtering breadcrumbs

Breadcrumbs can also be individually filtered, see Breadcrumbs.

Event hints

When an SDK creates an event or breadcrumb for transmission that 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 (before-send, before-breadcrumb or the event processor system in the SDKs).

Before Send

The before-send callback is passed the event and a second argument hint which holds one or more hints. Typically this hint holds the original exception so that additional data can be extracted or grouping be affected.

In this example the fingerprint is forced to a common value if an exception of a certain type has been caught:

import sentry_sdk

def before_send(event, hint):
    if 'exc_info' in hint:
        exc_type, exc_value, tb = hint['exc_info']
        if isinstance(exc_value, DatabaseUnavailable):
            event['fingerprint'] = ['database-unavailable']
    return event

sentry_sdk.init(before_send=before_send)

For information about which hints are available see hints in python.

import * as Sentry from '@sentry/browser';

init({
  beforeSend(event, hint) {
    const { message } = hint.originalException;
    if (message && message.match(/database unavailable/i)) {
      event.fingerprint = ['database-unavailable'];
    }
    return event;
  }
});

For information about which hints are available see hints in JavaScript.

Before Breadcrumb

Similar to before-send hints are also supported in before-breadcrumb. This is particularly useful when breadcrumbs should be augmented or changed with some extra data from log records or events. The following example shows a typical situation where you might want to extract extra information and how to do it:

import sentry_sdk

def before_breadcrumb(crumb, hint):
    if 'log_record' in hint:
        crumb['data']['thread'] = hint['log_record'].threadName
    return crumb

sentry_sdk.init(before_breadcrumb=before_breadcrumb)

For information about which hints are available see hints in python.

import * as Sentry from '@sentry/browser';

Sentry.init({
  dsn: 'https://<key>@sentry.io/',
  beforeBreadcrumb(breadcrumb, hint) {
    if (breadcrumb.category === 'ui.click') {
      const { target } = hint.event;
      if (target.ariaLabel) {
        breadcrumb.message = target.ariaLabel;
      }
    }
    return breadcrumb;
  },
});

For information about which hints are available see hints in javascript.