Video in progress

If a service dies or stops sending a metric, why does an "error ratio > 5%" alert stay silent, and how do you close that gap with `absent()`?

When the metric doesn't arrive, the expression has no series and the result is empty. Prometheus treats an empty result as "everything is fine", so the alert doesn't fire. absent() and absent_over_time() turn exactly the "no data" case into a series with value 1, and up == 0 shows a failed scrape.

📖 Answer

An alert rule works on the logic "if the expression returns any series, fire". If all pods are down, a deploy changed the labels, or someone renamed the metric, rate(...) returns nothing. The dashboard is empty and the alert is green.

- alert: PaymentMetricsMissing
  expr: absent(up{job="payment-service"} == 1)
  for: 5m
  labels:
    severity: page
  annotations:
    summary: "payment-service'ning birorta ham tirik target'i yo'q"

- alert: OrdersCounterStale
  expr: absent_over_time(orders_created_total{job="order-service"}[15m])
  for: 0m

The difference:

  • absent(v) returns 1 if v is empty right now;
  • absent_over_time(v[15m]) fires if there was not a single point during the whole window. It is better for metrics that appear rarely;
  • up == 0 means Prometheus knows about the target but couldn't scrape it. If the target disappears from service discovery altogether, up disappears too, which is why you also need absent(up{...}).

In practice I put two rules on every critical service: "no live target" and "the main business counter hasn't been updated for 15 minutes". The second one also catches a deploy that renamed a metric.

A trap: absent() takes labels only from equality (=) matchers in the expression. If you write job=~"pay.*", the alert will have no job label and routing may break.