From 94d5e0f41cc2adf238e596353578d30b0c8dbf53 Mon Sep 17 00:00:00 2001 From: Innokentii Konstantinov Date: Wed, 11 Jun 2025 21:09:57 +0800 Subject: [PATCH] engine: fix PossibleNonCounterInfo annotation for rate and increase. (#16718) * Comment exposed field Signed-off-by: Innokentii Konstantinov * Remove invalid test Signed-off-by: Innokentii Konstantinov * Fix PossibleNonCounterInfo annotation. Signed-off-by: Innokentii Konstantinov * lint Signed-off-by: Innokentii Konstantinov --------- Signed-off-by: Innokentii Konstantinov --- promql/engine.go | 6 ++-- promql/engine_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 3ea663e2f9..2d13accddc 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1904,9 +1904,9 @@ func (ev *evaluator) eval(ctx context.Context, expr parser.Expr) (parser.Value, if typeLabel != string(model.MetricTypeCounter) { warnings.Add(annotations.NewPossibleNonCounterLabelInfo(metricName, typeLabel, e.Args[0].PositionRange())) } - } else if !strings.HasSuffix(metricName, "_total") || - !strings.HasSuffix(metricName, "_sum") || - !strings.HasSuffix(metricName, "_count") || + } else if !strings.HasSuffix(metricName, "_total") && + !strings.HasSuffix(metricName, "_sum") && + !strings.HasSuffix(metricName, "_count") && !strings.HasSuffix(metricName, "_bucket") { // Fallback to name suffix checking warnings.Add(annotations.NewPossibleNonCounterInfo(metricName, e.Args[0].PositionRange())) diff --git a/promql/engine_test.go b/promql/engine_test.go index 858c02d87f..f352d5999c 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -3537,6 +3537,78 @@ func TestRateAnnotations(t *testing.T) { expectedWarningAnnotations: []string{}, expectedInfoAnnotations: []string{}, }, + "no info annotation when rate() over series with _total suffix": { + data: ` + series_total{label="a"} 1 2 3 + `, + expr: "rate(series_total[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when rate() over series with _sum suffix": { + data: ` + series_sum{label="a"} 1 2 3 + `, + expr: "rate(series_sum[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when rate() over series with _count suffix": { + data: ` + series_count{label="a"} 1 2 3 + `, + expr: "rate(series_count[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when rate() over series with _bucket suffix": { + data: ` + series_bucket{label="a"} 1 2 3 + `, + expr: "rate(series_bucket[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when increase() over series with _total suffix": { + data: ` + series_total{label="a"} 1 2 3 + `, + expr: "increase(series_total[1m1s])", + expectedWarningAnnotations: []string{}, + typeAndUnitLabelsEnabled: false, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when increase() over series with _sum suffix": { + data: ` + series_sum{label="a"} 1 2 3 + `, + expr: "increase(series_sum[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when increase() over series with _count suffix": { + data: ` + series_count{label="a"} 1 2 3 + `, + expr: "increase(series_count[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, + "no info annotation when increase() over series with _bucket suffix": { + data: ` + series_bucket{label="a"} 1 2 3 + `, + expr: "increase(series_bucket[1m1s])", + typeAndUnitLabelsEnabled: false, + expectedWarningAnnotations: []string{}, + expectedInfoAnnotations: []string{}, + }, } for name, testCase := range testCases { t.Run(name, func(t *testing.T) {