How to do count queries with InfluxDB/Grafana/FLUX?


I am using InfluxDB line protocol to monitor the script execution time of my REST API calls.

It is pretty straightforward to display a graph in Grafana showing the MEAN execution time per minute.

But how do you do “count” queries?

(that is, number of records for each minute)

I am using:

  • InfluxDB Cloud 2.0
  • Grafana Cloud
  • FLUX query language

If this were SQL, I’d do a COUNT(*) and GROUP BY. But I’ve tried various FLUX functions but I cannot seem to find the correct syntax to show counts per minute.

Here is some sample line protocol data from my REST API:

restapi,cache=miss,clientname=test,sector=dj elapsed=0.48,lat=28.33,lon=-86.25 1598355594
restapi,cache=miss,clientname=test,sector=dj elapsed=0.81,lat=31.29,lon=-83.23 1598355607
restapi,cache=miss,clientname=test,sector=9m elapsed=1.24,lat=32.52,lon=-122.74 1598355620
restapi,cache=miss,clientname=test,sector=9n elapsed=1.78,lat=35.99,lon=-131.74 1598355629
restapi,cache=miss,clientname=test,sector=9t elapsed=0.61,lat=29.05,lon=-106.31 1598355640
restapi,cache=miss,clientname=test,sector=9r elapsed=0.90,lat=43.49,lon=-120.05 1598355647
restapi,cache=miss,clientname=test,sector=9t elapsed=1.87,lat=30.82,lon=-103.37 1598355655
restapi,cache=miss,clientname=test,sector=9v elapsed=0.10,lat=30.16,lon=-98.39 1598355664
restapi,cache=miss,clientname=test,sector=9z elapsed=1.27,lat=40.70,lon=-100.87 1598355675
restapi,cache=miss,clientname=test,sector=9j elapsed=1.75,lat=30.44,lon=-126.03 1598355686
restapi,cache=miss,clientname=test,sector=9w elapsed=1.22,lat=37.93,lon=-101.77 1598355696
restapi,cache=miss,clientname=test,sector=dj elapsed=1.89,lat=31.27,lon=-87.68 1598355720
restapi,cache=miss,clientname=test,sector=dp elapsed=0.22,lat=44.59,lon=-87.80 1598355733
restapi,cache=miss,clientname=test,sector=9t elapsed=1.64,lat=30.34,lon=-104.75 1598355811
restapi,cache=miss,clientname=test,sector=9y elapsed=1.17,lat=37.22,lon=-99.10 1598355818

This is the FLUX query I use to show the mean execution time per minute in Grafana:

// FLUX query to display MEAN execution time per minute
// (this works)
from(bucket: v.bucket)
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "restapi" and
    r.cache == "miss" and 
    r._field == "elapsed"
  |> drop(columns: ["clientname", "sector"])
  |> aggregateWindow(every: 1m, fn: mean)

What is the syntax to show the counts per minute?