Skip to content

refactor: extract repeated OTEL span-start patterns into named tracing helpers#6684

Merged
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-fix-span-start-patterns
May 29, 2026
Merged

refactor: extract repeated OTEL span-start patterns into named tracing helpers#6684
lpcox merged 4 commits into
mainfrom
copilot/duplicate-code-fix-span-start-patterns

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 29, 2026

Four near-identical GetTracer().Start(...) blocks across server/unified.go and proxy/handler.go shared the same attribute sets and span-kind declarations with no shared abstraction, creating attribute-ordering drift and a maintenance burden for future spec updates.

Changes

  • internal/tracing/span_helpers.go — four new named constructors, each encoding span name, attributes, and kind:

    • StartToolCallSpanmcp.tool_call (Internal)
    • StartBackendExecuteSpangateway.backend.execute (Client)
    • StartDIFCPipelineSpanproxy.difc_pipeline (Internal)
    • StartProxyForwardSpanproxy.backend.forward (Client)
  • internal/server/unified.go / internal/proxy/handler.go — inline span blocks replaced with the new helpers; now-unused oteltrace imports removed.

  • internal/tracing/span_helpers_test.go — tests for each helper asserting span name, kind, and attribute values.

// Before — repeated at 4 call sites with varying attribute order
ctx, execSpan := us.GetTracer().Start(ctx, "gateway.backend.execute",
    oteltrace.WithAttributes(
        tracing.GenAIToolName.String(toolName),
        tracing.GenAIAgentID.String(serverID),
    ),
    oteltrace.WithSpanKind(oteltrace.SpanKindClient),
)

// After
execCtx, execSpan := tracing.StartBackendExecuteSpan(ctx, us.GetTracer(), serverID, toolName)

Add StartToolCallSpan, StartBackendExecuteSpan, StartDIFCPipelineSpan,
and StartProxyForwardSpan helpers to internal/tracing/span_helpers.go.
Replace the four inline span-start call sites in server/unified.go and
proxy/handler.go with the new helpers.  Add tests for each helper in
span_helpers_test.go.
Copilot AI changed the title [WIP] Refactor duplicate OTEL span start patterns in proxy and server packages refactor: extract repeated OTEL span-start patterns into named tracing helpers May 29, 2026
Copilot finished work on behalf of lpcox May 29, 2026 12:47
Copilot AI requested a review from lpcox May 29, 2026 12:47
@lpcox lpcox marked this pull request as ready for review May 29, 2026 12:49
Copilot AI review requested due to automatic review settings May 29, 2026 12:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 extracts four repeated tracer.Start(...) call patterns from server/unified.go and proxy/handler.go into named constructor helpers in internal/tracing/span_helpers.go, reducing duplication and centralizing span name, kind, and attribute conventions. Tests verify each helper's span name, kind, and attributes via an in-memory exporter.

Changes:

  • Added four new span-start helpers (StartToolCallSpan, StartBackendExecuteSpan, StartDIFCPipelineSpan, StartProxyForwardSpan).
  • Replaced inline GetTracer().Start(...) blocks at four call sites and removed unused oteltrace imports.
  • Added unit tests covering each helper.
Show a summary per file
File Description
internal/tracing/span_helpers.go New named span constructors encoding name/attrs/kind.
internal/tracing/span_helpers_test.go In-memory exporter tests for each new helper.
internal/server/unified.go Replaces two inline span starts with helpers; drops oteltrace import.
internal/proxy/handler.go Replaces two inline span starts with helpers; drops oteltrace import.

Copilot's findings

Tip

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

  • Files reviewed: 4/4 changed files
  • Comments generated: 0

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 29, 2026

@copilot fix failing ci checks https://github.com/github/gh-aw-mcpg/actions/runs/26639206737/job/78507356943?pr=6684 https://github.com/github/gh-aw-mcpg/actions/runs/26639206737/job/78507356989?pr=6...

Fixed in d620dc1 by removing an unused oteltrace import in internal/server/unified.go, which was causing both failing jobs (integration-test build step and lint go vet step). Local make build, make lint, and go test ./internal/server/... now pass.

Copilot finished work on behalf of lpcox May 29, 2026 13:19
@lpcox lpcox merged commit 7c71fc6 into main May 29, 2026
16 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-span-start-patterns branch May 29, 2026 13:25
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.

[duplicate-code] Duplicate Code Pattern: Repeated OTEL Span Start Attribute Patterns in proxy and server packages

3 participants