Search code examples
prometheusgrafana

Grafana Status History with Multiple Metrics


I want to use Grafana's status history visualization to show the working status of different wallets.

I have counter withdrawals_requests_total{currency=$currency}, which gets incremented once there's a new request. And I have another counter withdrawal_wallets_total{currency=$currency, id=$id}, which increments after a successful withdrawal via the specific wallet ID. A wallet may have multiple currencies.

My goal is to have a status history graph with {{currency}}-{{wallet ID}} as row titles. And at a specific time, the status should be:

  • WARN: increase(withdrawal_wallets_total{currency, id}) = 0 or missing AND increase(withdrawals_requests_total{currency}) > 0
  • OK: Otherwise

So the graph should give warning if there're withdrawal requests of a certain currency but a wallet which supports that currency is never used for a period of time.

There's another gauge metric wallet_balance_total{currency, id} whose value is the current wallet balance of a currency. It's updated at a fixed interval and it's a complete list of wallets. I guess I might need it to list all the wallets in the graph.

I'd like to know how can I configure the Grafana panel in order to have such a graph?


Solution

  • If I understand correctly what you need, you can use following query:

    wallet_balance_total{} * 0 +
    on(currency) group_left() (
      (
        increase(withdrawals_requests_total{}[1h]) > 0 #If needed here can be applied *0+1
        unless on(currency) increase(withdrawal_wallets_total{}[1h])>0
      )
      or sum by(currency)(wallet_balance_total)*0
    )
    

    Here:

    • wallet_balance_total is used to get all the label pairs (currency, id). *0 is used to ignore value of the metric,
    • + on(currency) group_left() adding results of RHS, pairing based on currency label and preserving other labels of LHS,
      • expression that returns result only if condition you described in question is met,
      • or sum by(currency)(wallet_balance_total)*0 if previous condition didn't return anything - return 0 with attached labels (hack for promql instead of "left join")

    This query will return result equal to increase of withdrawals_requests_total. If you'd prefer 0 or 1, wrap expression before comment into brackets and apply mentioned *0+1 trick.