How to Implement Moving Averages in Grafana Dashboards

Better Stack Team
Updated on December 20, 2024

To add moving averages to your Grafana dashboards using popular data sources like Prometheus and InfluxDB.

Begin by accessing your Grafana dashboard. To incorporate a moving average into your visualization, click the Menu button and select Edit to modify your current dashboard.

Create or Edit Panel

Ensure you choose the correct data source from the dropdown menu. Prometheus and InfluxDB are widely favored for implementing moving averages due to their robust support for time-series data.

Choose Data Source

Moving Averages with Prometheus

Prometheus offers the avg_over_time() function, which effectively serves the purpose of calculating moving averages. While it doesn't have a dedicated moving_average() function, avg_over_time() allows you to compute the average of a metric over a specified time window.

For example, to calculate a 5-minute moving average of HTTP requests for the frontend job, you can use the following Prometheus query:

 
avg_over_time(rate(http_requests_total{job="frontend"}[1m])[5m:])

Prometheus does not natively support more sophisticated moving averages like Weighted Moving Averages (WMA) or Exponential Moving Averages (EMA). Consider leveraging Grafana's transformation features or integrating with InfluxDB for these advanced calculations.

Using the moving_average() Function in InfluxDB

To implement a moving average in InfluxDB, you can leverage the moving_average() function directly within your InfluxQL queries. This function simplifies the process of smoothing out data by calculating the average over a specified number of data points, effectively highlighting long-term trends while minimizing short-term fluctuations.

The basic syntax for the moving_average() function is as follows:

 
moving_average(metric, windowSize)

Suppose you want to monitor the memory usage of your application servers to identify long-term usage patterns and mitigate short-term spikes. Calculating a 15-point moving average can provide a clearer view of memory usage trends. Here's how you can structure your InfluxQL query:

 
SELECT moving_average(mean("memory_usage"), 15) 
FROM "system_metrics" 
WHERE time > now() - 3h 
GROUP BY time(10m)

By effectivelyusing the moving_average() function in InfluxDB and adhering to these best practices, you can create insightful and responsive Grafana dashboards.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github