Repository navigation
fix(go-sdk): set error.type on the span of a failed non-streaming call - #1988
basil-k-aji-dev wants to merge 2 commits into
Conversation
Reviewer's GuideImplementationAttach the computed error type to the span in each non-streaming failure path so span attributes match the existing duration-metric value.
TestsAdd failure-path tests that verify span error status, exception events, and error.type for HTTP and transport errors across the instrumentors.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by Qodofix(go-sdk): set error.type on failed non-streaming spans
AI Description
Merge Risk
|
Code Review by Qodo
1. Streaming failures still lack error.type
|
Linked issuesChecked at commit ccc020b #1985 Bug: Go SDK never sets error.type on the span for failed calls · 1 of 2 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/go/instrumentation/openai/instrumentor.go" line_range="49" />
<code_context>
if errType == "" {
errType = fmt.Sprintf("%T", retErr)
}
+ span.SetAttributes(attribute.String(semconv.ErrorType, errType))
operationDurationHistogram.Record(ctx, time.Since(startTime).Seconds(), metric.WithAttributes(
attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
</code_context>
<issue_to_address>
**Streaming failures remain uncovered**
The issue asks for `error.type` on failed calls, but this addition covers only non-streaming failure handling. The Anthropic, OpenAI, and vLLM streaming failure paths still record errors without setting the attribute.
</issue_to_address>
### Comment 2
<location path="sdk/go/instrumentation/openai/openai_test.go" line_range="768-769" />
<code_context>
+ }
+ got := attrString(span, semconv.ErrorType)
+ if tc.want == "" {
+ if !strings.HasPrefix(got, "*") {
+ t.Errorf("error.type: want Go type name, got %q", got)
+ }
+ } else if got != tc.want {
</code_context>
<issue_to_address>
**Wrong transport error types pass**
When a transport-error path records an incorrect pointer-named type such as `*wrong`, the transport-error case in each `TestSpanErrorType_NonStreamingFailure` accepts any `error.type` beginning with `*`; a wrong pointer-named type therefore passes, so these tests do not catch an incorrect error type.
Compare the span attribute with the expected type derived from the returned error, rather than only checking its prefix.
Also at `sdk/go/instrumentation/anthropic/anthropic_test.go:715-716`, `sdk/go/instrumentation/vllm/vllm_test.go:778-779`.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: sdk/go/instrumentation/openai/instrumentor.go:49, sdk/go/instrumentation/openai/openai_test.go:769
| if errType == "" { | ||
| errType = fmt.Sprintf("%T", retErr) | ||
| } | ||
| span.SetAttributes(attribute.String(semconv.ErrorType, errType)) |
There was a problem hiding this comment.
🟡 Medium · Streaming failures remain uncovered
The issue asks for error.type on failed calls, but this addition covers only non-streaming failure handling. The Anthropic, OpenAI, and vLLM streaming failure paths still record errors without setting the attribute.
Prompt for AI agents
In `sdk/go/instrumentation/openai/instrumentor.go` at line 49:
**Streaming failures remain uncovered**
The issue asks for `error.type` on failed calls, but this addition covers only non-streaming failure handling. The Anthropic, OpenAI, and vLLM streaming failure paths still record errors without setting the attribute.From #1985:
Set `error.type` on spans for failed Go SDK calls, using the same value as the metric.
Issue number: Fixes #1985
Change description:
The Go SDK never set
error.typeon the span of a failed call. Each instrumentor already computeserrTypein its deferred failure block, but only attached it to thegen_ai.client.operation.durationmetric. This adds the same value to the span, next to the existing metric code, so a span and its metric agree. The Python SDK already sets it on the span (handle_exception), and OpenTelemetry marks it conditionally required on GenAI spans.One line,
span.SetAttributes(attribute.String(semconv.ErrorType, errType)), in the failure block of:anthropiccreateMessageopenaicreateChatCompletion,createEmbeddingandcreateImagevllmcreateChatCompletiondefer span.End()is registered before that block in every function, so the block runs first and the attribute lands on the exported span.helpers.RecordErroris unchanged.Value, unchanged from the metric: the HTTP status as a string for a non-200 response (
"429","500"), and the Go type of the error otherwise (for example*fmt.wrapError). Python uses the exception class name; I kept the SDK's own metric value so span and metric match.Out of scope: the streaming failure paths in
streaming.go. The ordering ofspan.End()and the error recording there is issue #1982, anderror.typeon streaming spans would be a follow-up once that lands.How was this tested
New
TestSpanErrorType_NonStreamingFailurein the openai, anthropic and vllm test files: a 429, a 500 and a transport error each, asserting span status Error, one exception event anderror.type. For openai it runs through chat, embedding and image. The transport row only checks that the value starts with*, so it does not pin a stdlib-internal type name.Red on the unmodified source, on the attribute value:
They pass with the change. Removing the line from each of the five functions fails its tests.
go vet ./...andgo test ./...pass insdk/go,go test -count=3 ./instrumentation/...is stable, andcd cli && go build ./...succeeds against the patched module.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.-racecould not run on the machine I used.My other Go SDK PRs (#1982, #1983, #1984) touch the same test files and add similar span-recorder helpers, so whichever merges second needs a small rebase. 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 the computed error type on failed non-streaming GenAI spans across the Go SDK instrumentors.
Bug Fixes:
error.typeon spans for failed non-streaming Anthropic, OpenAI, and vLLM requests, keeping span attributes consistent with recorded error metrics.Tests: