feat(google-genai): Support span streaming#6313
feat(google-genai): Support span streaming#6313alexander-alderman-webb wants to merge 7 commits into
Conversation
Codecov Results 📊✅ 76 passed | Total: 76 | Pass Rate: 100% | Execution Time: 8.66s 📊 Comparison with Base Branch
All tests are passing successfully. ❌ Patch coverage is 1.48%. Project has 16563 uncovered lines. Files with missing lines (4)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 26.35% 26.21% -0.14%
==========================================
Files 190 190 —
Lines 22483 22445 -38
Branches 7652 7997 +345
==========================================
+ Hits 5924 5882 -42
- Misses 16559 16563 +4
- Partials 561 548 -13Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit bdf8ded. Configure here.
sentrivana
left a comment
There was a problem hiding this comment.
lgtm, some suggestions
| set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
| set_on_span(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | ||
| set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) | ||
| set_span_data_for_request(chat_span, integration, model_name, contents, kwargs) | ||
| chat_span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True) | ||
| set_on_span(SPANDATA.GEN_AI_RESPONSE_STREAMING, True) |
There was a problem hiding this comment.
We could put these attrs into the attributes dict of the streaming start_span alongside sentry.op and sentry.origin. That way they'd be accessible in people's traces_samplers (since sampling happens right on start_span).
Basically, as long as the attrs are known on start_span, it's good to set them on the span from the get go.
| set_on_span = chat_span.set_data | ||
| chat_span.__enter__() | ||
|
|
||
| set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, "chat") |
| attributes={ | ||
| "sentry.op": OP.GEN_AI_CHAT, | ||
| "sentry.origin": ORIGIN, | ||
| }, | ||
| ) as chat_span: | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) |
There was a problem hiding this comment.
...and here
| attributes={ | |
| "sentry.op": OP.GEN_AI_CHAT, | |
| "sentry.origin": ORIGIN, | |
| }, | |
| ) as chat_span: | |
| chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | |
| chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | |
| chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) | |
| attributes={ | |
| "sentry.op": OP.GEN_AI_CHAT, | |
| "sentry.origin": ORIGIN, | |
| SPANDATA.GEN_AI_OPERATION_NAME: "chat", | |
| SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM, | |
| SPANDATA.GEN_AI_REQUEST_MODEL: model_name, | |
| }, | |
| ) as chat_span: |
| attributes={ | ||
| "sentry.op": OP.GEN_AI_CHAT, | ||
| "sentry.origin": ORIGIN, | ||
| }, | ||
| ) as chat_span: | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | ||
| chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) |
There was a problem hiding this comment.
| attributes={ | |
| "sentry.op": OP.GEN_AI_CHAT, | |
| "sentry.origin": ORIGIN, | |
| }, | |
| ) as chat_span: | |
| chat_span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | |
| chat_span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | |
| chat_span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) | |
| attributes={ | |
| "sentry.op": OP.GEN_AI_CHAT, | |
| "sentry.origin": ORIGIN, | |
| SPANDATA.GEN_AI_OPERATION_NAME: "chat", | |
| SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM, | |
| SPANDATA.GEN_AI_REQUEST_MODEL: model_name | |
| }, | |
| ) as chat_span: |
| attributes={ | ||
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | ||
| "sentry.origin": ORIGIN, | ||
| }, | ||
| ) as span: | ||
| span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") | ||
| span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | ||
| span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) |
There was a problem hiding this comment.
| attributes={ | |
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | |
| "sentry.origin": ORIGIN, | |
| }, | |
| ) as span: | |
| span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") | |
| span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | |
| span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) | |
| attributes={ | |
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | |
| "sentry.origin": ORIGIN, | |
| SPANDATA.GEN_AI_OPERATION_NAME: "embeddings", | |
| SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM, | |
| SPANDATA.GEN_AI_REQUEST_MODEL: model_name | |
| }, | |
| ) as span: |
| attributes={ | ||
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | ||
| "sentry.origin": ORIGIN, | ||
| }, | ||
| ) as span: | ||
| span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") | ||
| span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | ||
| span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) |
There was a problem hiding this comment.
| attributes={ | |
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | |
| "sentry.origin": ORIGIN, | |
| }, | |
| ) as span: | |
| span.set_attribute(SPANDATA.GEN_AI_OPERATION_NAME, "embeddings") | |
| span.set_attribute(SPANDATA.GEN_AI_SYSTEM, GEN_AI_SYSTEM) | |
| span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name) | |
| attributes={ | |
| "sentry.op": OP.GEN_AI_EMBEDDINGS, | |
| "sentry.origin": ORIGIN, | |
| SPANDATA.GEN_AI_OPERATION_NAME: "embeddings", | |
| SPANDATA.GEN_AI_SYSTEM: GEN_AI_SYSTEM, | |
| SPANDATA.GEN_AI_REQUEST_MODEL: model_name, | |
| }, | |
| ) as span: |
| span = sentry_sdk.traces.start_span( | ||
| name=f"execute_tool {tool_name}", | ||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_EXECUTE_TOOL, | ||
| "sentry.origin": ORIGIN, | ||
| }, | ||
| ) | ||
| span.set_attribute(SPANDATA.GEN_AI_TOOL_NAME, tool_name) | ||
| if tool_doc: | ||
| span.set_attribute(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_doc) |
There was a problem hiding this comment.
| span = sentry_sdk.traces.start_span( | |
| name=f"execute_tool {tool_name}", | |
| attributes={ | |
| "sentry.op": OP.GEN_AI_EXECUTE_TOOL, | |
| "sentry.origin": ORIGIN, | |
| }, | |
| ) | |
| span.set_attribute(SPANDATA.GEN_AI_TOOL_NAME, tool_name) | |
| if tool_doc: | |
| span.set_attribute(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_doc) | |
| attributes = { | |
| "sentry.op": OP.GEN_AI_EXECUTE_TOOL, | |
| "sentry.origin": ORIGIN, | |
| SPANDATA.GEN_AI_TOOL_NAME: tool_name, | |
| } | |
| if tool_doc: | |
| attributes[SPANDATA.GEN_AI_TOOL_DESCRIPTION] = tool_doc | |
| span = sentry_sdk.traces.start_span( | |
| name=f"execute_tool {tool_name}", | |
| attributes=attributes, | |
| ) |

Description
Use
sentry_sdk.traces.start_span, replaceSpan.set_data()withStreamedSpan.set_attribute()andSpan.set_status(SPANSTATUS.INTERNAL_ERROR)withStreamedSpan.status = SpanStatus.ERRORwhen in span streaming mode.Parametrize tests on the trace lifecycle option.
Issues
Closes #6024
Reminders
tox -e linters.feat:,fix:,ref:,meta:)