Skip to content

feat(server-utils): Record the call site on gen_ai request spans - #27090

Draft
sentry-junior[bot] wants to merge 1 commit into
developfrom
feat/gen-ai-call-site
Draft

sentry-junior[bot] wants to merge 1 commit into
developfrom
feat/gen-ai-call-site

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

What

Chat, completion and embeddings spans from the OpenAI, Anthropic, Google GenAI and LangChain embeddings integrations now get code.file.path, code.function and code.line.number for the line in the app that made the request. Both the client wrappers (instrumentOpenAiClient and friends) and the tracing channel integrations set them. A stream gets the line that started it, not the line that consumed it. getsentry/sentry-python#8441 does the same for Python.

Why

LLM spans can't be grouped by where they were made without manual instrumentation, and analyses such as prompt-cache detection need that grouping.

How it stays cheap:

  • Only recording spans capture anything. Unsampled spans pay one isRecording() check.
  • At span start, Error.captureStackTrace collects at most 20 frames. V8 doesn't format them then.
  • At span end, the frames are read as V8's structured call sites (a temporary Error.prepareStackTrace), so the stack is never formatted into a string or run through the stack parser. Formatting and parsing were the expensive part: reading strings with the client's stackParser cost 2 to 3 times as much.
  • The client wrappers pass their own function as the captureStackTrace boundary, so SDK frames are never collected. In tracing channel subscribers, the site is the first in-app frame after the first library frame.

Measured overhead per sampled span, Node 24, with the call 40 frames deep:

Path Added
Client wrapper (capture + read) ~15 µs
Channel subscriber, worst case (scans all 20 frames) ~22 µs
Real openai client through the channel integration, mocked fetch, whole call ~110 → ~147 µs (≈ 23 µs ours, ≈ 10 µs serializing the three extra attributes)
Unsampled span < 1 µs

That is in the noise next to a network round trip to an LLM, but it is not free for sampled spans. A captureCallSite: false style option would be easy to add if we want one.

Wrappers: there is no in_app_exclude here, so an app's own LLM helper becomes the call site for every request that goes through it. Python handles that with in_app_exclude. For JS, a later change could add an option to skip named modules or functions, or record a few in-app frames instead of one.

Not covered yet:

  • LangChain chat models and LLMs: LangChain runs callback handlers after its own awaits, so no app frame is on the stack. That needs a hook at BaseChatModel.invoke and friends.
  • Vercel AI: its spans come from the ai package's own telemetry.
  • Anthropic's tool runner and message threads go deeper than 20 frames, so they often get no site.
  • In bundled code, the path is the bundle's path and line. Source maps are not applied.

Open questions:

  • code.function or code.function.name? This uses code.function to match Python. The conventions define both and deprecate neither.
  • Is 20 frames the right cap?
  • Should invoke_agent and tool spans get a call site too?

via @BYK.

--

View Session [Sentry]

Set code.file.path, code.function and code.line.number on the chat,
completion and embeddings spans of the OpenAI, Anthropic, Google GenAI
and LangChain embeddings integrations, so LLM calls can be grouped by
where in the app they were made.

The stack is captured with Error.captureStackTrace only for recording
spans, capped at 20 frames, and read from V8's structured call sites
when the span ends, so it's never formatted or parsed as a string.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: e5b7eca (PR test merge using develop at 29e62cd)

Against: 1c10c6f (develop) · base run #68574

⚠️ Baseline mismatch: 5 commit(s) in the PR build are missing from the baseline. Their size changes also affect this comparison. Compare commits. Re-run once the baseline build for 29e62cd finishes.

Path Size % Change Change
@sentry/browser 30.64 kB - -
@sentry/browser - with treeshaking flags 28.73 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.62 kB - -
@sentry/browser (incl. Tracing) 53.98 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing Core) 40.82 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 53.98 kB +0.01% +1 B 🔺
@sentry/browser (incl. Tracing, Profiling) 56.86 kB - -
@sentry/browser (incl. Tracing, Replay) 93.96 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 82.52 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 98.8 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 111.75 kB - -
@sentry/browser (incl. Feedback) 48.51 kB +0.01% +1 B 🔺
@sentry/browser (incl. sendFeedback) 31.2 kB - -
@sentry/browser (incl. FeedbackAsync) 36.26 kB +0.01% +1 B 🔺
@sentry/browser (incl. Metrics) 31.96 kB - -
@sentry/browser (incl. Logs) 32.25 kB - -
@sentry/browser (incl. Metrics & Logs) 32.91 kB - -
@sentry/react 32.5 kB - -
@sentry/react (incl. Tracing) 56.46 kB - -
@sentry/vue 38.99 kB - -
@sentry/vue (incl. Tracing) 57.05 kB -0.01% -1 B 🔽
@sentry/svelte 30.68 kB - -
@sentry/effect (client) 42.21 kB -0.01% -1 B 🔽
@sentry/effect (client, incl. Tracing) 55.02 kB - -
@sentry/effect (client, incl. Tracing, Logs, Metrics) 66.5 kB - -
@sentry/remix (Remix 3 client bundle) 59.47 kB -0.01% -1 B 🔽
CDN Bundle 32.45 kB - -
CDN Bundle (incl. Tracing) 54.12 kB - -
CDN Bundle (incl. Logs, Metrics) 35.2 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 56.57 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 76.15 kB - -
CDN Bundle (incl. Tracing, Replay) 91.93 kB -0.01% -1 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 94.44 kB +0.01% +1 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 97.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.85 kB - -
CDN Bundle - uncompressed 94.67 kB - -
CDN Bundle (incl. Tracing) - uncompressed 160.02 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 103.01 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 167.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 233.4 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 280.45 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 288.08 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 292.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 300 kB - -
@sentry/nextjs (client) 59.75 kB - -
@sentry/sveltekit (client) 54.8 kB - -
@sentry/astro (client) - rolldown, without tracing 35.73 kB -0.01% -1 B 🔽
@sentry/core/server 45.91 kB - -
@sentry/core/browser 14.55 kB - -
@sentry/node 158.02 kB +0.23% +360 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.63 kB - -
@sentry/node - without tracing 89.66 kB +0.01% +1 B 🔺
@sentry/node - without channel injection 147.38 kB +0.27% +396 B 🔺
@sentry/aws-serverless 100.86 kB +0.3% +294 B 🔺
@sentry/cloudflare (withSentry) - minified 213.68 kB +0.39% +813 B 🔺
@sentry/cloudflare (withSentry) 495.15 kB +0.35% +1.72 kB 🔺
@sentry/cloudflare (withSentry, instrumented CommonJS package) - minified 216.62 kB +0.38% +813 B 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 239.08 kB +0.34% +789 B 🔺

View base workflow run

@sentry-junior
sentry-junior Bot requested a review from BYK December 2, 2026 16:33
let pastLibrary = !calledFromLibrary;
for (const callSite of callSites || []) {
const fileName = callSite.getFileName();
const filename = fileName?.startsWith('file://') ? decodeURI(fileName.slice('file://'.length)) : fileName;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Windows file:// call-site paths keep a leading slash

When CallSite.getFileName() returns a Windows file URL like file:///C:/app/src/index.ts, code.file.path becomes /C:/app/src/index.ts instead of C:/app/src/index.ts.

Evidence
  • findCallSite only strips file:// then decodeURIs the remainder.
  • For file:///C:/app/src/index.ts that produces /C:/app/src/index.ts.
  • Core already documents and fixes this case in normalizeStackTracePath: strip file://, then remove the leading / when the path matches /[A-Z]: so file:///C:/foo.js becomes C:/foo.js.
  • This path does not apply that Windows step, so code.file.path is written with the extra slash.

Identified by Warden · code-review · QXB-BTW

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 4 not successful, 59 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E cloudflare-workers-without-tracing-send-to-sentry Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-express-mcp-v2 (MCP SDK OpenTelemetry) Test (optional) Failing after 1m Details
⛔ CI: Optional E2E Tests / E2E solidstart-2 (no upload) Test (optional) Cancelled after 15m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E hono-5 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E hono-5 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-14-supabase Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E astro-7 (static trace lifecycle) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E hapi-21 (bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (deno) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-eve (0.x) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (kv-cache) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (no upload) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-eve (content off) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-4-sourcemaps (Vite server) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (development) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 4m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps enabled by Sentry) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (manual upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (cloudflare) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (manual upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E hono-5 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (no upload) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry (static trace lifecycle) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (bun) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E solidstart-2 (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E vinext-cf-workers (workers cache) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps (delete all) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 1m Details

This branch has not been deployed

No deployments
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.

1 participant