Search code examples
prometheusgrafana

How to change the legend in a Grafana view


I have a Grafana view panel with a query that returns information for each node in a Kubernetes cluster. When the chart is displayed, the legend shows more information that needed. The query is as follows:

(1 - (node_filesystem_avail_bytes{fstype!="rootfs", mountpoint="/"} / node_filesystem_size_bytes{fstype!="rootfs", mountpoint="/"})) * 100

If I run this query in Prometheus, I get something like this (values are omitted):

{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.10:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-xvz7v", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.11:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-w8cqq", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.12:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-4wfhq", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.13:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-zfskj", service="prometheus-prometheus-node-exporter"}
{container="node-exporter", device="/dev/mapper/ubuntu--vg-ubuntu--lv", endpoint="http-metrics", fstype="ext4", instance="172.20.32.15:9100", job="node-exporter", mountpoint="/", namespace="prometheus", pod="prometheus-prometheus-node-exporter-cllnv", service="prometheus-prometheus-node-exporter"}

This is also what is populating the chart legend, making it very long and more verbose than needed. Each item in the legend is the collection of all labels associated with the metric in the query.

I'd like to be able to pare this information down so that only the labels that people care about are shown in the legend. For example, for this chart, I'd want to show:

fstype="ext4" instance="172.20.32.10" mountpoint="/"
fstype="ext4" instance="172.20.32.11" mountpoint="/"
fstype="ext4" instance="172.20.32.12" mountpoint="/"
fstype="ext4" instance="172.20.32.13" mountpoint="/"
fstype="ext4" instance="172.20.32.15" mountpoint="/"

I've been poking around in Grafana transformations, but haven't found anything that does what I need. I also spent a little time trying to see if a regular expression could somehow be used, but this seems to be too complex.


Solution

  • You can configure text used as legend for query result with query legend configuration.

    It is located under query you want to configure: collapsible menu with name "Options".

    From there you can select either of three variants of legend:

    • Auto - Displays unique labels. Also displays all overlapping labels if a series has multiple labels.
    • Verbose - Displays all label names.
    • Custom - Uses templating to select which labels will be included. For example, {{hostname}} is replaced by the label value for the label hostname. Clear the input and click outside of it to select another mode.

    For format you described in your question you'll need Custom with value

    fstype="{{fstype}}" instance="{{instance}}" mountpoint="{{mountpoint}}"