Matt
Matt

Reputation: 7249

Get total number of results or percentage above threshold with promql

I am trying to get a percentage based on a threshold. My data looks like the following:

{
  "request": {
    "url": "api/datasources/proxy/1/api/v1/query_range?query=health&start=1635183780&end=1635184080&step=15",
    "method": "GET",
    "hideFromInspector": false
  },
  "response": {
    "status": "success",
    "data": {
      "resultType": "matrix",
      "result": [
        {
          "metric": {
            "__name__": "health",
            "application": "myapp"
          },
          "values": [
            [
              1635183780,
              "3"
            ],
            [
              1635183795,
              "3"
            ],
            [
              1635183810,
              "3"
            ],
            [
              1635183825,
              "3"
            ],
            [
              1635183840,
              "3"
            ],
            [
              1635183855,
              "3"
            ],
            [
              1635183870,
              "3"
            ],
            [
              1635183885,
              "3"
            ],
            [
              1635183900,
              "3"
            ],
            [
              1635183915,
              "3"
            ],
            [
              1635183930,
              "3"
            ],
            [
              1635183945,
              "3"
            ],
            [
              1635183960,
              "3"
            ],
            [
              1635183975,
              "3"
            ],
            [
              1635183990,
              "3"
            ],
            [
              1635184005,
              "3"
            ],
            [
              1635184020,
              "3"
            ],
            [
              1635184035,
              "3"
            ],
            [
              1635184050,
              "3"
            ],
            [
              1635184065,
              "3"
            ],
            [
              1635184080,
              "3"
            ]
          ]
        }
      ]
    }
  }
}

I want to get the percentage of when the value was 3. So like in this case it is 100%, but if it goes below 3, that would drop the percentage down.

I am using promql in grafana. I have tried to do something like count or count_over_time, but getting an error since the result says it is an instant vector and not range. My understanding is pretty limited, so I'm not sure how to get the total number of values represented here. to even do something like count(health == 3) / count(health)

Upvotes: 0

Views: 2375

Answers (1)

Matt
Matt

Reputation: 7249

I was finally able to figure this out in case anyone has issues.

In grafana, I had to change the type to instant. This will query the api using a query call instead of query_range.

Then you can use count_over_time: count_over_time(health[15m])

I still needed to get this metric where the value is 3, and simply doing count_over_time((health == 3)[15m]) does not work. It's basically missing one thing and that is the step. So, my final query is ended up being: count_over_time((health == 3)[15m:]) and this got me the result I needed.

Upvotes: 1

Related Questions