Legend Format Hide Null Labels

I have 2 prometheus labels that I would like to show in my grafana legend, but depending on the values selected in the grafana templating variables 1 of the prometheus labels will or will not exist. I am wondering if there is a way to only show the labels that do exist.

Since my query is using an aggregation, there is nothing contained in the “__name __” label, therefore I am entering my own metric name in my legend, followed by the labels. For example: AVG {{ label1 }} {{ label2 }}

I would like {{ label2 }} to only be present if label2 exists. With my current implementation, when label2 doesn’t exist I end up with something like this in my legend: AVG value1 label2 <- Notice ‘label2’ wasn’t replaced with the string interpolation.

My other thought is to just display the default legend values with some custom text prepended to it, but I couldn’t figure out how to do that.

Thanks for any help

1 Like

there no option for this currently

As I work around, I found that adding a label_replace into my prometheus query allows me to set the __name__ label, therefore, allowing me to leave the grafana legend format blank (default). Here is an example of how I do my query with label_replace:

label_replace(sum(rate(my_histogram_sum{label1=~"$Label1",label2=~"$Label2"}[$Interval])) by (${Aggregation:csv}) / sum(rate(my_histogram_count{label1=~"$Label1",label2=~"$Label2"}[$Interval])) by (${Aggregation:csv}),"__name__","AVG","","")