Skip to content

Add pluggable context providers, with a Sentry integration - #59

Merged
snopoke merged 3 commits into
mainfrom
sk/sentry
Aug 5, 2026
Merged

Add pluggable context providers, with a Sentry integration#59
snopoke merged 3 commits into
mainfrom
sk/sentry

Conversation

@snopoke

@snopoke snopoke commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Adds a way to link a failed task back to where it was actually reported — starting with Sentry, so the TaskBadger UI can eventually show a link to the originating Sentry issue.

ContextProvider is a small extension point consulted whenever a tracked task errors (@track, Celery, Procrastinate, or Task.error(exception=...)), letting a provider attach extra data. SentryContextProvider is the first implementation.

Worth a closer look: SentryContextProvider doesn't call sentry_sdk.capture_exception itself — it assumes the app already reports exceptions via its own Sentry setup, and just reads back sentry_sdk.last_event_id() to avoid double-reporting the same exception. Since that's a passive read, a snapshot is taken when the task starts and compared at error time, so a stale id from some unrelated earlier capture isn't misreported as belonging to this task's error.

Usage:

from taskbadger.context_providers.sentry import SentryContextProvider

taskbadger.init(..., context_providers=[SentryContextProvider(organization_slug="acme")])

Failed tasks then get data["sentry"] = {"event_id": ..., "url": "https://sentry.io/organizations/acme/issues/?query=<id>"}.

sentry-sdk is an optional dependency (new sentry extra), imported lazily so it's a no-op if not installed.

snopoke added 2 commits August 5, 2026 12:07
Lets a provider attach extra context (e.g. a link to the originating
issue) to a task's data when it errors. Wired into @track, the Celery
signal handlers, and the Procrastinate integration, plus Task.error()
for manual reporting. A start/reset snapshot pair lets providers that
read back state from elsewhere tell a fresh capture from a stale one.
Links a failed task to its Sentry issue by reading back
sentry_sdk.last_event_id() rather than capturing the exception itself,
since the app is assumed to already report it via its own Sentry
integration. A snapshot taken when the task starts filters out stale
ids left over from an unrelated earlier capture.
@snopoke
snopoke marked this pull request as ready for review August 5, 2026 11:26
Covers the Celery, Procrastinate, @track, and Task.error() entry points with a real provider, not just the low-level helpers.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@snopoke
snopoke merged commit 1f1d0eb into main Aug 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant