Skip to content

Commit

Permalink
Pull MQE functions tests
Browse files Browse the repository at this point in the history
Fix everything related to prometheus/prometheus#15686

We don't support `double_exponential_smoothing` and `predict_linear`, but `deriv` needs to emit a new annotation
  • Loading branch information
julienduchesne committed Jan 8, 2025
1 parent 3328b33 commit cd4912e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/streamingpromql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ func TestCompareVariousMixedMetricsVectorSelectors(t *testing.T) {

for _, labels := range labelCombinations {
labelRegex := strings.Join(labels, "|")
for _, function := range []string{"rate", "increase", "changes", "resets", "deriv", "irate", "idelta"} {
for _, function := range []string{"rate", "increase", "changes", "resets", "irate", "idelta"} {
expressions = append(expressions, fmt.Sprintf(`%s(series{label=~"(%s)"}[45s])`, function, labelRegex))
expressions = append(expressions, fmt.Sprintf(`%s(series{label=~"(%s)"}[1m])`, function, labelRegex))
expressions = append(expressions, fmt.Sprintf(`sum(%s(series{label=~"(%s)"}[2m15s]))`, function, labelRegex))
Expand Down
25 changes: 19 additions & 6 deletions pkg/streamingpromql/operators/functions/range_vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,31 @@ func resetsChanges(isReset bool) RangeVectorStepFunction {
}

var Deriv = FunctionOverRangeVectorDefinition{
SeriesMetadataFunction: DropSeriesName,
StepFunc: deriv,
SeriesMetadataFunction: DropSeriesName,
StepFunc: deriv,
NeedsSeriesNamesForAnnotations: true,
}

func deriv(step *types.RangeVectorStepData, _ float64, _ types.EmitAnnotationFunc) (float64, bool, *histogram.FloatHistogram, error) {
head, tail := step.Floats.UnsafePoints()
func deriv(step *types.RangeVectorStepData, _ float64, emitAnnotation types.EmitAnnotationFunc) (float64, bool, *histogram.FloatHistogram, error) {
fHead, fTail := step.Floats.UnsafePoints()
hHead, hTail := step.Histograms.UnsafePoints()

if (len(head) + len(tail)) < 2 {
haveHistograms := len(hHead) > 0 || len(hTail) > 0

if len(fHead)+len(fTail) == 1 && haveHistograms {
emitAnnotation(annotations.NewHistogramIgnoredInMixedRangeInfo)
return 0, false, nil, nil
}

if (len(fHead) + len(fTail)) < 2 {
return 0, false, nil, nil
}

slope, _ := linearRegression(head, tail, head[0].T)
slope, _ := linearRegression(fHead, fTail, fHead[0].T)

if haveHistograms {
emitAnnotation(annotations.NewHistogramIgnoredInMixedRangeInfo)
}

return slope, true, nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/streamingpromql/testdata/ours/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ load 1m
metric{case="nhcb"} {{schema:-53 sum:1 count:5 custom_values:[5 10] buckets:[1 4]}} {{schema:-53 sum:15 count:2 custom_values:[5 10] buckets:[0 2]}} {{schema:-53 sum:3 count:15 custom_values:[5 10] buckets:[7 8]}} {{schema:-53 sum:3 count:15 custom_values:[5 10] buckets:[0 0]}}
metric{case="floats, nh and nhcb"} 0 1 2 3 2 1 0 _ {{schema:3 sum:0 count:2 buckets:[1 2 1]}} {{schema:3 sum:0 count:1 buckets:[1 2 1]}} {{schema:3 sum:0 count:1 buckets:[1 0 1]}} {{schema:-53 sum:1 count:5 custom_values:[5 10] buckets:[1 4]}} {{schema:-53 sum:15 count:2 custom_values:[5 10] buckets:[0 2]}} {{schema:-53 sum:3 count:15 custom_values:[5 10] buckets:[7 8]}} {{schema:-53 sum:3 count:15 custom_values:[5 10] buckets:[0 0]}}

eval range from 0 to 20m step 1m deriv(metric[3m1s])
eval_info range from 0 to 20m step 1m deriv(metric[3m1s])
{case="all Inf"} _ NaN NaN NaN NaN NaN NaN NaN NaN NaN
{case="all NaN"} _ NaN NaN NaN NaN NaN NaN NaN NaN NaN
{case="all floats 1"} _ 0.016666666666666666 0.016666666666666666 0.016666666666666666 0.016666666666666666 0.016666666666666666 0.016666666666666666 0.016666666666666666 0.016666666666666666
Expand Down
70 changes: 65 additions & 5 deletions pkg/streamingpromql/testdata/upstream/functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,29 @@ clear
load 5m
http_requests{path="/foo"} 0 50 100 150 200
http_requests{path="/bar"} 200 150 100 50 0
http_requests_gauge{path="/foo"} {{schema:0 sum:0 count:0 buckets:[0 0 0] counter_reset_hint:gauge}}+{{schema:0 sum:1 count:2 buckets:[1 1 1] counter_reset_hint:gauge}}x5
http_requests_counter{path="/foo"} {{schema:0 sum:0 count:0 buckets:[0 0 0]}}+{{schema:0 sum:1 count:2 buckets:[1 1 1]}}x5
http_requests_mix{path="/foo"} 0 50 100 {{schema:0 sum:0 count:0 buckets:[0 0 0] counter_reset_hint:gauge}} {{schema:0 sum:1 count:2 buckets:[1 1 1] counter_reset_hint:gauge}}

# Unsupported by streaming engine.
# eval instant at 20m delta(http_requests[20m])
# {path="/foo"} 200
# {path="/bar"} -200

# Unsupported by streaming engine.
# eval instant at 20m delta(http_requests_gauge[20m])
# {path="/foo"} {{schema:0 sum:4 count:8 buckets:[4 4 4]}}

# Unsupported by streaming engine.
# # delta emits warn annotation for non-gauge histogram types.
# eval_warn instant at 20m delta(http_requests_counter[20m])
# {path="/foo"} {{schema:0 sum:4 count:8 buckets:[4 4 4]}}

# Unsupported by streaming engine.
# # delta emits warn annotation for mix of histogram and floats.
# eval_warn instant at 20m delta(http_requests_mix[20m])
# #empty

clear

# Tests for idelta().
Expand All @@ -264,7 +281,8 @@ load 5m
http_requests_total{job="app-server", instance="1", group="canary"} 0+80x10
testcounter_reset_middle_mix 0+10x4 0+10x5 {{schema:0 sum:1 count:1}} {{schema:1 sum:2 count:2}}
http_requests_mix{job="app-server", instance="1", group="canary"} 0+80x10 {{schema:0 sum:1 count:1}}
http_requests_histogram{job="app-server", instance="1", group="canary"} {{schema:0 sum:1 count:2}}x10
http_requests_histogram{job="app-server", instance="1", group="canary"} {{schema:0 sum:1 count:2}}x10
http_requests_inf{job="app-server", instance="1", group="canary"} -Inf 0+80x10 Inf

# deriv should return the same as rate in simple cases.
eval instant at 50m rate(http_requests_total{group="canary", instance="1", job="app-server"}[50m])
Expand All @@ -277,15 +295,20 @@ eval instant at 50m deriv(http_requests_total{group="canary", instance="1", job=
eval instant at 50m deriv(testcounter_reset_middle_total[100m])
{} 0.010606060606060607

# deriv should ignore histograms.
eval instant at 110m deriv(http_requests_mix{group="canary", instance="1", job="app-server"}[110m])
# deriv should ignore histograms in a mixed range of floats and histograms, flagged by an info annotation.
eval_info instant at 110m deriv(http_requests_mix{group="canary", instance="1", job="app-server"}[110m])
{group="canary", instance="1", job="app-server"} 0.26666666666666666

eval instant at 100m deriv(testcounter_reset_middle_mix[110m])
eval_info instant at 100m deriv(testcounter_reset_middle_mix[110m])
{} 0.010606060606060607

# deriv should silently ignore ranges consisting only of histograms.
eval instant at 50m deriv(http_requests_histogram[60m])
#empty
#empty

# deriv should return NaN in case of +Inf or -Inf found.
eval instant at 100m deriv(http_requests_inf[100m])
{job="app-server", instance="1", group="canary"} NaN

# predict_linear should return correct result.
# X/s = [ 0, 300, 600, 900,1200,1500,1800,2100,2400,2700,3000]
Expand Down Expand Up @@ -328,6 +351,25 @@ eval instant at 50m deriv(http_requests_histogram[60m])
# eval instant at 70m predict_linear(testcounter_reset_middle_total[55m] @ 3000, 3600)
# {} 89.54545454545455

# Unsupported by streaming engine.
# # predict_linear should ignore histograms in a mixed range of floats and histograms, flagged by an info annotation.
# eval_info instant at 60m predict_linear(testcounter_reset_middle_mix[60m], 3000)
# {} 70

# Unsupported by streaming engine.
# eval_info instant at 60m predict_linear(testcounter_reset_middle_mix[60m], 50m)
# {} 70

# Unsupported by streaming engine.
# # predict_linear should silently ignore ranges consisting only of histograms.
# eval instant at 60m predict_linear(http_requests_histogram[60m], 50m)
# #empty

# Unsupported by streaming engine.
# # predict_linear should return NaN in case of +Inf or -Inf found.
# eval instant at 100m predict_linear(http_requests_inf[100m], 6000)
# {job="app-server", instance="1", group="canary"} NaN

# With http_requests_total, there is a sample value exactly at the end of
# the range, and it has exactly the predicted value, so predict_linear
# can be emulated with deriv.
Expand Down Expand Up @@ -758,6 +800,11 @@ load 10s
http_requests{job="api-server", instance="1", group="production"} 0+20x1000 200+30x1000
http_requests{job="api-server", instance="0", group="canary"} 0+30x1000 300+80x1000
http_requests{job="api-server", instance="1", group="canary"} 0+40x2000
http_requests_mix{job="api-server", instance="0", group="production"} 0+10x1000 100+30x1000 {{schema:0 count:1 sum:2}}x1000
http_requests_mix{job="api-server", instance="1", group="production"} 0+20x1000 200+30x1000 {{schema:0 count:1 sum:2}}x1000
http_requests_mix{job="api-server", instance="0", group="canary"} 0+30x1000 300+80x1000 {{schema:0 count:1 sum:2}}x1000
http_requests_mix{job="api-server", instance="1", group="canary"} 0+40x2000 {{schema:0 count:1 sum:2}}x1000
http_requests_histogram{job="api-server", instance="1", group="canary"} {{schema:0 count:1 sum:2}}x1000

# Unsupported by streaming engine.
# eval instant at 8000s double_exponential_smoothing(http_requests[1m], 0.01, 0.1)
Expand All @@ -766,6 +813,19 @@ load 10s
# {job="api-server", instance="0", group="canary"} 24000
# {job="api-server", instance="1", group="canary"} 32000

# Unsupported by streaming engine.
# # double_exponential_smoothing should ignore histograms in a mixed range of floats and histograms, flagged by an info annotation.
# eval_info instant at 20010s double_exponential_smoothing(http_requests_mix[1m], 0.01, 0.1)
# {job="api-server", instance="0", group="production"} 30100
# {job="api-server", instance="1", group="production"} 30200
# {job="api-server", instance="0", group="canary"} 80300
# {job="api-server", instance="1", group="canary"} 80000

# Unsupported by streaming engine.
# # double_exponential_smoothing should silently ignore ranges consisting only of histograms.
# eval instant at 10000s double_exponential_smoothing(http_requests_histogram[1m], 0.01, 0.1)
# #empty

# negative trends
clear
load 10s
Expand Down

0 comments on commit cd4912e

Please sign in to comment.