Skip to content

Commit f9443da

Browse files
committed
Simplify warnings
Signed-off-by: Filip Petkovski <[email protected]>
1 parent 1822755 commit f9443da

File tree

2 files changed

+75
-64
lines changed

2 files changed

+75
-64
lines changed

execution/aggregate/accumulator.go

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const (
2424
MixedTypeValue
2525
)
2626

27+
type warning error
28+
2729
type accumulator interface {
28-
Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error
30+
Add(v float64, h *histogram.FloatHistogram) (warning, error)
2931
Value() (float64, *histogram.FloatHistogram)
3032
ValueType() ValueType
3133
Reset(float64)
@@ -62,47 +64,43 @@ func (s *sumAcc) AddVector(ctx context.Context, float64s []float64, histograms [
6264
return err
6365
}
6466

65-
func (s *sumAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
67+
func (s *sumAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
6668
if h == nil {
6769
s.hasFloatVal = true
6870
s.value, s.compensation = KahanSumInc(v, s.value, s.compensation)
69-
return nil
71+
return nil, nil
7072
}
7173
if s.histSum == nil {
7274
s.histSum = h.Copy()
73-
return nil
75+
return nil, nil
7476
}
7577
// The histogram being added must have an equal or larger schema.
7678
// https://github.com/prometheus/prometheus/blob/57bcbf18880f7554ae34c5b341d52fc53f059a97/promql/engine.go#L2448-L2456
7779
var err error
7880
if h.Schema >= s.histSum.Schema {
7981
if s.histSum, err = s.histSum.Add(h); err != nil {
8082
if errors.Is(err, histogram.ErrHistogramsIncompatibleSchema) {
81-
warnings.AddToContext(annotations.MixedExponentialCustomHistogramsWarning, ctx)
82-
return nil
83+
return annotations.MixedExponentialCustomHistogramsWarning, nil
8384
}
8485
if errors.Is(err, histogram.ErrHistogramsIncompatibleBounds) {
85-
warnings.AddToContext(annotations.IncompatibleCustomBucketsHistogramsWarning, ctx)
86-
return nil
86+
return annotations.IncompatibleCustomBucketsHistogramsWarning, nil
8787
}
88-
return err
88+
return nil, err
8989
}
9090
} else {
9191
t := h.Copy()
9292
if s.histSum, err = t.Add(s.histSum); err != nil {
9393
if errors.Is(err, histogram.ErrHistogramsIncompatibleSchema) {
94-
warnings.AddToContext(annotations.MixedExponentialCustomHistogramsWarning, ctx)
95-
return nil
94+
return annotations.MixedExponentialCustomHistogramsWarning, nil
9695
}
9796
if errors.Is(err, histogram.ErrHistogramsIncompatibleBounds) {
98-
warnings.AddToContext(annotations.IncompatibleCustomBucketsHistogramsWarning, ctx)
99-
return nil
97+
return annotations.IncompatibleCustomBucketsHistogramsWarning, nil
10098
}
101-
return err
99+
return nil, err
102100
}
103101
s.histSum = t
104102
}
105-
return nil
103+
return nil, nil
106104
}
107105

108106
func (s *sumAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -148,29 +146,33 @@ func (c *maxAcc) AddVector(ctx context.Context, vs []float64, hs []*histogram.Fl
148146
}
149147

150148
fst, rem := vs[0], vs[1:]
151-
if err := c.Add(ctx, fst, nil); err != nil {
149+
if _, err := c.Add(fst, nil); err != nil {
152150
return err
153151
}
154152
if len(rem) == 0 {
155153
return nil
156154
}
157-
return c.Add(ctx, floats.Max(rem), nil)
155+
warn, err := c.Add(floats.Max(rem), nil)
156+
if warn != nil {
157+
warnings.AddToContext(warn, ctx)
158+
}
159+
return err
158160
}
159161

160-
func (c *maxAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
162+
func (c *maxAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
161163
if h != nil {
162-
return nil
164+
return nil, nil
163165
}
164166

165167
if !c.hasValue {
166168
c.value = v
167169
c.hasValue = true
168-
return nil
170+
return nil, nil
169171
}
170172
if c.value < v || math.IsNaN(c.value) {
171173
c.value = v
172174
}
173-
return nil
175+
return nil, nil
174176
}
175177

176178
func (c *maxAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -209,29 +211,33 @@ func (c *minAcc) AddVector(ctx context.Context, vs []float64, hs []*histogram.Fl
209211
}
210212

211213
fst, rem := vs[0], vs[1:]
212-
if err := c.Add(ctx, fst, nil); err != nil {
214+
if _, err := c.Add(fst, nil); err != nil {
213215
return err
214216
}
215217
if len(rem) == 0 {
216218
return nil
217219
}
218-
return c.Add(ctx, floats.Min(rem), nil)
220+
warn, err := c.Add(floats.Min(rem), nil)
221+
if warn != nil {
222+
warnings.AddToContext(warn, ctx)
223+
}
224+
return err
219225
}
220226

221-
func (c *minAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
227+
func (c *minAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
222228
if h != nil {
223-
return nil
229+
return nil, nil
224230
}
225231

226232
if !c.hasValue {
227233
c.value = v
228234
c.hasValue = true
229-
return nil
235+
return nil, nil
230236
}
231237
if c.value > v || math.IsNaN(c.value) {
232238
c.value = v
233239
}
234-
return nil
240+
return nil, nil
235241
}
236242

237243
func (c *minAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -269,10 +275,10 @@ func (c *groupAcc) AddVector(ctx context.Context, vs []float64, hs []*histogram.
269275
return nil
270276
}
271277

272-
func (c *groupAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
278+
func (c *groupAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
273279
c.hasValue = true
274280
c.value = 1
275-
return nil
281+
return nil, nil
276282
}
277283

278284
func (c *groupAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -310,10 +316,10 @@ func (c *countAcc) AddVector(ctx context.Context, vs []float64, hs []*histogram.
310316
return nil
311317
}
312318

313-
func (c *countAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
319+
func (c *countAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
314320
c.hasValue = true
315321
c.value += 1
316-
return nil
322+
return nil, nil
317323
}
318324

319325
func (c *countAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -350,14 +356,14 @@ func newAvgAcc() *avgAcc {
350356
return &avgAcc{}
351357
}
352358

353-
func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
359+
func (a *avgAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
354360
if h != nil {
355361
a.histCount++
356362
if a.histSum == nil {
357363
a.histSum = h.Copy()
358364
a.histScratch = &histogram.FloatHistogram{}
359365
a.histSumScratch = &histogram.FloatHistogram{}
360-
return nil
366+
return nil, nil
361367
}
362368

363369
h.CopyTo(a.histScratch)
@@ -366,17 +372,17 @@ func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram
366372
right := a.histSumScratch.Div(a.histCount)
367373
toAdd, err := left.Sub(right)
368374
if err != nil {
369-
return err
375+
return nil, err
370376
}
371377
a.histSum, err = a.histSum.Add(toAdd)
372-
return err
378+
return nil, err
373379
}
374380

375381
a.count++
376382
if !a.hasValue {
377383
a.hasValue = true
378384
a.kahanSum = v
379-
return nil
385+
return nil, nil
380386
}
381387

382388
a.hasValue = true
@@ -389,7 +395,7 @@ func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram
389395
// group struct and continue with the regular
390396
// calculation of the mean value.
391397
a.kahanSum, a.kahanC = newSum, newC
392-
return nil
398+
return nil, nil
393399
}
394400

395401
// If we are here, we know that the sum _would_ overflow. So
@@ -405,7 +411,7 @@ func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram
405411
// The `floatMean` and `s.F` values are `Inf` of the same sign. They
406412
// can't be subtracted, but the value of `floatMean` is correct
407413
// already.
408-
return nil
414+
return nil, nil
409415
}
410416
if !math.IsInf(v, 0) && !math.IsNaN(v) {
411417
// At this stage, the mean is an infinite. If the added
@@ -414,7 +420,7 @@ func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram
414420
// This is required because our calculation below removes
415421
// the mean value, which would look like Inf += x - Inf and
416422
// end up as a NaN.
417-
return nil
423+
return nil, nil
418424
}
419425
}
420426
currentMean := a.avg + a.kahanC
@@ -424,17 +430,17 @@ func (a *avgAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram
424430
a.avg,
425431
a.kahanC,
426432
)
427-
return nil
433+
return nil, nil
428434
}
429435

430436
func (a *avgAcc) AddVector(ctx context.Context, vs []float64, hs []*histogram.FloatHistogram) error {
431437
for _, v := range vs {
432-
if err := a.Add(ctx, v, nil); err != nil {
438+
if _, err := a.Add(v, nil); err != nil {
433439
return err
434440
}
435441
}
436442
for _, h := range hs {
437-
if err := a.Add(ctx, 0, h); err != nil {
443+
if _, err := a.Add(0, h); err != nil {
438444
if errors.Is(err, histogram.ErrHistogramsIncompatibleSchema) {
439445
// to make valueType NoValue
440446
a.histSum = nil
@@ -518,11 +524,10 @@ func newStdDevAcc() *stdDevAcc {
518524
return &stdDevAcc{}
519525
}
520526

521-
func (s *stdDevAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
527+
func (s *stdDevAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
522528
if h != nil {
523529
// ignore native histogram for STDDEV.
524-
warnings.AddToContext(annotations.NewHistogramIgnoredInAggregationInfo("stddev", posrange.PositionRange{}), ctx)
525-
return nil
530+
return annotations.NewHistogramIgnoredInAggregationInfo("stddev", posrange.PositionRange{}), nil
526531
}
527532

528533
s.hasValue = true
@@ -535,7 +540,7 @@ func (s *stdDevAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistog
535540
s.mean += delta / s.count
536541
s.value += delta * (v - s.mean)
537542
}
538-
return nil
543+
return nil, nil
539544
}
540545

541546
func (s *stdDevAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -557,11 +562,10 @@ func newStdVarAcc() *stdVarAcc {
557562
return &stdVarAcc{}
558563
}
559564

560-
func (s *stdVarAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
565+
func (s *stdVarAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
561566
if h != nil {
562567
// ignore native histogram for STDVAR.
563-
warnings.AddToContext(annotations.NewHistogramIgnoredInAggregationInfo("stdvar", posrange.PositionRange{}), ctx)
564-
return nil
568+
return annotations.NewHistogramIgnoredInAggregationInfo("stdvar", posrange.PositionRange{}), nil
565569
}
566570

567571
s.hasValue = true
@@ -574,7 +578,7 @@ func (s *stdVarAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistog
574578
s.mean += delta / s.count
575579
s.value += delta * (v - s.mean)
576580
}
577-
return nil
581+
return nil, nil
578582
}
579583

580584
func (s *stdVarAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -598,15 +602,14 @@ func newQuantileAcc() accumulator {
598602
return &quantileAcc{}
599603
}
600604

601-
func (q *quantileAcc) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
605+
func (q *quantileAcc) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
602606
if h != nil {
603-
warnings.AddToContext(annotations.NewHistogramIgnoredInAggregationInfo("quantile", posrange.PositionRange{}), ctx)
604-
return nil
607+
return annotations.NewHistogramIgnoredInAggregationInfo("quantile", posrange.PositionRange{}), nil
605608
}
606609

607610
q.hasValue = true
608611
q.points = append(q.points, v)
609-
return nil
612+
return nil, nil
610613
}
611614

612615
func (q *quantileAcc) Value() (float64, *histogram.FloatHistogram) {
@@ -639,7 +642,7 @@ func newHistogramAvg() *histogramAvg {
639642
}
640643
}
641644

642-
func (acc *histogramAvg) Add(ctx context.Context, v float64, h *histogram.FloatHistogram) error {
645+
func (acc *histogramAvg) Add(v float64, h *histogram.FloatHistogram) (warning, error) {
643646
if h == nil {
644647
acc.hasFloat = true
645648
}
@@ -649,17 +652,17 @@ func (acc *histogramAvg) Add(ctx context.Context, v float64, h *histogram.FloatH
649652
var err error
650653
if h.Schema >= acc.sum.Schema {
651654
if acc.sum, err = acc.sum.Add(h); err != nil {
652-
return err
655+
return nil, err
653656
}
654657
} else {
655658
t := h.Copy()
656659
if _, err = t.Add(acc.sum); err != nil {
657-
return err
660+
return nil, err
658661
}
659662
acc.sum = t
660663
}
661664
acc.count++
662-
return nil
665+
return nil, nil
663666
}
664667

665668
func (acc *histogramAvg) Value() (float64, *histogram.FloatHistogram) {

0 commit comments

Comments
 (0)