Skip to content

Refactor duplicated tracing and HTTP server setup paths in cmd/server packages#4048

Merged
lpcox merged 4 commits intomainfrom
copilot/duplicate-code-analysis-report
Apr 18, 2026
Merged

Refactor duplicated tracing and HTTP server setup paths in cmd/server packages#4048
lpcox merged 4 commits intomainfrom
copilot/duplicate-code-analysis-report

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 18, 2026

Duplicate-code analysis identified repeated logic in three areas: OTLP flag registration, tracing provider lifecycle handling, and HTTP server construction. This PR factors those paths into shared helpers while preserving existing behavior in root and proxy command flows.

  • Tracing flags deduplication (internal/cmd)

    • Added registerTracingFlags(...) to centralize OTLP flag registration (otlp-endpoint, otlp-service-name, otlp-sample-rate).
    • Replaced duplicated flag wiring in:
      • internal/cmd/flags_tracing.go
      • internal/cmd/proxy.go
  • Tracing provider lifecycle deduplication (internal/cmd)

    • Added shared helpers:
      • initTracingProviderWithFallback(...)
      • shutdownTracingProviderWithTimeout(...)
    • Reused in both command paths:
      • internal/cmd/root.go
      • internal/cmd/proxy.go
    • This removes parallel fallback/shutdown implementations while keeping existing warning behavior and noop fallback semantics.
  • HTTP server creation deduplication (internal/server)

    • Added newHTTPServer(addr, handler) in internal/server/http_server.go.
    • Replaced repeated &http.Server{Addr, Handler} construction in:
      • internal/server/transport.go
      • internal/server/routed.go
  • Focused test additions

    • Added internal/cmd/tracing_helpers_test.go to verify tracing flag defaults and binding behavior.
    • Added internal/server/http_server_test.go to verify shared HTTP server helper wiring.
