Search code examples
prometheusgrafanametricspromql

How to apply increase function after label_replace?


We have this counter metric: http_server_requests_seconds_count

http_server_requests_seconds_count{instance="localhost:8080", method="GET", outcome="SUCCESS", status="200", uri="/actuator/health"}       20
http_server_requests_seconds_count{instance="localhost:8080", method="GET", outcome="SUCCESS", status="201", uri="/abc/xyz"}   50

We want to write a promql/grafana-ql to create a chart showing how http_server_requests hit at a time range by status. It can be done easily:

sum(increase(http_server_requests_seconds_count)[$__interval]) by (status)

It will create 2 time series on Grafana because we have status="200" and status="201"

But now requirement changed. We want to sum() by 2xx (200, 201,...), 4xx (400, 401, 403,...) and 5xx (500, 502, 503,...):

sum(increase(label_replace(http_server_requests_seconds_count, "status_new", "${1}xx", "status", "(2|3|4|5)(.{2})")[$__interval])) by (status_new)

But it is not working:

Error executing query: invalid parameter "query": 1:114: parse error: ranges only allowed for vector selectors

How can we archive this requirement? Please help! I really need any answer!


Solution

  • You should use label_replace over the result of increase:

    sum(
     label_replace(
      increase(http_server_requests_seconds_count[$__interval]),
      "status_new",  "${1}xx",
      "status",      "(2|3|4|5)(.{2})"
     )
    ) by (status_new)
    

    Alternatively, you can use subquery syntax like this:

    sum(increase(label_replace(http_server_requests_seconds_count, "status_new", "${1}xx", "status", "(2|3|4|5)(.{2})")[$__interval : ])) by (status_new)
    

    (or maybe [$__interval : 1s]). But I would not recommend this for this or any other situation, of using functions before increase or other similat functions unless you are sure it's in fact needed.