Commit 6c0e673
committed
feat(#95): agent — metric-source primary selection + per-category overrides
First foundational PR for issue #95 (Phase 3 of #91). Establishes the
"who's the primary metric source on this host" mechanism so future PRs
can drop in nginx / Apache / Caddy / Traefik detectors without re-
architecting how the dashboard picks whose numbers to render.
Background: most hosts have one obvious producer per metric category
(HTTP requests, response codes, etc.). Where two coexist — e.g. HAProxy
fronting nginx, both genuinely serving traffic — the agent now picks
one as primary using a built-in preference list and surfaces the choice
in the capability manifest. The dashboard reads primary_sources and
renders that source's data; alternatives travel along so a "switch
source" UI can offer them. Operators override the pick per-category via
env var for the edge case where auto-detection chooses wrong.
Adds:
- MetricCategory + MetricSourceGear interface (gear/source.go). One
category defined today: CategoryHTTPRequests. The interface is
opt-in — gears that don't produce metrics simply don't implement it.
- preferenceOrder map keyed by category; first-match-wins among
Available producers. Documented rationale for the homelab-flavoured
ordering (HAProxy first as the L7 entry point).
- Manager.ResolvePrimarySources() — resolves the primary for each
category, honouring an operator override if present + valid, falling
back to auto-detection with a warning otherwise. Producers not in
preferenceOrder still surface as alternatives (alphabetised) so
newer gears don't disappear from the UI before someone gets around
to ranking them.
- SourceSelection struct surfaced in CapabilitiesResponse under
primary_sources — fields are stable JSON keys: source, reason,
alternatives.
- Per-category override env var: GEARBOX_AGENT_HTTP_SOURCE. Names a
gear; case-insensitive, trimmed, lowercased at load. Validated
against registered producers; invalid names log a warning and fall
back rather than dropping metrics for the category.
- Startup log line per resolved category so operators can confirm
their override took effect via journalctl without hitting the API.
- HAProxy gear declares CategoryHTTPRequests so the existing source
works end-to-end immediately.
Tests (8 new in gear/source_test.go, 3 new in config_test.go):
- Auto-detect picks first Available from preferenceOrder.
- Operator override wins over preference; reason names env var.
- Override target unavailable → fall back to auto + warning.
- Override target unknown / not a producer for the category → same.
- No available producers → category omitted from result entirely.
- Unranked producer still surfaces as alternative (alphabetised).
- End-to-end /api/v1/system/capabilities includes primary_sources.
- normaliseSourceOverride trims + lowercases edge cases.
- Load() picks up GEARBOX_AGENT_HTTP_SOURCE; default is empty.
Docs: README "Metric-source overrides" section explains the model and
override behaviour, with explicit "auto-detection is the default" copy.
Out of scope (later phases of #95):
- nginx / Apache / Caddy / Traefik / Docker gear stubs.
- Additional metric categories (backend health, container metrics).
- Dashboard consumption of primary_sources.
Refs: docs/research/metrics-source-agnostic.md (#91), issue #95.1 parent 2a3fc24 commit 6c0e673
9 files changed
Lines changed: 701 additions & 4 deletions
File tree
- gearbox-agent
- cmd/gearbox-agent
- internal
- framework
- config
- gear
- gears/haproxy
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
209 | 229 | | |
210 | 230 | | |
211 | 231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
| 386 | + | |
386 | 387 | | |
387 | 388 | | |
388 | 389 | | |
| |||
565 | 566 | | |
566 | 567 | | |
567 | 568 | | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
69 | 84 | | |
70 | 85 | | |
71 | 86 | | |
| |||
161 | 176 | | |
162 | 177 | | |
163 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
164 | 183 | | |
165 | 184 | | |
166 | 185 | | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
167 | 195 | | |
168 | 196 | | |
169 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| |||
0 commit comments