Repository navigation
feat(server-utils): Record the call site on gen_ai request spans - #27090
Draft
sentry-junior[bot] wants to merge 1 commit into
Draft
sentry-junior[bot] wants to merge 1 commit into
sentry-junior[bot] wants to merge 1 commit into
Conversation
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>
Contributor
size-limit report 📦Compared: e5b7eca (PR test merge using develop at 29e62cd) Against: 1c10c6f (develop) · base run #68574
|
| let pastLibrary = !calledFromLibrary; | ||
| for (const callSite of callSites || []) { | ||
| const fileName = callSite.getFileName(); | ||
| const filename = fileName?.startsWith('file://') ? decodeURI(fileName.slice('file://'.length)) : fileName; |
There was a problem hiding this comment.
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
findCallSiteonly stripsfile://thendecodeURIs the remainder.- For
file:///C:/app/src/index.tsthat produces/C:/app/src/index.ts. - Core already documents and fixes this case in
normalizeStackTracePath: stripfile://, then remove the leading/when the path matches/[A-Z]:sofile:///C:/foo.jsbecomesC:/foo.js. - This path does not apply that Windows step, so
code.file.pathis written with the extra slash.
Identified by Warden · code-review · QXB-BTW
Contributor
Optional E2E testsSome optional checks were not successful. They do not block the merge. 4 not successful, 59 successful. See the run.
Successful checks
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Chat, completion and embeddings spans from the OpenAI, Anthropic, Google GenAI and LangChain embeddings integrations now get
code.file.path,code.functionandcode.line.numberfor the line in the app that made the request. Both the client wrappers (instrumentOpenAiClientand 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:
isRecording()check.Error.captureStackTracecollects at most 20 frames. V8 doesn't format them then.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'sstackParsercost 2 to 3 times as much.captureStackTraceboundary, 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:
openaiclient through the channel integration, mockedfetch, whole callThat is in the noise next to a network round trip to an LLM, but it is not free for sampled spans. A
captureCallSite: falsestyle option would be easy to add if we want one.Wrappers: there is no
in_app_excludehere, so an app's own LLM helper becomes the call site for every request that goes through it. Python handles that within_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:
awaits, so no app frame is on the stack. That needs a hook atBaseChatModel.invokeand friends.aipackage's own telemetry.Open questions:
code.functionorcode.function.name? This usescode.functionto match Python. The conventions define both and deprecate neither.invoke_agentand tool spans get a call site too?via @BYK.
--
View Session [Sentry]