Metric breach statistic

I have metric in InfluxDB, which is visualized and alerted by Grafana. It has a simple threshold >N. I can select all data points, which breach the threshold with the condition value>N.
But I would like to count a number of breaches.

Any idea about InfluxDB function/selector/math which can be used in the query to count metric breaches?


Do you want to count :

  1. the total number of points over the threshold
  2. the total length of time over the threshold
  3. the number of time it went over the threshold. I mean, if it goes over the threshold, stays there a while then goes under the threshold you only count it as one time.

I want 3.) the number of time it went over the threshold

1.) is a trivial query: SELECT COUNT(value) FROM measurement WHERE value>N
2.) interesting stat - it will be nice to have a query for that as well

Thank you for any ideas.

One way to do it is to use the Discrete plugin for Grafana (

In the Metrics tab, use a query with fill previous or linear.
In Legend tab, check Values, Show Counts.
In mappings, create two range mappings (e.g. From -10000 to N_value Text Normal, From V_value To 10000 Text Alert).
Exit the plugin edit mode and it will reload the states with those you created.
In Color tab, click Add Current Values. Set your Normal and Alert colors as needed.

In Discrete plugin Legend tab, check Values, Show time.

Thanks. That is a calculation on the app (Grafana) level = I have to send all raw datapoints (time grouping and filling may add additional error in this case) to the app (that’s ~50k datapoints/month/timeserie) and the app will calculate stats. It works for small datasets, but there will be a performance/memory problem for bigger datasets (1000 timeseries and yearly stat).

I would like to have a calculation on the InfluxDB level, so my app (Grafana) will receive just one number. Any ideas for an InfluxDB query?