Search code examples
prometheusgrafanazero

Grafana graph with prometheus data dynamically starting at zero?


I have a graph over time (mostly "today so far") and would like to start the graph based on prometheus data at zero. Of course the base value changes everytime the range differs so a static value is useless.

Example:

max_over_time(my_metric_counter[${__interval}])

This returns a graph starting somewhere (for example 120 in the current time range). Substracting a static value makes no sense here:

max_over_time(my_metric_counter[${__interval}]) - 120

I already found out what to do - but it took me quite a while. So I want to share this with you...


Solution

  • I am adding a grafana variable as follows:

    Name: my_metric_counter_start_value

    Type: Query

    Hide: Variable

    Datasource: MyPrometheusDatasourceName

    Refresh: "On time range change"

    Query: query_result(min_over_time(my_metric_counter[${__range}]))

    Regex: /.} ([0-9]+) ./

    The I change my graphs query:

    max_over_time(my_metric_counter[${__interval}]) - $my_metric_counter_start_value
    

    And that's it! Hopefully this will help you too ;-)