Add Prometheus metrics for connections, firewall, L7, and bytes transferred
Instrument mc-proxy with prometheus/client_golang. New internal/metrics/ package defines counters, gauges, and histograms for connection totals, active connections, firewall blocks by reason, backend dial latency, bytes transferred, L7 HTTP status codes, and L7 policy blocks. Optional [metrics] config section starts a scrape endpoint. Firewall gains BlockedWithReason() to report block cause. L7 handler wraps ResponseWriter to record status codes per hostname. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -541,3 +541,54 @@ proxy_protocol = true
|
||||
t.Fatal("expected send_proxy_protocol = true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMetricsConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "test.toml")
|
||||
|
||||
data := `
|
||||
[database]
|
||||
path = "/tmp/test.db"
|
||||
|
||||
[metrics]
|
||||
addr = "127.0.0.1:9090"
|
||||
path = "/metrics"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Metrics.Addr != "127.0.0.1:9090" {
|
||||
t.Fatalf("got metrics.addr %q, want %q", cfg.Metrics.Addr, "127.0.0.1:9090")
|
||||
}
|
||||
if cfg.Metrics.Path != "/metrics" {
|
||||
t.Fatalf("got metrics.path %q, want %q", cfg.Metrics.Path, "/metrics")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMetricsInvalidPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "test.toml")
|
||||
|
||||
data := `
|
||||
[database]
|
||||
path = "/tmp/test.db"
|
||||
|
||||
[metrics]
|
||||
addr = "127.0.0.1:9090"
|
||||
path = "no-slash"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
|
||||
_, err := Load(path)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for metrics.path without leading slash")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user