annotations: histogram counter reset warning includes operation

Signed-off-by: Julius Hinze <julius.hinze@grafana.com>
This commit is contained in:
Julius Hinze 2025-08-20 15:11:44 +02:00
parent 77b5c3f217
commit cdf7208478
No known key found for this signature in database
4 changed files with 24 additions and 12 deletions

View File

@ -2960,7 +2960,7 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram
return 0, nil, false, err
}
if counterResetCollision {
err = annotations.NewHistogramCounterResetCollisionWarning(pos)
err = annotations.NewHistogramCounterResetCollisionWarning(pos, annotations.HistogramAdd)
}
return 0, res.Compact(0), true, err
case parser.SUB:
@ -2969,7 +2969,7 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64, hlhs, hrhs *histogram
return 0, nil, false, err
}
if counterResetCollision {
err = annotations.NewHistogramCounterResetCollisionWarning(pos)
err = annotations.NewHistogramCounterResetCollisionWarning(pos, annotations.HistogramSub)
}
return 0, res.Compact(0), true, err
case parser.EQLC:

View File

@ -264,7 +264,7 @@ func histogramRate(points []HPoint, isCounter bool, metricName string, pos posra
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(pos))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(pos, annotations.HistogramSub))
}
if isCounter {
@ -281,7 +281,7 @@ func histogramRate(points []HPoint, isCounter bool, metricName string, pos posra
}
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(pos))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(pos, annotations.HistogramAdd))
}
}
prev = curr
@ -403,7 +403,7 @@ func instantValue(vals Matrix, args parser.Expressions, out Vector, isRate bool)
return out, annos.Add(annotations.NewIncompatibleCustomBucketsHistogramsWarning(metricName, args.PositionRange()))
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args.PositionRange()))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args.PositionRange(), annotations.HistogramSub))
}
}
resultSample.H.CounterResetHint = histogram.GaugeType
@ -721,14 +721,14 @@ func funcAvgOverTime(_ []Vector, matrixVal Matrix, args parser.Expressions, enh
return mean, err
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange()))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange(), annotations.HistogramSub))
}
_, counterResetCollision, err = mean.Add(toAdd)
if err != nil {
return mean, err
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange()))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange(), annotations.HistogramAdd))
}
}
return mean, nil
@ -927,7 +927,7 @@ func funcSumOverTime(_ []Vector, matrixVal Matrix, args parser.Expressions, enh
return sum, err
}
if counterResetCollision {
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange()))
annos.Add(annotations.NewHistogramCounterResetCollisionWarning(args[0].PositionRange(), annotations.HistogramAdd))
}
}
return sum, nil

View File

@ -121,7 +121,7 @@ func TestHistogramRate_Annotations(t *testing.T) {
},
wantAnnotations: newAnnotations(
annotations.NewNativeHistogramNotGaugeWarning(metricName, pos),
annotations.NewHistogramCounterResetCollisionWarning(pos),
annotations.NewHistogramCounterResetCollisionWarning(pos, annotations.HistogramSub),
),
},
} {

View File

@ -155,7 +155,7 @@ var (
NativeHistogramQuantileNaNResultInfo = fmt.Errorf("%w: input to histogram_quantile has NaN observations, result is NaN", PromQLInfo)
NativeHistogramQuantileNaNSkewInfo = fmt.Errorf("%w: input to histogram_quantile has NaN observations, result is skewed higher", PromQLInfo)
NativeHistogramFractionNaNsInfo = fmt.Errorf("%w: input to histogram_fraction has NaN observations, which are excluded from all fractions", PromQLInfo)
HistogramCounterResetCollisionWarning = fmt.Errorf("%w: conflicting histogram counter resets", PromQLWarning)
HistogramCounterResetCollisionWarning = fmt.Errorf("%w: conflicting counter resets during histogram", PromQLWarning)
)
type annoErr struct {
@ -358,11 +358,23 @@ func NewNativeHistogramFractionNaNsInfo(metricName string, pos posrange.Position
}
}
type HistogramOperation string
const (
HistogramAdd HistogramOperation = "addition"
HistogramSub HistogramOperation = "subtraction"
)
// NewHistogramCounterResetCollisionWarning is used when two counter histograms are added or subtracted where one has
// a CounterReset hint and the other has NotCounterReset.
func NewHistogramCounterResetCollisionWarning(pos posrange.PositionRange) error {
func NewHistogramCounterResetCollisionWarning(pos posrange.PositionRange, operation HistogramOperation) error {
switch operation {
case HistogramAdd, HistogramSub:
default:
operation = "unknown operation"
}
return annoErr{
PositionRange: pos,
Err: fmt.Errorf("%w", HistogramCounterResetCollisionWarning),
Err: fmt.Errorf("%w %s", HistogramCounterResetCollisionWarning, operation),
}
}