Commit 4ac7d7e
authored
Refactor duplicated tracing and HTTP server setup paths in cmd/server packages (#4048)
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.
```go
// 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]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### 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:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>10 files changed
Lines changed: 157 additions & 45 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
| |||
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
22 | | - | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
| |||
121 | 118 | | |
122 | 119 | | |
123 | 120 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
129 | 124 | | |
130 | 125 | | |
131 | 126 | | |
| |||
165 | 160 | | |
166 | 161 | | |
167 | 162 | | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
173 | 171 | | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
179 | 175 | | |
180 | 176 | | |
181 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
311 | 311 | | |
312 | 312 | | |
313 | 313 | | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
320 | 322 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 177 | + | |
181 | 178 | | |
182 | 179 | | |
183 | 180 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 51 | + | |
55 | 52 | | |
0 commit comments