Skip to content

refactor: extract ensureTracingConfig to eliminate repeated nil-guard#5317

Merged
lpcox merged 2 commits into
mainfrom
copilot/fix-duplicate-tracing-config-guard
May 8, 2026
Merged

refactor: extract ensureTracingConfig to eliminate repeated nil-guard#5317
lpcox merged 2 commits into
mainfrom
copilot/fix-duplicate-tracing-config-guard

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 8, 2026

Three consecutive copies of the same cfg.Gateway.Tracing == nil guard in internal/cmd/root.go — one per OTLP CLI flag — are collapsed into a single lazy-init helper.

Changes

  • internal/cmd/root.go: Add ensureTracingConfig(cfg *config.Config) *config.TracingConfig helper that initializes cfg.Gateway.Tracing on first use and returns it; replace the three duplicated nil-guard blocks with calls to the helper.
// Before — repeated three times
if cfg.Gateway.Tracing == nil {
    cfg.Gateway.Tracing = &config.TracingConfig{}
}
cfg.Gateway.Tracing.Endpoint = otlpEndpoint

// After
ensureTracingConfig(cfg).Endpoint = otlpEndpoint

Any future OTLP flag now needs one line instead of four.

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-build3163137263/b513/launcher.test /tmp/go-build3163137263/b513/launcher.test -test.testlogfile=/tmp/go-build3163137263/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s --de�� g_.a --debug-prefix-m-ifaceassert x_amd64/vet -I /known/anypb -I x_amd64/vet 0IST�� .cfg 2195407/b316/ x_amd64/vet 2195407/b263/sym/opt/hostedtoolcache/go/1.25.9/x64/pkg/tool/linux_amd64/compile -I /tmp/go-build183/tmp/go-build3163137263/b515/_pkg_.a x_amd64/vet (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3163137263/b495/config.test /tmp/go-build3163137263/b495/config.test -test.testlogfile=/tmp/go-build3163137263/b495/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build3163137263/b398/vet.cfg 1.80.0/internal/resolver/dns/internal/internal.go -I x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet -I g_.a -I x_amd64/vet --gdwarf-5 resolver/dns -o x_amd64/vet (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build3163137263/b513/launcher.test /tmp/go-build3163137263/b513/launcher.test -test.testlogfile=/tmp/go-build3163137263/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s --de�� g_.a --debug-prefix-m-ifaceassert x_amd64/vet -I /known/anypb -I x_amd64/vet 0IST�� .cfg 2195407/b316/ x_amd64/vet 2195407/b263/sym/opt/hostedtoolcache/go/1.25.9/x64/pkg/tool/linux_amd64/compile -I /tmp/go-build183/tmp/go-build3163137263/b515/_pkg_.a x_amd64/vet (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build3163137263/b513/launcher.test /tmp/go-build3163137263/b513/launcher.test -test.testlogfile=/tmp/go-build3163137263/b513/testlog.txt -test.paniconexit0 -test.timeout=10m0s --de�� g_.a --debug-prefix-m-ifaceassert x_amd64/vet -I /known/anypb -I x_amd64/vet 0IST�� .cfg 2195407/b316/ x_amd64/vet 2195407/b263/sym/opt/hostedtoolcache/go/1.25.9/x64/pkg/tool/linux_amd64/compile -I /tmp/go-build183/tmp/go-build3163137263/b515/_pkg_.a x_amd64/vet (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build3163137263/b522/mcp.test /tmp/go-build3163137263/b522/mcp.test -test.testlogfile=/tmp/go-build3163137263/b522/testlog.txt -test.paniconexit0 -test.timeout=10m0s -W .cfg olang.org/grpc@v-ifaceassert x_amd64/vet . ions =0 x_amd64/vet .cfg�� 2195407/b387/_pkg_.a 2195407/b316/ x_amd64/vet --gdwarf-5 g/grpc/balancer/--version p=/opt/hostedtoolcache/go/1.25.9-bool x_amd64/vet (dns block)

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

Copilot AI changed the title [WIP] Fix duplicate code pattern for Tracing config nil-guard refactor: extract ensureTracingConfig to eliminate repeated nil-guard May 8, 2026
Copilot finished work on behalf of lpcox May 8, 2026 14:07
Copilot AI requested a review from lpcox May 8, 2026 14:07
@lpcox lpcox marked this pull request as ready for review May 8, 2026 15:33
Copilot AI review requested due to automatic review settings May 8, 2026 15:33
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 CLI tracing flag handling in internal/cmd/root.go by extracting a small helper to lazily initialize cfg.Gateway.Tracing, removing repeated nil-guard blocks and keeping the “CLI flags override config” behavior intact.

Changes:

  • Extracted ensureTracingConfig(cfg) helper to initialize and return cfg.Gateway.Tracing.
  • Replaced three duplicated cfg.Gateway.Tracing == nil blocks (endpoint/service-name/sample-rate) with helper calls.
Show a summary per file
File Description
internal/cmd/root.go Introduces ensureTracingConfig and uses it to de-duplicate tracing flag application logic.

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: 1

Comment thread internal/cmd/root.go
Comment on lines +458 to +463
// ensureTracingConfig returns cfg.Gateway.Tracing, initializing it if nil.
func ensureTracingConfig(cfg *config.Config) *config.TracingConfig {
if cfg.Gateway.Tracing == nil {
cfg.Gateway.Tracing = &config.TracingConfig{}
}
return cfg.Gateway.Tracing
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 8, 2026

@copilot address the reiew feedback

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 Tracing Config Nil-Guard

3 participants