Repository navigation
fix(go-sdk): record the error on a failed stream before ending its span - #1989
basil-k-aji-dev wants to merge 1 commit into
Conversation
Reviewer's GuideImplementationRecord errors before ending the stream span on each setup failure path so the ended span retains its error status and exception event.
TestsAdd failure-path tests for both streaming clients that verify failed spans end with Error status and an exception event.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by Qodofix(go-sdk): record streaming setup errors before ending spans
AI Description
Merge Risk
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can route each severity your way: inline, summary, both, or drop |
Linked issuesChecked at commit b6a3f4a #1982 Bug: Go SDK OpenAI and Anthropic streaming failures end the span before recording the error · 1 of 1 met
|
Issue number: Fixes #1982
Change description:
When a streaming request fails before any chunk is read, the OpenAI and Anthropic Go clients ended the span first and recorded the error afterwards:
The OpenTelemetry SDK ignores
RecordErrorandSetStatuson an ended span, so the span of a failed streaming call finished with status Unset and no exception event. I checked that on its own with a small standalone program against the SDK (end-then-recordgivesstatus=Unset events=0,record-then-endgivesstatus=Error events=1), so it is not an artefact of the tests. The vllm client in the same SDK already uses the right order.This swaps the two calls on all eight failure paths (marshal error, request creation error, transport error and non-200 response, in
openai/streaming.goandanthropic/streaming.go). Error text, returns and the body close are unchanged; on the non-200 path the order is now read body, close, build the error, record, end.Left out on purpose:
gen_ai.client.operation.durationwitherror.typewhen a stream fails to start. The streaming setup functions have no meter, start time or named-return failure block, so that would be new plumbing rather than a reorder. I narrowed the issue to the span status and exception event.event: errorand the stream switch only handles the message and delta events; an OpenAI{"error": ...}data chunk decodes without error. In both cases the span ends Ok. That is a separate change.How was this tested
New subtests in the existing
TestChatCompletion_Streaming_Error_SpanStatus(openai) andTestCreateMessage_Streaming_Error_SpanStatus(anthropic), one per failure path: non-200 (401), transport error (closed server), marshal error (Temperature: math.NaN(), whichjson.Marshalrejects) and request creation error (a base URL with a control character). Each asserts span status Error and anexceptionevent.Red on the unmodified
streaming.go:All pass with the change. I also swapped the order back at each of the eight sites on its own, and each swap fails a test (8 of 8 caught).
go vet ./...andgo test ./...pass insdk/go.-racecould not run on the machine I used.gofmt -llists four files already unformatted onmain(helpers/config.go,helpers/pricing.go,anthropic/types.go,openai/types.go); this PR does not touch them.My other Go SDK PRs (#1986, #1987, #1988) touch the same test files and add similar span-recorder helpers (
setupSpanRecorderis declared identically in three of them), so whichever merges second needs a small rebase, and a duplicate helper has to be dropped. I will do that.Checklist
If your change doesn't seem to apply, please leave them unchecked.
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Summary by Sourcery
Record streaming request failures before ending OpenTelemetry spans in the OpenAI and Anthropic Go clients.
Bug Fixes:
Tests: