-1

Please, can somebody explain to me how to get percentage of values greater than 0 in PromQL.

I know that i can get vector values greater than 0 with this: TS_VEC>0. enter image description here

I think i need something like count_over_time(TS_VEC>0[24h])/{count_over_time(TS_VEC[24h])} but it's not work.

Thank you

max
  • 11
  • 3

2 Answers2

1

The following query returns per each time series m the estimated percentage of raw samples greater than 0 during the last day:

avg_over_time(
  (m >bool 0)[1d:10s]
) * 100

This query works in the following way:

  1. It selects all the time series with the name m over the last day. You can use any needed series selector instead of m. This query uses subquery feature.

  2. It generates 24h/10s=8640 points per each selected time series from raw samples over the last 24 hours according to this algorithm.

  3. It substitutes point values from step 2, which are bigger than 0 with 1, while replacing remaining point values with 0 according to >bool comparison. See these docs for details.

  4. It calculates the average value over points returned from step 3, individually per each selected time series. See avg_over_time docs. The average value 0 means that the original time series has no values bigger than 0 during the last 24 hours. The average value 1 means that all the values for a particular time series are bigger than 0 during the last 24 hours.

  5. It multiplies the average value per each matxhing time series by 100 in order to get the percentage of raw samples bigger than 0 over the last 24 hours.

P.S. The query can be simplified to the following one when using VictoriaMetrics - the Prometheus-like monitoring solution I work on:

share_gt_over_time(m[1d], 0) * 100

See share_gt_over_time() docs.

valyala
  • 11,669
  • 1
  • 59
  • 62
  • 1
    thank you very much for your answer and examples. Your solution is works perfectly for my problem. – max Sep 26 '22 at 22:27
0

Have you tried

count(TS_VEC>0)/count(TS_VEC)

Sergio Santiago
  • 1,316
  • 1
  • 11
  • 19
  • thank you for your answer! But it's not working, got no data response with this query. – max Sep 26 '22 at 22:26