Skip to content

fix(go-sdk): set error.type on the span of a failed non-streaming call - #1988

Open
basil-k-aji-dev wants to merge 2 commits into
openlit:mainfrom
basil-k-aji-dev:fix/go-span-error-type
Open

basil-k-aji-dev wants to merge 2 commits into
openlit:mainfrom
basil-k-aji-dev:fix/go-span-error-type

Conversation

@basil-k-aji-dev

@basil-k-aji-dev basil-k-aji-dev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Issue number: Fixes #1985

Change description:

The Go SDK never set error.type on the span of a failed call. Each instrumentor already computes errType in its deferred failure block, but only attached it to the gen_ai.client.operation.duration metric. 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:

  • anthropic createMessage
  • openai createChatCompletion, createEmbedding and createImage
  • vllm createChatCompletion

defer span.End() is registered before that block in every function, so the block runs first and the attribute lands on the exported span. helpers.RecordError is 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 of span.End() and the error recording there is issue #1982, and error.type on streaming spans would be a follow-up once that lands.

How was this tested

New TestSpanErrorType_NonStreamingFailure in the openai, anthropic and vllm test files: a 429, a 500 and a transport error each, asserting span status Error, one exception event and error.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:

openai_test.go:749: error.type: want "429", got ""
openai_test.go:749: error.type: want "500", got ""
anthropic_test.go:714: error.type: want "429", got ""

They pass with the change. Removing the line from each of the five functions fails its tests. go vet ./... and go test ./... pass in sdk/go, go test -count=3 ./instrumentation/... is stable, and cd cli && go build ./... succeeds against the patched module.

gofmt -l lists four files already unformatted on main (helpers/config.go, helpers/pricing.go, anthropic/types.go, openai/types.go); this PR does not touch them. -race could 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.

  • PR title follows Conventional Commit format.
  • I have reviewed the contributing guidelines
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented

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:

  • Set error.type on spans for failed non-streaming Anthropic, OpenAI, and vLLM requests, keeping span attributes consistent with recorded error metrics.

Tests:

  • Add coverage for HTTP 429, HTTP 500, and transport failures, including OpenAI chat, embedding, and image operations.

@sourcery-ai

sourcery-ai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Attach the computed error type to the span in each non-streaming failure path so span attributes match the existing duration-metric value.

  • sdk/go/instrumentation/anthropic/instrumentor.go L49
  • sdk/go/instrumentation/openai/instrumentor.go L49, L145, L241
  • sdk/go/instrumentation/vllm/instrumentor.go L47

Tests

Add failure-path tests that verify span error status, exception events, and error.type for HTTP and transport errors across the instrumentors.

  • sdk/go/instrumentation/anthropic/anthropic_test.go L17, L661-723
  • sdk/go/instrumentation/openai/openai_test.go L12-19, L671-777
  • sdk/go/instrumentation/vllm/vllm_test.go L20, L729-786

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

fix(go-sdk): set error.type on failed non-streaming spans

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Add error.type to failed non-streaming Go SDK spans, matching the existing duration metric.
• Cover HTTP and transport failures across Anthropic, OpenAI, and vLLM with span assertions.
Merge Risk 🟢 Low

Blast radius 🔴 High 💥 3 capabilities

  • 3 affected capabilities span independent functional scopes: Anthropic client instrumentation integration, OpenAI client instrumentation integration, vLLM client instrumentation integration.
  • This capability is the end-to-end telemetry behavior for failed Anthropic client calls: the changed error defer writes error.type to the request span.
  • It is independent of the OpenAI and vLLM integrations because those instrument different provider clients and requests.
  • Chat, embedding, and image error spans are distinct operation surfaces but share one bounded OpenAI client instrumentation integration and the same telemetry outcome: classifying failed OpenAI calls with error.type.
  • The changed ranges are separate implementation locations for that one end-to-end observability capability.
  • This is the end-to-end telemetry behavior for failed vLLM chat calls.
  • It is an independent functional scope from Anthropic and OpenAI instrumentation because it instruments a separate provider client and request surface.
Behavioral complexity 🟢 Low 🧩 Straightforward

  • Before, the existing error defer derived errType and used it for the error metric; after, it additionally applies the same scalar value to the span.
  • This is a direct transformation using the existing error-classification path, with no new branches, state transitions, ordering protocol, or coordinated invariant.
Potential damage 🟢 Low 🩹 Reversible

  • If this changed behavior regresses, failed Anthropic spans can lose error.type and become less useful for filtering, grouping, and diagnosis.
  • The provider request and returned error behavior remain unchanged.
  • Restoring the code restores future telemetry; previously emitted spans cannot be retroactively enriched, but no persistent application state or external provider effect requires repair.
  • Revert or redeploy the instrumentation code to restore the attribute for future failed calls.
  • Historical telemetry lacking the attribute remains incomplete and would require reprocessing only if the telemetry backend supports it; no application-state repair is indicated.
Assessed commit ccc020b
Diagram

graph TD
  A["Non-streaming calls"] --> B{"Call failed?"} -->|Yes| C["HTTP status or Go type"] --> D["error.type value"] --> E["Client span"]
  D --> F["Duration metric"]
