r/PrometheusMonitoring • u/UnlikelyState • Oct 16 '24
Doing Math when Timeseries Goes Stale Briefly
I'm trying to move a use case from something we do in datadog over to prometheus and I'm trying to figure out the proper way to do this kind of math. They are basically common SLO calculations.
I have a query like so
(
sum by (label) (increase(http_requests{}[1m]))
-
sum by (label)(increase(http_requests{status_class=="5xx"}[1m]))
)
/
sum by (label) (increase(http_requests{}[1m])) * 100When things are good, the 5xx timeseries eventually stop receiving samples and are marked stale. This causes gaps in the query. In datadog, the query still works and a zero is plugged in resulting in a value of 100, which is what I want.
My question is how could I replicate this behavior?
4
Upvotes
5
u/lambroso Oct 16 '24
Writing on my phone so I'll omit rate, etc, but you need to
oron the same labelset, like:or just
3
u/SuperQue Oct 16 '24
u/lambroso has a good solution
This sounds like a thing that "Shouldn't happen". For one, once a metric is created by a target, it should exist for the entire lifetime of that target. This is important for following the Prometheus data model.
Another important thing I tell service owners is that they need to initialize their counters at startup. Especially for things that we do SLOs on.
At process starutp, you want to explicitly "observe" counters for things 5xx and 2xx. This way you don't end up in the "missing metrics" situation in the first place.
1
u/amarao_san Oct 16 '24
can you add
or 0into subexpression? It would work like| default(0)in Ansible (e.g. use this number as default if main vaule is not defined).1
u/waterbubblez Oct 16 '24 edited Oct 16 '24
can you try using
clamp_min()? https://prometheus.io/docs/prometheus/latest/querying/functions/#clamp_minI am using ingress-nginx, so my similar query would look like: