counter_agg functions simplify this task, which can be difficult to do in pure SQL.
If it’s possible for your readings to decrease as well as increase, use gauge_agg instead.
Two-step aggregation
This group of functions uses the two-step aggregation pattern. Rather than calculating the final result in one step, you first create an intermediate aggregate by using the aggregate function. Then, use any of the accessors on the intermediate aggregate to calculate a final result. You can also roll up multiple intermediate aggregates with the rollup functions. The two-step aggregation pattern has several advantages:- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
Samples
Roll up counter aggregates and calculate deltas
Create daily counter aggregates for a counter with idbar:
Available functions
Aggregate
counter_agg(): aggregate counter data into an intermediate form for further analysis
Accessors
corr(): calculate the correlation coefficient from a counter aggregatecounter_zero_time(): calculate the time when a counter value was zerodelta(): calculate the change in a counter’s valueextrapolated_delta(): estimate the total change in a counter over a time periodextrapolated_rate(): estimate the average rate of change over a time periodfirst_time(): get the timestamp of the first point in a counter aggregatefirst_val(): get the value of the first point in a counter aggregateidelta_left(): calculate the instantaneous change at the left boundaryidelta_right(): calculate the instantaneous change at the right boundaryintercept(): calculate the y-intercept from a counter aggregateinterpolated_delta(): calculate the change over a specific time range with interpolationinterpolated_rate(): calculate the rate of change over a specific time range with interpolationirate_left(): calculate the instantaneous rate at the left boundaryirate_right(): calculate the instantaneous rate at the right boundarylast_time(): get the timestamp of the last point in a counter aggregatelast_val(): get the value of the last point in a counter aggregatenum_changes(): get the number of times the counter changed valuenum_elements(): get the number of points in a counter aggregatenum_resets(): get the number of counter resetsrate(): calculate the average rate of changeslope(): calculate the slope from a counter aggregatetime_delta(): calculate the elapsed time in a counter aggregate
Rollup
rollup(): combine multiple counter aggregates
Mutator
with_bounds(): add time bounds to a counter aggregate for extrapolation