Filtering events

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

A Func 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;
    };
});

In Go, a function can be used to modify the event or return a completely new one. If you return nil, the SDK will discard the event.

sentry.Init(sentry.ClientOptions{
	BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
		// Modify the event here
		if event.user != nil {
			// Don't send user's email address
			event.user.email = nil
		}
		
		return event
	},
})

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;
  }
});
#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);
  sentry_init(options);
  
  /* ... */
}

In PHP 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([
  'dsn' => '___PUBLIC_DSN___',
  'before_send' => function (Sentry\Event $event): ?Event {
    return $event;
  },
]);

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
)
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 is affected. before-send will be called right before the event is sent to the server, so it’s the last place where you can edit its data.

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

sentry.Init(sentry.ClientOptions{
	BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
		if ex, ok := hint.OriginalException.(DatabaseConnectionError); ok {
			event.Fingerprint = []string{"database-connection-error"}
		}

		return event
	},
})

For information about which hints are available see EventHint implementation.

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

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

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

#include <sentry.h>

sentry_value_t before_send(sentry_value_t event, void *hint) {
  /* inspect hint  and 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, before_send);
  sentry_init(options);
  
  /* ... */
}
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.

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 * 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.

This is not supported by the Native SDK.

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.