Skip to content

Commit c525a2e

Browse files
fix: handle absent Prometheus series by adding vector(0) fallback to rollout queries
1 parent d21652f commit c525a2e

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎internal/rollout/promql.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
// - agentrax.io/variant=canary (propagated from pod labels via ServiceMonitor)
3939
func requestCountQuery(adName, namespace string, window time.Duration) string {
4040
return fmt.Sprintf(
41-
`sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary"}[%s]))`,
41+
`sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary"}[%s])) or vector(0)`,
4242
namespace, adName, promDuration(window),
4343
)
4444
}
@@ -49,9 +49,9 @@ func requestCountQuery(adName, namespace string, window time.Duration) string {
4949
func errorRateQuery(adName, namespace string, window time.Duration) string {
5050
d := promDuration(window)
5151
return fmt.Sprintf(
52-
`sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary",code=~"5.."}[%s]))`+
52+
`(sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary",code=~"5.."}[%s])) or vector(0))`+
5353
` / on() group_left `+
54-
`clamp_min(sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary"}[%s])), 1)`,
54+
`clamp_min(sum(increase(http_requests_total{namespace=%q,app_kubernetes_io_name=%q,agentrax_io_variant="canary"}[%s])) or vector(0), 1)`,
5555
namespace, adName, d,
5656
namespace, adName, d,
5757
)

‎internal/rollout/promql_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,29 @@ func TestEvaluate_InvalidMaxErrorRate(t *testing.T) {
300300
}
301301
}
302302

303+
// TestEvaluate_ZeroSample_AbsentSeries verifies that when Prometheus returns 0.0
304+
// (e.g. from `or vector(0)` on absent series), Evaluate cleanly flags SampleTooSmall
305+
// without failing or triggering a false breach.
306+
func TestEvaluate_ZeroSample_AbsentSeries(t *testing.T) {
307+
srv := prometheusServer(t, []float64{0.0})
308+
defer srv.Close()
309+
310+
ad := makeAD("agent", "ns", "1%", 200)
311+
result, err := Evaluate(context.Background(), metrics.NewClient(srv.URL), ad, 2*time.Minute)
312+
if err != nil {
313+
t.Fatalf("unexpected error when series evaluate to 0: %v", err)
314+
}
315+
if !result.SampleTooSmall {
316+
t.Errorf("expected SampleTooSmall=true when count is 0, got %v", result.SampleTooSmall)
317+
}
318+
if result.ThresholdBreached {
319+
t.Errorf("expected ThresholdBreached=false when sample is 0, got %v", result.ThresholdBreached)
320+
}
321+
if result.SampleCount != 0 {
322+
t.Errorf("expected SampleCount=0, got %f", result.SampleCount)
323+
}
324+
}
325+
303326
// ── promDuration helper ───────────────────────────────────────────────────────
304327

305328
// TestPromDuration_Format verifies that promDuration formats durations correctly.

0 commit comments

Comments
 (0)