package metrics import ( "io" "net/http/httptest" "strings" "testing" "time" ) type batchSrc struct{} func (batchSrc) Snapshot() Snapshot { return Snapshot{} } func TestBatchSeries(t *testing.T) { s := New(batchSrc{}) s.ObserveBatchJob("created") s.ObserveBatchJob("done") s.ObserveBatchJob("done") s.ObserveBatchJob("reused") s.ObserveBatchJob("some raw string") // folded to "other", never a label s.ObserveBatchTarget("ok") s.ObserveBatchTarget("ok") s.ObserveBatchTarget("blocked") s.ObserveBatchTarget("upstream_5xx") s.ObserveBatchTarget("leak me") // folded to "other" s.SetBatchQueueDepth(3) s.SetBatchTargetsInflight(1) s.ObserveBatchJobSeconds(4 * time.Second) s.ObserveBatchTargetSeconds(1500 * time.Millisecond) s.ObserveBatchResultBytes(4096) req := httptest.NewRequest("GET", "/metrics", nil) req.RemoteAddr, req.Host = "127.0.0.1:9", "127.0.0.1:8890" rec := httptest.NewRecorder() s.Handler().ServeHTTP(rec, req) body, _ := io.ReadAll(rec.Body) out := string(body) for _, want := range []string{ `sift_batch_jobs_total{outcome="created"} 1`, `sift_batch_jobs_total{outcome="done"} 2`, `sift_batch_jobs_total{outcome="reused"} 1`, `sift_batch_jobs_total{outcome="other"} 1`, `sift_batch_targets_total{outcome="ok"} 2`, `sift_batch_targets_total{outcome="blocked"} 1`, `sift_batch_targets_total{outcome="upstream_5xx"} 1`, `sift_batch_targets_total{outcome="other"} 1`, `sift_batch_queue_depth 3`, `sift_batch_targets_inflight 1`, `sift_batch_job_seconds_count 1`, `sift_batch_target_seconds_count 1`, `sift_batch_result_bytes_count 1`, } { if !strings.Contains(out, want) { t.Errorf("metrics lack %q", want) } } if strings.Contains(out, "raw") || strings.Contains(out, "leak") { t.Error("a raw outcome string leaked into a label") } }