Back
Open Source

PromQL: a positive duration can still round down to zero

Prometheus #20489 rejects range and subquery-step durations that pass the > 0 check but lose all precision in the millisecond conversion.

JB
Josué Barros
••3 min read
PromQL: a positive duration can still round down to zero

A validation check is only as strong as the value it inspects. PromQL duration expressions were checked as floating-point seconds before being converted to time.Duration. A positive value smaller than one millisecond passed the check and then became zero milliseconds. The parser accepted a duration that could not exist at the resolution PromQL actually uses.

Prometheus #20489 closes that gap for ranges and explicit subquery steps.

The bug

The duration visitor evaluated expressions to a float64 number of seconds. It rejected values that were zero or negative:

if duration <= 0 && !allowedNegative {
    return 0, fmt.Errorf("duration must be greater than 0")
}

Then it converted to the internal duration type:

return time.Duration(duration*1000) * time.Millisecond, nil

The conversion truncates. A value such as 0.00025 seconds is positive, so it passes the first check. Multiplied by 1000, it becomes 0.25; converting to time.Duration truncates that to 0; the result is 0ms.

That is not a cosmetic parsing detail. A zero-length range can become valid, and an explicit subquery step of zero can fall back to the default interval. The expression says one thing; the engine executes another.

The fix

The merged change keeps the early check but adds a second check after conversion. The parameter name also changes from allowedNegative to allowZeroOrNegative, because the exception is not only about negative values: offsets may legitimately evaluate to zero.

result := time.Duration(duration*1000) * time.Millisecond
if result <= 0 && !allowZeroOrNegative {
    return 0, fmt.Errorf("duration must be greater than 0")
}
return result, nil

Ranges and explicit subquery steps now reject a non-positive result after the millisecond conversion. Offsets keep their zero exception.

The distinction matters. A zero offset is a meaningful no-op. A zero range or a zero explicit step is not a useful duration; accepting one changes query behavior.

The tests

The unit test TestCalculateDuration adds two cases:

  • 0.00025 seconds is rejected for a duration expression with duration must be greater than 0;
  • the same value is allowed when zero or negative values are permitted, and evaluates to 0.

The promql test data adds end-to-end failures for the forms that matter:

count_over_time(duration_metric[(1ms/4)])
count_over_time(duration_metric[1m:(1ms/4)])
count_over_time(duration_metric[(1ms/4):1m])

It also keeps the offset case valid:

duration_metric offset (1ms/4)

That is the right shape for a regression: prove the invalid forms fail, and prove the intentional exception still works.

What I keep from this merge

When a value crosses a representation boundary, validate the value in the representation that will be used. Checking seconds and then truncating to milliseconds leaves a gap. The fix is small because it checks the result after conversion, not because the original check was useless.

The PR also shows why parameter names matter. allowedNegative described a narrower rule than the code actually needed. allowZeroOrNegative documents the real exception and makes the offset behavior easier to reason about.

Source: prometheus/prometheus #20489, fixing #20479.

#prometheus#promql#opensource#debugging

Join the Newsletter

Subscribe to get my latest content by email.

We won't send you spam. Unsubscribe at any time.