Skip to content

feat(tracing): OTel improvements from go-fan review - #8841

Merged
lpcox merged 7 commits into
mainfrom
copilot/go-fan-review-go-module
Jul 7, 2026
Merged

feat(tracing): OTel improvements from go-fan review#8841
lpcox merged 7 commits into
mainfrom
copilot/go-fan-review-go-module

Conversation

Copilot AI commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Implements the High/Medium recommendations from the go-fan OTel review: allocation guards for noop spans, explicit error-scrubbing helper, centralised semconv imports, batch processor tuning, and safer fanout shutdown.

Changes

span.IsRecording() guards — unified.go, proxy/handler.go

Wrap AddEvent calls and attribute slice construction with if span.IsRecording() to skip allocations entirely in noop mode (the common case when tracing is disabled).

RecordSpanErrorSafe helper — internal/tracing/span_helpers.go

New helper that records only a scrubbed public message on the span instead of the internal error, making the security intent explicit and preventing accidental leakage of transport/credential details to trace backends:

// Before — ad-hoc scrubbing, easy to get wrong at new call sites
tracing.RecordSpanError(execSpan, fmt.Errorf("tool execution failed"), "tool execution failed")

// After — intent is self-documenting; internal err goes to caller/logs only
tracing.RecordSpanErrorSafe(execSpan, err, "tool execution failed")

Centralised semconv imports — internal/tracing/semconv.go (new)

Single file that imports go.opentelemetry.io/otel/semconv/v1.41.0 and re-exports all attribute keys and helper functions used across the package. All other non-test files (including proxy/handler.go) now reference these re-exports — a future version bump touches one file instead of five.

BatchSpanProcessor tuning — internal/tracing/provider.go

Explicit MaxExportBatchSize=256 + BatchTimeout=2s (SDK defaults are 512 / 5 s) to reduce export tail latency under gateway throughput patterns.

fanoutExporter.Shutdown context propagation — internal/tracing/fanout.go

Each exporter goroutine now receives a fresh context.WithTimeout derived from the parent's remaining deadline at the moment Shutdown is called. This prevents a slow exporter from consuming the entire budget and leaving subsequent exporters with an already-expired context.

Copilot AI linked an issue Jul 7, 2026 that may be closed by this pull request
5 tasks
Copilot AI added 2 commits July 7, 2026 12:47
- Add span.IsRecording() guards around AddEvent calls and attribute
  slice construction in unified.go and proxy/handler.go to eliminate
  allocations in the noop (common) case.
- Add RecordSpanErrorSafe helper that records a scrubbed public error
  on the span instead of the internal error, preventing internal
  details from leaking to trace backends. Migrate the ad-hoc
  fmt.Errorf pattern in unified.go to use the new helper.
- Centralise all semconv/v1.41.0 imports into internal/tracing/semconv.go.
  The new file is the single location for semconv re-exports (attribute
  keys, helper functions, SchemaURL). All other tracing package files and
  the proxy handler now reference these re-exports, so a future semconv
  version bump only requires editing one file.
- Tune BatchSpanProcessor options explicitly: MaxExportBatchSize=256
  and BatchTimeout=2s for better gateway throughput characteristics.
- Improve fanoutExporter.Shutdown context propagation: each exporter
  goroutine now gets a fresh per-exporter context derived from the
  parent's remaining deadline, preventing a slow exporter from
  starving others of their time budget.

Closes #8834
…o clarity

- Fix TestRecordSpanErrorSafe_RecordsPublicMsgNotInternalError: use a
  distinctive sentinel string ("secret-token-12345") in the internal
  error that is also checked in the NotContains assertion, so the test
  unambiguously verifies the internal error detail is not leaked.
- Refactor fanoutExporter.Shutdown context propagation: use an explicit
  early-return path (remaining <= 0 → use parent ctx) and a clear
  non-conditional context.WithTimeout + defer cancel() path for the
  deadline case, eliminating the previously asymmetric conditional block.
Copilot AI changed the title [WIP] Review OpenTelemetry Go SDK for usage in gh-aw feat(tracing): OTel improvements from go-fan review Jul 7, 2026
Copilot finished work on behalf of lpcox July 7, 2026 12:50
Copilot AI requested a review from lpcox July 7, 2026 12:50
@lpcox
lpcox marked this pull request as ready for review July 7, 2026 12:52
Copilot AI review requested due to automatic review settings July 7, 2026 12:52

Copilot AI 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.

Pull request overview

This PR improves the gateway’s OpenTelemetry tracing implementation by reducing allocations when tracing is disabled, centralizing semantic convention usage, making trace error recording explicitly “safe” for security-sensitive paths, tuning batch export behavior, and adjusting fanout shutdown behavior.

Changes:

  • Added span.IsRecording() guards around AddEvent and attribute slice construction to avoid allocations in noop tracing.
  • Introduced RecordSpanErrorSafe and updated an error path to record only a scrubbed/public message in traces.
  • Centralized semconv/v1.41.0 usage behind internal/tracing/semconv.go re-exports and tuned BatchSpanProcessor parameters; updated fanout exporter shutdown behavior.
Show a summary per file
File Description
internal/tracing/span_helpers.go Adds RecordSpanErrorSafe and migrates span helpers to semconv re-exports.
internal/tracing/span_helpers_test.go Updates semconv usage and adds tests for the new safe error-recording helper.
internal/tracing/semconv.go New central semconv re-export module for tracing package callers.
internal/tracing/provider.go Uses semconv re-exports for resource attrs and tunes SDK batch span processor options.
internal/tracing/http.go Switches HTTP span attributes to semconv re-exports.
internal/tracing/genai_attrs.go Clarifies split between semconv-derived GenAI keys (in semconv.go) and custom keys.
internal/tracing/fanout.go Changes shutdown to use per-exporter timeout contexts based on remaining deadline.
internal/server/unified.go Adds IsRecording() guards and uses RecordSpanErrorSafe for transport/tool execution errors.
internal/proxy/handler.go Adds IsRecording() guards and switches HTTP status key usage to semconv re-exports.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 9/9 changed files
  • Comments generated: 3
  • Review effort level: Low

Comment thread internal/tracing/fanout.go
Comment thread internal/tracing/span_helpers.go
Comment thread internal/tracing/span_helpers_test.go
lpcox and others added 3 commits July 7, 2026 06:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox

lpcox commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Copilot AI commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix this failing ci check https://github.com/github/gh-aw-mcpg/actions/runs/28868492598/job/85624699407?pr=8841

Fixed in the latest commit. The CI failure was caused by a missing closing brace } at the end of TestRecordSpanErrorSafe_RecordsPublicMsgNotInternalError in internal/tracing/span_helpers_test.go, which resulted in a syntax error (expected '(', found TestRecordSpanErrorSafe_SetsErrorTypeFromPublicErr).

Copilot finished work on behalf of lpcox July 7, 2026 13:10
@lpcox
lpcox merged commit 1f40eb6 into main Jul 7, 2026
24 checks passed
@lpcox
lpcox deleted the copilot/go-fan-review-go-module branch July 7, 2026 13:14
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.

[go-fan] Go Module Review: go.opentelemetry.io/otel

3 participants