I'm writing a test for a Prometheus alert rule which should fire when a certain rate()
threshold is met. However, the test keeps failing even though the input series and the rule appear correct.
I've managed to produce the following minimal test case:
tests:
- interval: 1m
input_series:
- series: 'total'
values: '0.0+0.01x30 0.3-0.01x30'
promql_expr_test:
- expr: total
eval_time: 20m
exp_samples:
- labels: 'total'
value: 0.2
(The example is a bit forced; I wouldn't normally add a specific value to the expectations in the rules. It's just an example I used to see why a certain arithmetical comparison was failing.)
Why does this test fail?
Prometheus mathematical comparisons are approximate. You should pick a somewhat flexible range for your rate
threshold.
In this particular example, the failure is caused by floating-point arithmetic. You can easily verify that the following test passes (with promtool 2.32):
tests:
- interval: 1m
input_series:
- series: 'total'
values: '0.0+0.01x30 0.3-0.01x30'
promql_expr_test:
- expr: total
eval_time: 20m
exp_samples:
- labels: 'total'
value: 0.20000000000000004