Flux calculate rate per second using sample interval

1.3k views Asked by At

The following Flux gives me DNS queries per second, but I'd like to not have a static value for the measurement interval (10 sec in the code below). How could this be done, is there a variable that can be used for this or is there a way to calculate the difference between _time of the samples like difference() does for _value?

from(bucket: "dns")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_pdns")
  |> filter(fn: (r) => r["_field"] == "questions")
  |> difference()  
  |> map(fn: (r) => ({r with _value: r._value / 10.0}))
1

There are 1 answers

0
dmgeurts On

Found the answer:

from(bucket: "dns")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_pdns")
  |> filter(fn: (r) => r["_field"] == "questions")
  |> derivative(unit: 1s, nonNegative: true)

And if you're looking to grab the total between more than one host:

from(bucket: "dns")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_pdns")
  |> filter(fn: (r) => r["_field"] == "questions")
  |> derivative(unit: 1s, nonNegative: true)
  |> drop(columns: ["host"])
  |> group(columns: ["_field", "_time"])
  |> sum()
  |> group(columns: ["_value", "_time"], mode: "except")