PromQL get delta result along with current value

Viewed 42

I am using the following promql to retrieve the delta for an interface metric comparing the values now and 1h ago. (And fire an alert when there is an increase):

round(delta(ifInErrors{interfacealias=~"ISP.*|INF.*", datacenter="MyDataCenter"}[1h])) > 0

Is there a way to get the current value of the specific metric as well along with the delta? I am asking this because I want to present in the alert description something like "ifInErrors for device mydevice have increased by delta the last hour and have reached current_value"

1 Answers

You could use the or binary operator to get both values, the small caveat is that very likely your query and the last value of the metric share the same label set, in which case the or operator returns a single operand:

vector1 or vector2 results in a vector that contains all original elements (label sets + values) of vector1 and additionally all elements of vector2 which do not have matching label sets in vector1.

You can likely, work around this by either making sure that either operand (left or right) doesn't contain the same label set as the original metric: using a sum aggregator, for instance, or using label_replace to add a unique label. Something like:

round(delta(ifInErrors{datacenter="MyDataCenter",interfacealias=~"ISP.*|INF.*"}[1h])) > 0
or
label_replace(
    ifInErrors{datacenter="MyDataCenter",interfacealias=~"ISP.*|INF.*"},
    "current",
    "true",
    "instance",
    ".*"
)
Related