How to Use the Selected Period of Time in a Query?

Better Stack Team
Updated on November 18, 2024

In Prometheus and Grafana, you often want to analyze metrics over a selected period of time. Both platforms offer ways to utilize the selected time range in your queries, allowing you to view metrics and their trends accurately. Here’s how to do it in each tool.

In Prometheus

Prometheus itself doesn’t have a built-in way to dynamically adjust queries based on a selected time range like Grafana does, as it typically returns data for the time range specified in the query. However, you can use Prometheus's range queries to manually specify a time range.

  1. Range Vector Queries: Use a range vector query to get data over a specific period.

     
    http_requests_total[5m]  # This gets the total requests over the last 5 minutes
    
  2. Time Parameters in Queries: You can specify absolute time using the @ operator:

     
    http_requests_total{job="api"}[5m] @ 1633036800  # Get data for the last 5 minutes at a specific timestamp
    

In Grafana

Grafana provides a more user-friendly way to work with time ranges. When you create a dashboard, you can use the time picker to select a range, and Grafana automatically adjusts the queries accordingly.

  1. Using the Time Picker: At the top right of the Grafana dashboard, use the time picker to select your desired time range (e.g., Last 5 minutes, Last 1 hour, Custom range).
  2. Dynamic Time Ranges: In your Prometheus queries in Grafana, you can use the following variables to reflect the selected time range:

    • $__range: Represents the total time range selected in the time picker.
    • $__interval: Automatically calculates an appropriate interval based on the selected time range and the number of data points.

    Here’s an example query:

     
    sum(rate(http_requests_total[$__interval])) by (status)
    

    This query calculates the rate of HTTP requests grouped by status over the selected time range, adjusting the interval dynamically.

  3. Using Variables: You can create custom variables in Grafana that reference the time range. This allows for more complex queries that adapt based on user selections.

Example Use Case in Grafana

  • Setting Up a Panel:

    1. Create a new panel in Grafana.
    2. Use a query like:

       
      rate(http_requests_total[$__interval])
      
 
3. Select a time range using the time picker.
  • Visualizing Trends: This setup allows you to visualize trends in your metrics over the specified time range, dynamically updating as users change the time selection.

Conclusion

While Prometheus allows for manual specification of time ranges in queries, Grafana provides a more dynamic approach to utilize the selected period of time. By leveraging variables and the time picker in Grafana, you can create powerful visualizations that adapt to user input, enhancing your monitoring and analysis capabilities.

Got an article suggestion? Let us know
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