[PR #8850] fix(instrumentation): don't mark redirect APIErrors as span errors #16498

Closed
opened 2026-04-13 10:32:38 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/8850

State: closed
Merged: Yes


Summary

withSpan unconditionally records all thrown exceptions as SpanStatusCode.ERROR. Better-auth uses throw ctx.redirect(url) — an APIError("FOUND") with status 302 — for flow control in OAuth callbacks. These are successful redirects, not errors, but they show up as error spans in OpenTelemetry traces (Dash0, Jaeger, etc.).

Before: Every successful OAuth callback produces an error span on handler /oauth2/callback/:providerId because the redirect throw is recorded as an exception.

After: Redirect APIErrors (3xx status codes) are detected by duck-typing (name === "APIError" + statusCode in 300–399 range) and recorded with SpanStatusCode.OK + the http.response.status_code attribute instead.

Changes

  • packages/core/src/instrumentation/tracer.ts — extract endSpanWithError() helper that checks for redirect errors before marking span status
  • packages/core/src/instrumentation/instrumentation.test.ts — 3 new tests: redirect sync, redirect async, non-redirect APIError still records ERROR

Test plan

  • Existing instrumentation tests pass (10/10)
  • New test: sync redirect APIError → SpanStatusCode.OK + http.response.status_code: 302
  • New test: async redirect APIError → SpanStatusCode.OK + http.response.status_code: 302
  • New test: non-redirect APIError (404) → still SpanStatusCode.ERROR

🤖 Generated with Claude Code


Summary by cubic

Fixes OpenTelemetry spans incorrectly marked as errors when OAuth callbacks throw redirect APIErrors. Redirect throws now end spans as OK and set http.response.status_code to the 3xx value.

  • Bug Fixes

    • In withSpan, detect redirect APIErrors (name === "APIError", statusCode 300–399) and treat them as non-errors.
    • Record http.response.status_code and set span status OK; non-redirect APIErrors still ERROR.
    • Added tests for sync/async redirects and a 404 case.
  • Refactors

    • Added isRedirectError() and extracted endSpanWithError() in packages/core/src/instrumentation/tracer.ts to centralize status handling.

Written for commit 6854d808f7. Summary will update on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8850 **State:** closed **Merged:** Yes --- ## Summary `withSpan` unconditionally records all thrown exceptions as `SpanStatusCode.ERROR`. Better-auth uses `throw ctx.redirect(url)` — an `APIError("FOUND")` with status 302 — for **flow control** in OAuth callbacks. These are successful redirects, not errors, but they show up as error spans in OpenTelemetry traces (Dash0, Jaeger, etc.). **Before:** Every successful OAuth callback produces an error span on `handler /oauth2/callback/:providerId` because the redirect throw is recorded as an exception. **After:** Redirect `APIError`s (3xx status codes) are detected by duck-typing (`name === "APIError"` + `statusCode` in 300–399 range) and recorded with `SpanStatusCode.OK` + the `http.response.status_code` attribute instead. ## Changes - `packages/core/src/instrumentation/tracer.ts` — extract `endSpanWithError()` helper that checks for redirect errors before marking span status - `packages/core/src/instrumentation/instrumentation.test.ts` — 3 new tests: redirect sync, redirect async, non-redirect APIError still records ERROR ## Test plan - [x] Existing instrumentation tests pass (10/10) - [x] New test: sync redirect APIError → `SpanStatusCode.OK` + `http.response.status_code: 302` - [x] New test: async redirect APIError → `SpanStatusCode.OK` + `http.response.status_code: 302` - [x] New test: non-redirect APIError (404) → still `SpanStatusCode.ERROR` 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes OpenTelemetry spans incorrectly marked as errors when OAuth callbacks throw redirect APIErrors. Redirect throws now end spans as OK and set `http.response.status_code` to the 3xx value. - **Bug Fixes** - In `withSpan`, detect redirect APIErrors (`name === "APIError"`, `statusCode` 300–399) and treat them as non-errors. - Record `http.response.status_code` and set span status OK; non-redirect APIErrors still ERROR. - Added tests for sync/async redirects and a 404 case. - **Refactors** - Added `isRedirectError()` and extracted `endSpanWithError()` in `packages/core/src/instrumentation/tracer.ts` to centralize status handling. <sup>Written for commit 6854d808f7e300f6773e4a0be929a62c03dec319. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 10:32:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16498