I use Prometheus+Grafana and write formulas like:
(
(
sum(
rate(
http_server_requests_seconds_bucket{name="$avalon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri", status!~"5..", le="1.0"}[5m]
)
)+
sum(
rate(
http_server_requests_seconds_bucket{name="$avaon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri", status!~"5..", le="4.0"}[5m]
)
)
)/2
)
/
sum(
rate(
http_server_requests_seconds_count{name="$avalon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri"}[5m]
)
)
Frankly speaking, I'm tired to repeat the same (mostly the same) labels in the selectors.
It would be great if I can write something like:
with{name="$avalon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri"} (
((
sum(rate(http_server_requests_seconds_bucket{status!~"5..",le="1.0"}[5m]))+
sum(rate(http_server_requests_seconds_bucket{status!~"5..",le="4.0"}[5m]))
)/2)
/sum(rate(http_server_requests_seconds_count[5m]))
)
I read the Prometheus manual and didn't realize is there a syntax that will make my formulas less wordy?
Prometheus doesn't provide functionality to write repeated label filters only once. If you need this functionality, then take a look at VictoriaMetrics - this is Prometheus-like monitoring solution I work on. Its' MetricsQL provides e.g. WITH templates
feature, which resolves exactly this issue. For example, the query above can be written in the following way in order to specify repeated label filters only once:
WITH (
cf = {name="$avalon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri"},
cfNo5xx = cf{status!~"5.."},
)
(
(
sum(
rate(
http_server_requests_seconds_bucket{cfNo5xx, le="1.0"}[5m]
)
)+
sum(
rate(
http_server_requests_seconds_bucket{cfNo5xx, le="4.0"}[5m]
)
)
)/2
)
/
sum(
rate(
http_server_requests_seconds_count{cf}[5m]
)
)
Try this query at WITH playground.
The query can be simplified further though:
WITH (
cf = {name="$avalon_apdex_service", kubernetes_namespace="$ns", uri!~"$uri"},
cfNo5xx = cf{status!~"5.."},
sumrate(m) = sum(rate(m[5m])),
)
(
(
sumrate(http_server_requests_seconds_bucket{cfNo5xx, le="1.0"})
+
sumrate(http_server_requests_seconds_bucket{cfNo5xx, le="4.0"})
)/2
)
/
sumrate(http_server_requests_seconds_count{cf})
Try experimenting with this query here.