[PR #8027] [MERGED] feat(core): add experimental opentelemetry instrumentation #15942

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8027
Author: @jonathansamines
Created: 2/17/2026
Status: Merged
Merged: 3/3/2026
Merged by: @Bekacru

Base: canaryHead: feat/otel-instrumentation


📝 Commits (9)

  • b89c994 feat(core): add experimental opentelemetry instrumentation
  • b597161 chore(core): better span error handling
  • c4c6ca1 docs: update db instrumentation docs
  • 69275ce chore(core): use more robust error message fallback
  • b4440dd Merge branch 'canary' into feat/otel-instrumentation
  • ffa3a05 Merge branch 'canary' into feat/otel-instrumentation
  • 96f5a11 Merge branch 'canary' into feat/otel-instrumentation
  • 9f32058 Merge branch 'canary' into feat/otel-instrumentation
  • 1394d45 chore(better-auth): fix format

📊 Changes

21 files changed (+1352 additions, -237 deletions)

View changed files

docs/content/docs/reference/instrumentation.mdx (+87 -0)
docs/next-env.d.ts (+6 -0)
📝 landing/components/sidebar-content.tsx (+6 -0)
📝 packages/better-auth/package.json (+3 -0)
📝 packages/better-auth/src/api/index.ts (+42 -8)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+213 -125)
📝 packages/better-auth/src/context/create-context.ts (+3 -1)
📝 packages/better-auth/src/context/helpers.ts (+14 -4)
📝 packages/better-auth/src/db/internal-adapter.ts (+2 -1)
📝 packages/better-auth/src/db/with-hooks.ts (+143 -42)
📝 packages/better-auth/src/index.ts (+1 -0)
packages/better-auth/src/instrumentation.db.test.ts (+266 -0)
packages/better-auth/src/instrumentation.endpoint.test.ts (+180 -0)
📝 packages/core/package.json (+13 -0)
📝 packages/core/src/db/adapter/factory.ts (+119 -47)
packages/core/src/instrumentation/attributes.ts (+22 -0)
packages/core/src/instrumentation/index.ts (+2 -0)
packages/core/src/instrumentation/instrumentation.test.ts (+139 -0)
packages/core/src/instrumentation/tracer.ts (+62 -0)
📝 packages/core/tsdown.config.ts (+1 -0)

...and 1 more files

📄 Description

Summary by cubic

Adds experimental OpenTelemetry tracing across Better Auth for endpoints, hooks, plugin middleware/onRequest/onResponse, and database ops/hooks. Tracing is a no-op unless a TracerProvider is configured, and spans record exceptions with error status and a safe message fallback.

  • New Features

    • New @better-auth/core/instrumentation entrypoint with withSpan (records exceptions and sets error status/message).
    • Standard attributes: http.route, http.response.status_code, db.operation.name, db.collection.name, better_auth.operation_id, better_auth.hook.type, better_auth.context.
    • Endpoint spans: parent "{METHOD} {path}" and "handler {path}" with http.route and better_auth.operation_id (from endpoint options/OpenAPI operationId, fallback to endpoint key).
    • Hook/middleware spans: global and plugin before/after with hook.type, context ("user" or "plugin:{id}"), http.route, operation_id; plugin onRequest/onResponse include http.route, context, and response status code; middleware spans use route templates.
    • DB spans: all adapter ops (create/findOne/findMany/update/updateMany/delete/deleteMany/count, incl. fallback join) plus model-level hooks (create/update/updateMany/delete/deleteMany before/after) with db.operation.name, db.collection.name, hook.type, and context.
    • Docs: reference page at /docs/reference/instrumentation and sidebar link.
  • Dependencies

    • core: add @opentelemetry/api (peer) and @opentelemetry/semantic-conventions; expose "./instrumentation".
    • better-auth (dev/tests): add @opentelemetry/api, @opentelemetry/sdk-trace-base, and @opentelemetry/sdk-trace-node.

Written for commit ffa3a0573a. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8027 **Author:** [@jonathansamines](https://github.com/jonathansamines) **Created:** 2/17/2026 **Status:** ✅ Merged **Merged:** 3/3/2026 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `feat/otel-instrumentation` --- ### 📝 Commits (9) - [`b89c994`](https://github.com/better-auth/better-auth/commit/b89c994366b68dd964c9b6bc1a74396f6d78fea9) feat(core): add experimental opentelemetry instrumentation - [`b597161`](https://github.com/better-auth/better-auth/commit/b597161e9c111e263227e96b91ca884905e3fbd9) chore(core): better span error handling - [`c4c6ca1`](https://github.com/better-auth/better-auth/commit/c4c6ca12d5087731e6ac525e20ea93595c208f37) docs: update db instrumentation docs - [`69275ce`](https://github.com/better-auth/better-auth/commit/69275ce38a73272991320de476309e517b34e26b) chore(core): use more robust error message fallback - [`b4440dd`](https://github.com/better-auth/better-auth/commit/b4440dd27c91700062055f23b0b46f8ab7c37425) Merge branch 'canary' into feat/otel-instrumentation - [`ffa3a05`](https://github.com/better-auth/better-auth/commit/ffa3a0573a96e3d0cd483960536b36f20a16af9c) Merge branch 'canary' into feat/otel-instrumentation - [`96f5a11`](https://github.com/better-auth/better-auth/commit/96f5a1104317f5c8c152cb168a011d7089824de3) Merge branch 'canary' into feat/otel-instrumentation - [`9f32058`](https://github.com/better-auth/better-auth/commit/9f320583f0281a4a2de972f30cea5e312f8f883d) Merge branch 'canary' into feat/otel-instrumentation - [`1394d45`](https://github.com/better-auth/better-auth/commit/1394d45185f03df04f0bc179f04f51c02e6bb40c) chore(better-auth): fix format ### 📊 Changes **21 files changed** (+1352 additions, -237 deletions) <details> <summary>View changed files</summary> ➕ `docs/content/docs/reference/instrumentation.mdx` (+87 -0) ➕ `docs/next-env.d.ts` (+6 -0) 📝 `landing/components/sidebar-content.tsx` (+6 -0) 📝 `packages/better-auth/package.json` (+3 -0) 📝 `packages/better-auth/src/api/index.ts` (+42 -8) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+213 -125) 📝 `packages/better-auth/src/context/create-context.ts` (+3 -1) 📝 `packages/better-auth/src/context/helpers.ts` (+14 -4) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+2 -1) 📝 `packages/better-auth/src/db/with-hooks.ts` (+143 -42) 📝 `packages/better-auth/src/index.ts` (+1 -0) ➕ `packages/better-auth/src/instrumentation.db.test.ts` (+266 -0) ➕ `packages/better-auth/src/instrumentation.endpoint.test.ts` (+180 -0) 📝 `packages/core/package.json` (+13 -0) 📝 `packages/core/src/db/adapter/factory.ts` (+119 -47) ➕ `packages/core/src/instrumentation/attributes.ts` (+22 -0) ➕ `packages/core/src/instrumentation/index.ts` (+2 -0) ➕ `packages/core/src/instrumentation/instrumentation.test.ts` (+139 -0) ➕ `packages/core/src/instrumentation/tracer.ts` (+62 -0) 📝 `packages/core/tsdown.config.ts` (+1 -0) _...and 1 more files_ </details> ### 📄 Description <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Adds experimental OpenTelemetry tracing across Better Auth for endpoints, hooks, plugin middleware/onRequest/onResponse, and database ops/hooks. Tracing is a no-op unless a TracerProvider is configured, and spans record exceptions with error status and a safe message fallback. - **New Features** - New @better-auth/core/instrumentation entrypoint with withSpan (records exceptions and sets error status/message). - Standard attributes: http.route, http.response.status_code, db.operation.name, db.collection.name, better_auth.operation_id, better_auth.hook.type, better_auth.context. - Endpoint spans: parent "{METHOD} {path}" and "handler {path}" with http.route and better_auth.operation_id (from endpoint options/OpenAPI operationId, fallback to endpoint key). - Hook/middleware spans: global and plugin before/after with hook.type, context ("user" or "plugin:{id}"), http.route, operation_id; plugin onRequest/onResponse include http.route, context, and response status code; middleware spans use route templates. - DB spans: all adapter ops (create/findOne/findMany/update/updateMany/delete/deleteMany/count, incl. fallback join) plus model-level hooks (create/update/updateMany/delete/deleteMany before/after) with db.operation.name, db.collection.name, hook.type, and context. - Docs: reference page at /docs/reference/instrumentation and sidebar link. - **Dependencies** - core: add @opentelemetry/api (peer) and @opentelemetry/semantic-conventions; expose "./instrumentation". - better-auth (dev/tests): add @opentelemetry/api, @opentelemetry/sdk-trace-base, and @opentelemetry/sdk-trace-node. <sup>Written for commit ffa3a0573a96e3d0cd483960536b36f20a16af9c. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:19:05 -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#15942