---
title: "Filtering"
description: "Learn more about how to configure your SDK to filter events reported to Sentry."
url: https://docs.sentry.io/platforms/python/configuration/filtering/
---

# Filtering | Sentry for Python

When you add Sentry to your app, you get a lot of valuable information about errors and performance. 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 filter out events.

We also offer [Inbound Filters](https://docs.sentry.io/concepts/data-management/filtering.md) to filter events in sentry.io. We recommend filtering at the client level though, because it removes the overhead of sending events you don't actually want. Learn more about the [fields available in an event](https://develop.sentry.dev/sdk/foundations/transport/event-payloads/).

## [Filtering Error Events](https://docs.sentry.io/platforms/python/configuration/filtering.md#filtering-error-events)

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

### [Using `before_send`](https://docs.sentry.io/platforms/python/configuration/filtering.md#using-before-send)

The `before_send` callback allows you to modify the event payload before the SDK sends the event to Sentry. You can also stop the event from being sent by returning `None`.

The callback receives two arguments: the event payload and a [hint](https://docs.sentry.io/platforms/python/configuration/filtering.md#event-hints). The corresponding `Event` and `Hint` types can be imported from `sentry_sdk.types` for better introspection into what fields are available. The callback can return either the event payload to send to Sentry or `None` if the event should be dropped.

Any modifications to the event payload done in the callback, including adding data to the event and modifying or deleting existing event fields, will be reflected in Sentry.

#### [Example `before_send`](https://docs.sentry.io/platforms/python/configuration/filtering.md#example-before_send)

Suppose that you wish prevent all errors of type `ZeroDivisionError` from being sent to Sentry and that you want to set an additional data attribute called `"foo"` to `"bar"` on all other events. You can achieve this behavior with the following `before_send` callback.

```python
import sentry_sdk
from sentry_sdk.types import Event, Hint

def before_send(event: Event, hint: Hint) -> Event | None:
    # Filter out all ZeroDivisionError events.
    # Note that the exception type is available in the hint,
    # but we should handle the case where the exception info
    # is missing.
    if hint.get("exc_info", [None])[0] == ZeroDivisionError:
        return None

    # We can set extra data on the event's "extra" field.
    event["extra"]["foo"] = "bar"

    # We have modified the event as desired, so return the event.
    # The SDK will then send the returned event to Sentry.
    return event

sentry_sdk.init(
    # ...

    before_send=before_send,
)
```

Note also that breadcrumbs can be filtered, as discussed in [our Breadcrumbs documentation](https://docs.sentry.io/product/error-monitoring/breadcrumbs.md).

#### [Event Hints](https://docs.sentry.io/platforms/python/configuration/filtering.md#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:

```python
import sentry_sdk
from sentry_sdk.types import Event, Hint

def before_send(event: Event, hint: Hint) -> Event | None:
    if 'exc_info' not in hint:
        return event

    exception = hint['exc_info'][1]

    if isinstance(exception, 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](https://docs.sentry.io/platforms/python/configuration/filtering.md#using-hints).

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, the SDK sends these objects to certain callbacks (`before_send`, `before_breadcrumb` and event processors).

### [Using Hints](https://docs.sentry.io/platforms/python/configuration/filtering.md#using-hints)

Hints are available in two places:

1. `before_send` / `before_breadcrumb`
2. `eventProcessors`

Event and breadcrumb `hints` are objects containing various information used to put together an event or a breadcrumb. Typically `hints` hold the original exception so that additional data can be extracted or grouping can be affected.

For events, hints contain properties such as `event_id`, `originalException`, `syntheticException` (used internally to generate cleaner stack trace), and any other arbitrary `data` that you attach.

For breadcrumbs, the use of `hints` depends on the type of breadcrumb. For logs, the hint contains the original `logging` log record.

```python
import sentry_sdk
from sentry_sdk.types import Event, Hint

def before_send(event: Event, hint: Hint) -> Event | None:
    if 'exc_info' not in hint:
        return event

    exception = hint['exc_info'][1]

    if isinstance(exception, DatabaseUnavailable):
        event['fingerprint'] = ['database-unavailable']

    return event

sentry_sdk.init(
    # ...
    before_send=before_send,
)
```

#### [Hints for Events](https://docs.sentry.io/platforms/python/configuration/filtering.md#hints-for-events)

`originalException`

The original exception that caused the Sentry SDK to create the event. This is useful for changing how the Sentry SDK groups events or to extract additional information.

`syntheticException`

When a string or a non-error object is raised, Sentry creates a synthetic exception so you can get a basic stack trace. This exception is stored here for further data extraction.

#### [Hints for Breadcrumbs](https://docs.sentry.io/platforms/python/configuration/filtering.md#hints-for-breadcrumbs)

`log_record`

For breadcrumbs created from logging integrations. The record holds the original console log level and the original input data to the log function.

### [Decluttering Sentry](https://docs.sentry.io/platforms/python/configuration/filtering.md#decluttering-sentry)

By default the Python SDK captures all error logs as events. If you see a particular kind of error very often that has a `logger` tag, you can ignore that particular logger entirely. For more information see our [logging integration](https://docs.sentry.io/platforms/python/integrations/logging.md).

## [Filtering Transaction Events](https://docs.sentry.io/platforms/python/configuration/filtering.md#filtering-transaction-events)

To prevent certain transactions from being reported to Sentry, use the `traces_sampler` or `before_send_transaction` configuration option, which allows you to provide a function to evaluate the current transaction and drop it if it's not one you want.

### [Using `traces_sampler`](https://docs.sentry.io/platforms/python/configuration/filtering.md#using-traces-sampler)

**Note:** The `traces_sampler` and `traces_sample_rate` config options are mutually exclusive. If you define a `traces_sampler` to filter out certain transactions, you must also handle the case of non-filtered transactions by returning the rate at which you'd like them sampled.

In its simplest form, used just for filtering the transaction, it looks like this:

```python
import sentry_sdk
from sentry_sdk.types import SamplingContext

def traces_sampler(sampling_context: SamplingContext) -> float:
    # Examine provided sampling context along with anything in the
    # global namespace to compute the sample rate for this transaction
    if "...":
        # Drop this transaction, by setting its sample rate to 0%
        return 0

    # Default sample rate for all others (replaces traces_sample_rate)
    return 0.1

sentry_sdk.init(
    # ...
    traces_sampler=traces_sampler,
)
```

It also allows you to sample different transactions at different rates.

If the transaction currently being processed has a parent transaction (from an upstream service calling this service), the parent (upstream) sampling decision will always be included in the sampling context data, so that your `traces_sampler` can choose whether and when to inherit that decision. In most cases, inheritance is the right choice, to avoid breaking distributed traces. A broken trace will not include all your services. See [Inheriting the parent sampling decision](https://docs.sentry.io/platforms/python/configuration/sampling.md#inheritance) to learn more.

Learn more about [configuring the sample rate](https://docs.sentry.io/platforms/python/configuration/sampling.md).

### [Using `before_send_transaction`](https://docs.sentry.io/platforms/python/configuration/filtering.md#using-before-send-transaction)

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

```python
import sentry_sdk
from sentry_sdk.types import Event, Hint

def strip_sensitive_data(event: Event, hint: Hint) -> Event | None:
    # modify event here
    return event

sentry_sdk.init(
    # ...
    before_send_transaction=strip_sensitive_data,
)
```

Additionally, you may filter out transaction events based on the request URL, like `/healthcheck`.

```python
import sentry_sdk
from sentry_sdk.types import Event, Hint
from urllib.parse import urlparse

def filter_transactions(event: Event, hint: Hint) -> Event | None:
    url_string = event["request"]["url"]
    parsed_url = urlparse(url_string)

    if parsed_url.path == "/healthcheck":
        return None

    return event

sentry_sdk.init(
    # ...
    before_send_transaction=filter_transactions,
)
```

## Pages in this section

- [Hints](https://docs.sentry.io/platforms/python/configuration/filtering/hints.md)
