Search code examples
pythonsentry

remove current breadcrumbs in sentry


I have simple synchronous code like this:

import sentry_sdk
from time import sleep


sentry_sdk.init(MY_DSN)
while True:
    # remove sentry breadcrumbs here, so they would not accumulate
    try:
        do_my_stuff()
    except:
        handle_my_exception()

    sleep(some_non_linear_algorithm())  # I would like not to use crontab

When exception occurs sentry captures it and all breadcrumbs from previous iterations, so I need to delete all current breadcrumbs in the beginning of each iteration. But I cannot find any API to do that in sentry documentation.


Solution

  • Wrap your code in this:

    with sentry_sdk.push_scope():
        try:
            ...
        except:
            ...
    

    This will make sure that you don't leak breadcrumbs, tags, etc between iterations.

    Alternatively you can use:

    with sentry_sdk.configure_scope() as scope:
        scope.clear()