// shared tracing flag registration
registerTracingFlags(
    cmd.Flags(),
    &proxyOTLPEndpoint,
    &proxyOTLPService,
    &proxyOTLPSampleRate,
    "OTLP HTTP endpoint for trace export (e.g. http://localhost:4318). Tracing is disabled when empty.",
    "Service name reported in traces.",
    "Fraction of traces to sample and export (0.0–1.0).",
)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /tmp/go-build3127029850/b510/launcher.test /tmp/go-build3127029850/b510/launcher.test -test.testlogfile=/tmp/go-build3127029850/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/auth/auth.go rotocol/go-sdk@v1.5.0/auth/authorization_code.go x_amd64/vet -p github.com/tetra-atomic -lang=go1.24 x_amd64/vet 4815�� g_.a -I x_amd64/vet --gdwarf-5 go-sdk/mcp -o x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3648569470/b514/launcher.test /tmp/go-build3648569470/b514/launcher.test -test.testlogfile=/tmp/go-build3648569470/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3127029850/b492/config.test /tmp/go-build3127029850/b492/config.test -test.testlogfile=/tmp/go-build3127029850/b492/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true mt-go@v0.1.8/format.go mt-go@v0.1.8/parse.go x_amd64/vet --gdwarf-5 backoff -o x_amd64/vet 4815�� g_.a pkg/mod/go.opent-ifaceassert x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3648569470/b496/config.test /tmp/go-build3648569470/b496/config.test -test.testlogfile=/tmp/go-build3648569470/b496/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build3127029850/b510/launcher.test /tmp/go-build3127029850/b510/launcher.test -test.testlogfile=/tmp/go-build3127029850/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/auth/auth.go rotocol/go-sdk@v1.5.0/auth/authorization_code.go x_amd64/vet -p github.com/tetra-atomic -lang=go1.24 x_amd64/vet 4815�� g_.a -I x_amd64/vet --gdwarf-5 go-sdk/mcp -o x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3648569470/b514/launcher.test /tmp/go-build3648569470/b514/launcher.test -test.testlogfile=/tmp/go-build3648569470/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build3127029850/b510/launcher.test /tmp/go-build3127029850/b510/launcher.test -test.testlogfile=/tmp/go-build3127029850/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true rotocol/go-sdk@v1.5.0/auth/auth.go rotocol/go-sdk@v1.5.0/auth/authorization_code.go x_amd64/vet -p github.com/tetra-atomic -lang=go1.24 x_amd64/vet 4815�� g_.a -I x_amd64/vet --gdwarf-5 go-sdk/mcp -o x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3648569470/b514/launcher.test /tmp/go-build3648569470/b514/launcher.test -test.testlogfile=/tmp/go-build3648569470/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3127029850/b519/mcp.test /tmp/go-build3127029850/b519/mcp.test -test.testlogfile=/tmp/go-build3127029850/b519/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true .cfg -I x_amd64/vet -I . -imultiarch x_amd64/vet -W .cfg om/segmentio/enc-ifaceassert x_amd64/vet . g/grpc/internal/--version --64 x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build3648569470/b523/mcp.test /tmp/go-build3648569470/b523/mcp.test -test.testlogfile=/tmp/go-build3648569470/b523/testlog.txt -test.paniconexit0 -test.timeout=10m0s -uns�� -unreachable=false /tmp/go-build3127029850/b025/vet.cfg ache/go/1.25.8/x64/pkg/tool/linux_amd64/compile rnal/filetype/bubash ache/go/1.25.8/x/usr/bin/runc x_amd64/compile ache/go/1.25.8/x64/pkg/tool/linux_amd64/compile -uns�� 7029850/b512/_pkg_.a /tmp/go-build3127029850/b122/vet.cfg 7029850/b512=> .go b/gh-aw-mcpg/int/usr/bin/runc x_amd64/compile /opt/hostedtoolcache/go/1.25.8/xorigin (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI linked an issue Apr 18, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Analyze and resolve duplicate code patterns identified in latest commit Refactor duplicated tracing and HTTP server setup paths in cmd/server packages Apr 18, 2026
Copilot AI requested a review from lpcox April 18, 2026 01:08
@lpcox lpcox marked this pull request as ready for review April 18, 2026 01:14
Copilot AI review requested due to automatic review settings April 18, 2026 01:14
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

Refactors duplicated tracing flag/provider lifecycle logic across the root and proxy commands, and centralizes repeated HTTP server construction in internal/server, with small focused tests to validate the new helpers.

Changes:

  • Deduplicates OTLP tracing flag registration via a shared registerTracingFlags(...) helper.
  • Deduplicates tracing provider init + shutdown-with-timeout via shared helpers reused by both root and proxy.
  • Deduplicates http.Server{Addr, Handler} construction via newHTTPServer(...) and adds targeted unit tests.
Show a summary per file
File Description
internal/server/transport.go Uses shared newHTTPServer helper for MCP HTTP server creation.
internal/server/routed.go Uses shared newHTTPServer helper for routed-mode HTTP server creation.
internal/server/http_server.go Introduces shared newHTTPServer(addr, handler) helper.
internal/server/http_server_test.go Adds unit test coverage for newHTTPServer.
internal/cmd/tracing_helpers.go Introduces shared tracing flag registration + tracing provider init/shutdown helpers.
internal/cmd/tracing_helpers_test.go Adds unit test verifying tracing flag defaults and flag-to-var binding.
internal/cmd/root.go Switches to shared tracing provider init/shutdown helpers.
internal/cmd/proxy.go Switches to shared tracing flag registration and tracing provider init/shutdown helpers.
internal/cmd/flags_tracing.go Replaces duplicated tracing flag wiring with registerTracingFlags.
go.mod Promotes github.com/spf13/pflag to a direct dependency (now imported directly).

Copilot's findings

Tip

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

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

Comment thread internal/cmd/tracing_helpers.go Outdated
Comment on lines +22 to +27
func initTracingProviderWithFallback(ctx context.Context, tracingCfg *config.TracingConfig, warnf func(format string, args ...any)) *tracing.Provider {
tracingProvider, err := tracing.InitProvider(ctx, tracingCfg)
if err != nil {
warnf("failed to initialize tracing provider: %v", err)
tracingProvider, _ = tracing.InitProvider(ctx, nil)
}
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 18, 2026

@copilot address this review feedback #4048 (review)

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 18, 2026

@copilot address this review feedback #4048 (review)

Addressed in 9894603. I updated the shared tracing init helper to accept a caller-provided warning format so each command preserves its original message text/capitalization (root: "Failed...", proxy: "failed..."). Also re-ran go test ./internal/cmd and validation checks.

@lpcox lpcox merged commit 4ac7d7e into main Apr 18, 2026
10 of 11 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-analysis-report branch April 18, 2026 01:48
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 Analysis Report

3 participants