Loading
High-Level Assessment

Use the existing per-call error classification for the span, as this PR does. Centralizing it in a helper would broaden the change without improving these five failure paths; streaming paths remain out of scope.

Files changed (6) +241 / -0

Bug fix (3) +5 / -0
instrumentor.goSet error.type on failed Anthropic message spans +1/-0

Set error.type on failed Anthropic message spans

• Attaches the error classification already used by the duration metric to the non-streaming message span.

sdk/go/instrumentation/anthropic/instrumentor.go

instrumentor.goSet error.type on failed OpenAI operation spans +3/-0

Set error.type on failed OpenAI operation spans

• Attaches the existing metric error classification to failed non-streaming chat, embedding, and image spans.

sdk/go/instrumentation/openai/instrumentor.go

instrumentor.goSet error.type on failed vLLM chat spans +1/-0

Set error.type on failed vLLM chat spans

• Attaches the error classification already used by the duration metric to the non-streaming chat span.

sdk/go/instrumentation/vllm/instrumentor.go

Tests (3) +236 / -0
anthropic_test.goTest Anthropic failure span attributes +64/-0

Test Anthropic failure span attributes

• Adds cases for HTTP 429, HTTP 500, and transport failures. Each checks the ended span's error status, exception event, and error.type value.

sdk/go/instrumentation/anthropic/anthropic_test.go

openai_test.goTest OpenAI failure spans across three operations +113/-0

Test OpenAI failure spans across three operations

• Adds a span recorder and cases for HTTP 429, HTTP 500, and transport failures in chat, embedding, and image calls. The tests check span status, exception count, and error.type.

sdk/go/instrumentation/openai/openai_test.go

vllm_test.goTest vLLM failure span attributes +59/-0

Test vLLM failure span attributes

• Adds HTTP 429, HTTP 500, and transport failure cases that verify span error status, one exception event, and error.type.

sdk/go/instrumentation/vllm/vllm_test.go

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (1) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Streaming failures still lack error.type 📎 Requirement gap ≡ Correctness
Description
The added span.SetAttributes(attribute.String(semconv.ErrorType, errType)) lines cover only the
non-streaming failure blocks, and the streaming failure paths in openai/streaming.go (the
helpers.RecordError calls around lines 40 to 165) never compute an errType, so a failed
streaming call ends with no error.type on its span or its duration metric. The PR description
defers this to a follow-up, but the ticket-derived rule covers every failed OpenAI, Anthropic and
vLLM call span, so streaming failures will keep failing this check until that follow-up lands.
Code

sdk/go/instrumentation/openai/instrumentor.go[49]

+			span.SetAttributes(attribute.String(semconv.ErrorType, errType))
Evidence
Compliance 3 requires each failed call's span to carry error.type matching its metric. The PR adds
the attribute only to non-streaming failure blocks, while the streaming failure paths in
openai/streaming.go call RecordError without setting error.type, and the PR description defers
that work, so the rule is only partly met.

Set error.type on failed Go SDK GenAI spans
sdk/go/instrumentation/openai/streaming.go[36-68]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Failed streaming calls in the Go SDK end their spans with no `error.type` attribute, so they do not meet the rule that every failed OpenAI, Anthropic and vLLM call span carries the same `error.type` as its operation-duration metric.
## Fix Focus Areas
- sdk/go/instrumentation/openai/streaming.go[36-68]
- sdk/go/instrumentation/openai/streaming.go[130-170]
- sdk/go/instrumentation/anthropic/streaming.go[28-70]
- sdk/go/instrumentation/vllm/streaming.go[42-80]
## Recommended Fix
In each streaming failure path, compute the same `errType` used by the non-streaming blocks (HTTP status string for non-200 responses, otherwise `fmt.Sprintf("%T", err)`) and call `span.SetAttributes(attribute.String(semconv.ErrorType, errType))` before `span.End()`. Keep the ordering consistent with the end/record fix tracked in the streaming error-ordering work. If the team keeps this out of scope, document the gap in the PR and track it as a follow-up issue.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can show, collapse, or hide each part of a finding: code, evidence, and all

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@sourcery-ai

sourcery-ai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit ccc020b

#1985 Bug: Go SDK never sets error.type on the span for failed calls · 1 of 2 met

# Requirement Status Notes
1 Set error.type on spans for failed calls in OpenAI, Anthropic and vLLM 🟡 Partly met The non-streaming failure paths are covered, but streaming failures still do not set error.type; the pull request explicitly defers those paths.
2 Use the same error.type value on the span as on the metric ✅ Met Each changed failure block applies the computed errType to both the span and the duration metric.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

if errType == "" {
errType = fmt.Sprintf("%T", retErr)
}
span.SetAttributes(attribute.String(semconv.ErrorType, errType))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

This branch was previously deployed

1 inactive (outdated) deployment
release-notes — 13aa872c Deployed Nov 22, 2026 by basil-k-aji-dev via Normalize / Conventional title #880
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Go SDK never sets error.type on the span for failed calls

1 participant