Skip to content

Wire Bugsnag into the API server#231

Merged
morgan-wowk merged 1 commit into
masterfrom
bugsnag/api-server-integration
May 15, 2026
Merged

Wire Bugsnag into the API server#231
morgan-wowk merged 1 commit into
masterfrom
bugsnag/api-server-integration

Conversation

@morgan-wowk
Copy link
Copy Markdown
Collaborator

@morgan-wowk morgan-wowk commented May 8, 2026

TL;DR

Integrates Bugsnag error monitoring into the API server.

What changed?

  • Bugsnag is initialized during app startup with the service name tangle-api.
  • The BugsnagMiddleware is conditionally added to the FastAPI middleware stack when Bugsnag is enabled.
  • Unhandled exceptions caught by the global exception handler are now reported to Bugsnag via bugsnag_instrumentation.notify.

How to test?

  • Enable Bugsnag by configuring the appropriate environment variables/credentials.
  • Trigger an unhandled exception in the API and verify it appears in the Bugsnag dashboard.
  • Confirm the middleware is only added when IS_BUGSNAG_ENABLED is True, and that the app starts normally when Bugsnag is disabled.

Why make this change?

To improve observability and error tracking in production by capturing and reporting unhandled exceptions to Bugsnag, enabling faster diagnosis and resolution of issues.

Bugsnag ASGI Middleware

BugsnagMiddleware (source) wraps the ASGI layer and automatically enriches every error event with request context:

  • Client IP from the ASGI scope
  • HTTP method, version, and path
  • Full request URL (scheme + host + port + path + query string)
  • All request headers — with authorization, cookie, x-api-key, and proxy-authorization redacted via params_filters
  • ASGI scope dict as an "environment" tab — opt-in via send_environment=True, off by default

The middleware and the explicit bugsnag.notify() call in handle_error are complementary, not duplicative: FastAPI's exception handler consumes exceptions before they reach the ASGI layer, so handle_error covers normal request errors while BugsnagMiddleware acts as a safety net for exceptions that escape FastAPI entirely (e.g. startup failures, middleware crashes).

Additionally, every Bugsnag event — whether from the middleware or from a direct notify() call — is enriched with a "tangle_context" tab via our before_notify callback, containing: request_id, user_id, pipeline_run_id, and execution_id from the current request context.

Copy link
Copy Markdown
Collaborator Author

morgan-wowk commented May 8, 2026

@morgan-wowk morgan-wowk marked this pull request as ready for review May 8, 2026 20:40
@morgan-wowk morgan-wowk requested a review from Ark-kun as a code owner May 8, 2026 20:40
Comment thread api_server_main.py Outdated
@app.exception_handler(Exception)
def handle_error(request: fastapi.Request, exc: BaseException):
exception_str = traceback.format_exception(type(exc), exc, exc.__traceback__)
bugsnag_instrumentation.notify(exc)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the noise, I realize you mention this is not ready for review yet, but wanted to add this comment before I forget.

I really like how you added in the orchestrator stack for adding the execution.id to the exception for better tracebility. Not sure if there is anything like that now in the API level? I believe we are adding system level annotations to the source (UI, CLI/tangle-deploy), that "might" be nice to add to this to label the exception better.

Just a thought, if we don't have the plumbing now to easily do this, then not a concern.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is welcome. Good question / topic to look into!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. We don't have any traces or contextual ids in the API level.

+1 that having the source would be great!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer adding that to a future PR since the source has been in discussion recently and may change

@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from f967108 to bba37bb Compare May 8, 2026 21:05
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from cf06f0c to 248ce56 Compare May 8, 2026 21:05
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from bba37bb to d1e5bdb Compare May 8, 2026 22:38
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch 2 times, most recently from 66f4c0d to c0d147f Compare May 8, 2026 23:13
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from d1e5bdb to 368e08e Compare May 8, 2026 23:13
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from c0d147f to 4a6f0bc Compare May 8, 2026 23:18
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch 2 times, most recently from ff5439f to 765252d Compare May 8, 2026 23:25
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 4a6f0bc to 7a7696e Compare May 8, 2026 23:25
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch 2 times, most recently from e7d28aa to 666bc18 Compare May 8, 2026 23:32
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 7a7696e to 6842b69 Compare May 8, 2026 23:32
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from 666bc18 to a5bc1e7 Compare May 8, 2026 23:38
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 6842b69 to 936d216 Compare May 8, 2026 23:38
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from a5bc1e7 to cd51f98 Compare May 8, 2026 23:40
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 936d216 to 514d0f0 Compare May 8, 2026 23:40
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from cd51f98 to d7ace7d Compare May 8, 2026 23:41
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 514d0f0 to 365eab5 Compare May 8, 2026 23:41
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 365eab5 to fb3d6ba Compare May 13, 2026 01:25
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from d7ace7d to 4c9bbf0 Compare May 13, 2026 01:25
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from fb3d6ba to 2b50dde Compare May 13, 2026 01:47
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from 4c9bbf0 to 5fc47fb Compare May 13, 2026 01:47
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 2b50dde to 83fb649 Compare May 13, 2026 18:43
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from 5fc47fb to b73fa62 Compare May 13, 2026 18:43
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 83fb649 to 3c2bd5a Compare May 13, 2026 20:41
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from b73fa62 to f4d070d Compare May 13, 2026 20:41
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 3c2bd5a to aff71b8 Compare May 13, 2026 20:54
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from f4d070d to df974a8 Compare May 13, 2026 20:54
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from aff71b8 to 6b75f47 Compare May 13, 2026 21:26
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from df974a8 to 5ef7052 Compare May 13, 2026 21:26
@morgan-wowk morgan-wowk force-pushed the bugsnag/instrumentation-module branch from 6b75f47 to dc99bb4 Compare May 13, 2026 23:20
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from 5ef7052 to 83977b0 Compare May 13, 2026 23:20
Copy link
Copy Markdown
Collaborator Author

morgan-wowk commented May 15, 2026

Merge activity

  • May 15, 5:05 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • May 15, 5:08 PM UTC: Graphite rebased this pull request as part of a merge.
  • May 15, 5:09 PM UTC: @morgan-wowk merged this pull request with Graphite.

@morgan-wowk morgan-wowk changed the base branch from bugsnag/instrumentation-module to graphite-base/231 May 15, 2026 17:06
@morgan-wowk morgan-wowk changed the base branch from graphite-base/231 to master May 15, 2026 17:07
@morgan-wowk morgan-wowk force-pushed the bugsnag/api-server-integration branch from 83977b0 to c18f430 Compare May 15, 2026 17:08
@morgan-wowk morgan-wowk merged commit b9ecb07 into master May 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants