Repository navigation
feat(ai): Record the call site on LLM request spans - #8441
Draft
sentry-junior[bot] wants to merge 2 commits into
Draft
sentry-junior[bot] wants to merge 2 commits into
sentry-junior[bot] wants to merge 2 commits into
Conversation
Set code.file.path, code.function, code.line.number and code.namespace on gen_ai.chat, gen_ai.embeddings, gen_ai.responses and gen_ai.text_completion spans when they start, using the same frame walk as database and HTTP client spans. Recording it at start keeps the request's call site for streamed responses, whose spans end wherever the stream is consumed. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
Contributor
PR risk: mediumFor commit Main concerns: reach, data sent to Sentry, host performance. This label is a hint for reviewers, not an approval. |
Contributor
Coverage Results 📊✅ 57920 passed | ⏭️ 3011 skipped | Total: 60931 | Pass Rate: 95.06% | Execution Time: 158m 30s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 93.75% (15 of 16 changed executable lines covered; target 80%). Changed files with executable lines (2)
Coverage diff@@ Coverage Diff @@
## master #8441 +/-##
==========================================
+ Coverage 90.17% 90.18% +0.01%
==========================================
Files 201 201 —
Tracked lines 22184 22198 +14
Branches 7772 7776 +4
==========================================
+ Hits 20003 20018 +15
+ Misses 2181 2180 -1
+ Partials 1283 1282 -1Generated by Coverage Action |
Python 3.7 reports the last line of a multi-line call as the frame's line number.
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.
LLM request spans now carry
code.file.path,code.function,code.line.numberandcode.namespacefor the first in-app frame that made the request. Sentry can then group LLM calls by where they come from in the user's code, without manual instrumentation.add_gen_ai_sourcereusesadd_source, the frame walk behind DB and HTTP client span source, with the sameproject_root,in_app_includeandin_app_excludeoptions.Scope.start_spancalls it for sampled spans whosesentry.opisgen_ai.chat,gen_ai.embeddings,gen_ai.responsesorgen_ai.text_completion. All AI integrations passsentry.opat span start, so this one hook covers openai, anthropic, google_genai, langchain, litellm, mistral, cohere, huggingface_hub, bedrock and manually created gen_ai spans.The source is taken when the span starts. A streamed response ends its span wherever the stream is consumed, which is often not where the request was made. There is no duration threshold or new option, since one stack walk per LLM request costs microseconds (about 17µs per span start in a micro-benchmark). Other spans pay one dict lookup.
Wrappers: when an app routes every LLM call through one helper, the first in-app frame is always that helper.
add_sourcealready skips modules listed inin_app_exclude, soin_app_exclude=["myapp.llm"]makes the span point at the helper's caller. The conventions define only single-framecode.*attributes, so recording a list of frames would need a new attribute.Not covered: pydantic_ai and openai_agents send model requests from tasks the framework spawns, so no user frame is on the stack and their chat spans get no
code.*. Covering them needs the source captured atAgent.run/Runner.runand carried to child spans. litellm's own span for async calls can also lack a site, because its callbacks run off the caller's stack. The underlying openai span still gets one.Open decisions
in_app_excludethe right lever for wrappers? It also marks those frames as not in-app in error stack traces, which changes grouping for errors raised through the wrapper. The alternative is a new option that only span source reads.enable_*opt-out likeenable_db_query_source?invoke_agentspans get the call site too? That works for langchain/langgraph, but not for the two frameworks above.via @BYK.
--
View Session [Sentry]