How to calculate average request duration?


#1

I want to calculate the average request duration in 1 hour windows. Using the InfluxDB internal metrics as an example, I think this query is close to what I want:

duration = from(bucket: "_internal/monitor")
  |> range(start:-12h)
  |> filter(fn: (r) => r._measurement == "httpd" and (r._field == "writeReqDurationNs"))
  |> window(every:1h)
  |> difference()
  
requests = from(bucket: "_internal/monitor")
  |> range(start:-12h)
  |> filter(fn: (r) => r._measurement == "httpd" and (r._field == "writeReq))
  |> window(every:1h)
  |> difference()
  
join(tables: {duration:duration, requests:requests}, on: ["_time"])
 |> map(fn: (r) => r.duration["_value"] / r.requests["_value"]))
 |> yield()

But it fails with:

Error: failed to create logical plan: duplicate yield name “_result” found on plan node: generated_yield

Any ideas on resolving this error?

Is this the best way to compose this query, or are there better ways?


#2

Give each stream defined in variables a unique yield name. The version of Flux packaged with InfluxDB 1.7.4 is a little older, but I don’t believe this is required any more in newer versions of Flux (which should roll out with the next InfluxDB release.

I also notice the mapping syntax in your join isn’t quite right. This query works, although the data needs to be transformed a bit more to get a useful visualization.

duration = from(bucket: "_internal/monitor")
  |> range(start:-12h)
  |> filter(fn: (r) => r._measurement == "httpd" and (r._field == "writeReqDurationNs"))
  |> window(every:1h)
  |> difference()
  |> yield(name: "duration")

requests = from(bucket: "_internal/monitor")
  |> range(start:-12h)
  |> filter(fn: (r) => r._measurement == "httpd" and (r._field == "writeReq"))
  |> window(every:1h)
  |> difference()
  |> yield(name: "requests")
  
join(tables: {duration:duration, requests:requests}, on: ["_time"])
 |> map(fn: (r) => ({ _value: r._value_duration / r._value_requests }))
 |> yield()