Adding the sum of the differences

Hi,

I have a continually increasing counter called diskio_io_time. I want to calculate the difference between the current value and the previous value in time. However this counter has one value for each device mapper in a Linux server (dm-0, dm-1, dm-2, …) and I would like to sum all the differences together.

My flux statement so far is like this:

from(bucket: “bucketname”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “prometheus”)
|> filter(fn: (r) => r[“_field”] == “diskio_io_time”)
|> filter(fn: (r) => r[“host”] == “host.com”)
|> filter(fn: (r) => r[“name”] == “dm-0” or r[“name”] == “dm-11”)
|> filter(fn: (r) => r[“url”] == “https://host.com:9273/metrics”)
|> difference(columns: [“_value”])
|> yield()

Once that is executed, there are now two tables, one marked “0” and the other marked “1”. In each table is a result column with the difference in a column called “_value”

My question is, how can I add these two columns, both called “_value” from the two different tables together?

Many thanks,
Matthew.

Hi @matthew.webster and welcome to the InfluxDB forum.

Does this help?

|> difference(columns: ["_value"])
|> group()  // combines Table 1 and Table 2 so that there is only one "_value" column
|> sum() // adds the "_value" column
|> yield()

Hi Grant,

Thank you for your help.

I went back to Flux basics on the website and listened to the videos and read the introductory material. I came to understand that tables and group keys are fundamental to understanding how to combine and make computations on different counters. Whoever did the ‘Table Grouping Example’ at this page really helped:

Eventually I came up with this solution:

from(bucket: “bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “prometheus”)
|> filter(fn: (r) => r[“_field”] == “diskio_io_time”)
|> filter(fn: (r) => r[“name”] == “dm-0” or r[“name”] == “dm-11”)
|> filter(fn: (r) => r[“url”] == “https://node.com:9273/metrics”)
|> group(columns: [“name”])
|> difference()
|> group(columns: )
|> aggregateWindow(every: v.windowPeriod, fn: sum, createEmpty: false)
|> yield(name: “sum_differences”)

I think you and I both did the group operator after the difference operator. Yours looks more straightforward, meanwhile mine worked. Happy to have mine critiqued.

Many thanks Grant.

Hi @matthew.webster

Nice job. Your query does not contain a sum() function, so you are still getting a time series table, correct? I thought your goal was to sum up ALL of the difference values into a SINGLE value, which is why I included the sum() function.

In my function, there is no aggregateWindow function because (per the above) I wanted to get a single value.

Thanks Grant for the reply and apologies for the delay in responding.

My goal was to sum up the differences for each time period, so I didn’t include the sum() function. I came to understand that the

|> group(columns: )

is the way to sum up the values calculated by the difference function, and that this is not possible with the sum() function. I hope that is correct?

But thank you for providing the way to sum up all the values across multiple time periods. I guess then the flux query would be:

from(bucket: “bucketname”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “prometheus”)
|> filter(fn: (r) => r[“_field”] == “diskio_io_time”)
|> filter(fn: (r) => r[“host”] == “host.com”)
|> filter(fn: (r) => r[“name”] == “dm-0” or r[“name”] == “dm-11”)
|> filter(fn: (r) => r[“url”] == “https://host.com:9273/metrics”)
|> difference(columns: [“_value”])
|> group() // combines Table 1 and Table 2 so that there is only one “_value” column
|> sum() // adds the “_value” column
|> yield()

I tried this and indeed there is one value now, and that value is the sum of the individual differences that I got with my query:


individual

Many thanks,
Matthew.

Hey @matthew.webster

That looks good to me. Does the result seem right to you?