1

Using the new Flux langauge, how best to calculate uptime? My current Flux query looks a bit like this:

from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "process_start_time_seconds")
  |> filter(fn: (r) => r["_field"] == "gauge")
  |> map(fn: (r) => ({
        r with
        _value: (int(v: now()) / 1000000000) - int(v: r._value)
  })
)
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)

This works but seems to be incredibly complex for such a small thing, in Prometheus it's basically one line:

(time() - process_start_time_seconds{job="my-job"})

Is there a way I can improve the Flux query?

roadSurfer
  • 557
  • 1
  • 5
  • 17

1 Answers1

3

I don't think you can simplify it a lot, but here are some ideas:

  1. Store the converted current time in a variable
  2. Don't use aggregateWindow() when you only want to fetch a single value over time
  3. Move the map() as far out as you can for better performance
  4. Use prettier syntax

It could then look like this (just a sketch, not tested for syntax):

currentSeconds = (int(v: now()) / 1000000000)
from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "process_start_time_seconds")
  |> filter(fn: (r) => r._field == "gauge")
  |> last()
  |> map(fn: (r) => ({
        r with _value: currentSeconds - int(v: r._value)
  })
)
ypnos
  • 50,202
  • 14
  • 95
  • 141