Skip to content

refactor(tracing): extract private startSpan helper to eliminate boilerplate in span factory functions#7535

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-fix-span-factory-functions
Jun 14, 2026
Merged

refactor(tracing): extract private startSpan helper to eliminate boilerplate in span factory functions#7535
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-fix-span-factory-functions

Conversation

Copilot AI commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

The four Start*Span factory functions in internal/tracing/span_helpers.go shared an identical tracer.Start skeleton, differing only in span name, kind, and attributes — ~40 lines of copy-paste structure.

Changes

  • internal/tracing/span_helpers.go: Extracted a private startSpan helper that centralizes the tracer.Start call with WithAttributes/WithSpanKind options. All four public functions delegate to it while retaining their individual log lines and attribute construction.
// Before — repeated in every public function:
return tracer.Start(ctx, "mcp.tool_call",
    oteltrace.WithAttributes(GenAISystem.String("mcp"), ...),
    oteltrace.WithSpanKind(oteltrace.SpanKindInternal),
)

// After — single private helper:
func startSpan(ctx context.Context, tracer oteltrace.Tracer, spanName string, kind oteltrace.SpanKind, attrs ...attribute.KeyValue) (context.Context, oteltrace.Span) {
    return tracer.Start(ctx, spanName,
        oteltrace.WithAttributes(attrs...),
        oteltrace.WithSpanKind(kind),
    )
}

// Public wrappers become one-liners:
func StartToolCallSpan(...) (context.Context, oteltrace.Span) {
    logTracing.Printf("Starting tool call span: ...")
    return startSpan(ctx, tracer, "mcp.tool_call", oteltrace.SpanKindInternal,
        GenAISystem.String("mcp"), GenAIAgentID.String(serverID), ...
    )
}

No behavioral change — span names, kinds, attributes, and log messages are all preserved exactly.

GitHub Advanced Security started work on behalf of lpcox June 14, 2026 17:23 View session
GitHub Advanced Security finished work on behalf of lpcox June 14, 2026 17:24
Copilot AI changed the title [WIP] Refactor duplicate span factory functions in tracing refactor(tracing): extract private startSpan helper to eliminate boilerplate in span factory functions Jun 14, 2026
Copilot finished work on behalf of lpcox June 14, 2026 17:27
Copilot AI requested a review from lpcox June 14, 2026 17:27
GitHub Advanced Security started work on behalf of lpcox June 14, 2026 17:32 View session
GitHub Advanced Security finished work on behalf of lpcox June 14, 2026 17:33
@lpcox lpcox marked this pull request as ready for review June 14, 2026 17:34
Copilot AI review requested due to automatic review settings June 14, 2026 17:34

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 refactors internal/tracing/span_helpers.go by extracting a private startSpan helper to centralize the common tracer.Start invocation used by the public Start*Span factory functions, reducing boilerplate while keeping span behavior consistent.

Changes:

  • Added a private startSpan(ctx, tracer, spanName, kind, attrs...) helper that applies WithAttributes and WithSpanKind consistently.
  • Updated the four public Start*Span helpers to delegate span creation to startSpan while preserving their existing logging and attribute construction.
Show a summary per file
File Description
internal/tracing/span_helpers.go Extracts shared span-start logic into startSpan and rewires the public span helpers to use it.

Copilot's findings

Tip

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

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

@lpcox lpcox merged commit 93d009b into main Jun 14, 2026
40 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-span-factory-functions branch June 14, 2026 17:45
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.

3 participants