I want to remove a string from the legend format in Grafana

I want to remove _custom_exporter from all the metrics being present on the Grafana dashboard.

How do i put this in legend format?

i should display only OS names.
like ubuntu20_04 instead of ubuntu20_04_custom_exporter.

Can you please help me?

Hi @murarisaranvikas, welcome to the community!

This post in the community has a lot of great information about how to use the value mapping feature with regex to edit values that show up in your graph.

I think we could definitely use more examples, so I mocked up your query with CSV data and
a bar chart to show an example of how you could remove the end of the value name in your bar chart.

To transform this:

Into this, removing the _exporter string at the end:

You can use a value mapping in the bar gauge options in the sidebar of the panel editor.

When you click Add value mappings it opens with a default Value mapping not Regex.

Click the Add a value mapping button, and choose Regex from the dropdown to add the regex value mappng and substitution. You can delete the default value mapping.

The regex (.*)_.* will capture everything up to the last underscore and $1 will substitute that as the new value.

The site https://regex101.com/ is a really helpful tool to test out regex. You’ll want to change the flavor to golang to get the right interpretations for Grafana.

5 Likes

Hi there. Thanks for the great explanation! Unfortunately I have a similar problem but the regex doesn’t work. I need to get the last part of the string as legend entry. Although it is working great on https://regex101.com/, it is still not working in Grafana:

What I am doing wrong?

Thanks ans regards
JLER

This works for me if I use the Transform option in the panel.
image

I am trying to do something similar, but remove a string before a character. Any thoughts on the regex?

1 Like

Hi,

what is your data and what do want to achieve?

Regards,
Fadjar

We found a solution.
I wanted to remove the text after the last space:
some text the-rest-of-it essentially needed to keep the-rest-of-it and remove some text

we managed it using this transform:
image

1 Like

Having this same exact problem. I already setup the mapping:

But the labels are not being mapped

I checked that the regex indeed matches well in regex101

My data source is netdata from prometheus

Got it working. Turns out what I wanted was a transform, not a value mapping: