How to use the selected period of time in a query?

后端 未结 2 1931
天命终不由人
天命终不由人 2021-01-30 23:51

I\'m using Grafana with Prometheus and I\'d like to build a query that depends on the selected period of time selected in the upper right corner of the screen.

Is there

相关标签:
2条回答
  • 2021-01-31 00:19

    There are two ways that I know:

    1. You can use the $__interval variable like this:

      increase(http_requests_total[$__interval])
      

    There is a drawback that the $__interval variable's value is adjusted by resolution of the graph, but this may also be helpful in some situations.

    1. This approach should fit your case better:

    Go to Dashboard's Templating settings, create new variable with the type of Interval. Enable "Auto Option", adjust "Step count" to be equal 1. Then ensure that the "auto" is selected in corresponding drop-down list at the top of the dashboard.

    Let's assume you name it timeRange, then the query will look like this:

    increase(http_requests_total[$timeRange])
    

    This variable will not be adjusted by graph resolution and if you select "Last 10 hours" its value will be 10h.

    0 讨论(0)
  • 2021-01-31 00:23

    If you are looking at using prometheus as data source, $__range supports your dashboard time is great.

     increase(gin_total_requests[$__range])
    

    Why create your variable when you can use inbuilt Global variable.

    I have added links to documentations to back up my answer Grafan 5.3+

    0 讨论(0)
提交回复
热议问题