Calculate age of last timestamp

Hello,

I’m trying to show in Grafana the age of the last timestamp in a measurement, the idea being to alert an operator that the pipeline from data -> influxcdb may have broken. I have a “deadman” tick script/alert working, but thought to compliment it with a dashboard.

If I insert this data:

> insert test some_field_always_present=3
> select * from test
name: test
time                some_field_always_present
----                -------------------------
1600374655095817980 3

I can get the last timestamp via:

> select some_field_always_present from test order by time desc limit 1
name: test
time                some_field_always_present
----                -------------------------
1600374655095817980 3
> 

In reality there are many fields, so I select something that I know will be there to limit the output. I can show this timestamp in Grafana via a table.

What would be really nice is to show the age of this timestamp compared to now.
I started drafting a flux query for this:

last = from(bucket: "telegraf/autogen")
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "test" and r._field=="some_field_always_present")
  |> keep(columns: ["_time", "_value", "_field"])
  |> last()
  |> map(fn: (r) => ({
      time: r._time,
      age: r._time
  }))
  |> yield()

with the expectation that for “age” I can do something like “now() - r._time” - however, nothing I’m trying seems to work.
Any ideas?

Thanks,
Tom

On reflection, the use case might be uneccesarily complex, a grafana single stat showing rows created in the last 5m, 15m 1h will probably suffice.
Would still be interested if this is possible though, imagine I have to play around a bit more with the format of _time and now() so they match…

I stumbled across a Grafana friendly way to do this:

from(bucket: "telegraf/autogen")
  |> range(start: -1d)
  |> filter(fn: (r) => r._measurement == "test" and r._field=="some_field_always_present")
  |> keep(columns: ["_time", "_value", "_field"])
  |> last()
  |> map(fn:(r) => ({_value:uint(v: now()) - uint(v: r._time), _time:r._time, _field:"Time"}))

Then plot a single stat with unit set to nanoseconds:

image

1 Like

Nice!
Then I’ll just leave this as a fyi