Search code examples
prometheusgrafana

Grafana label filter by metric results


Hi I would like to create a label filter for my labels that are based on the result of the metric.

Currently, what I have is something like this:

label_values(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub"}, resourceName)

But I want to filter it based on the healthy/unhealthy resources

label_values(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub"}==0, resourceName)

or

label_values(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub"}==1, resourceName)

To make it more dynamic the idea is to have it as a variable:

label_values(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub"}==$status, resourceName)

But when I try this, it is not working even if I put the value at 0 or 1. I can only filter it based on the values of the attribute.

I also tried to check if there is a way to filter it this way

label_values(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub", value=="0"}, resourceName)

Is there a way to do this?


Solution

  • Official Grafana's documentation says:

    The label_values function doesn’t support queries, so you can use these variables in conjunction with the query_result function to filter variable queries.

    For example of query you mentioned in your question you'll need the following:

    Query: query_result(azure_metric_loadbalancer_heartbeat{subscriptionID=~"$sub"} == $status)
    Regex: /resourceName="([^"]+)"/
    

    Explanation of reges:

    Result of the query would look somthing like

    query_result(azure_metric_loadbalancer_heartbeat{subscriptionID="aeaeaeae-e23e-23e2-32e23e", instance="myinstance.azure.net:8455", resource Name="my-resource"}
    

    You need to extract my-resource from it. For this regex matching resource Name="my-resource" is used, and desired outcome is captured into the group. Grafana the automatically exctracts this group into output.

    • / denotes start and end of the regex,
    • resourceName=" and " match exactly that.
    • ([^"]+) matches any symbol except quote, and captures it into the group.