Can you extract a word from within a tag to automatically label the legend?

Hello,

I have some measurements in InfluxDB that I am trying to graph with Grafana. There is a Tag called “friendly_name”, which is in the format:
friendly_name = RHT Sensor (***)
where *** can vary (i.e. Kitchen, Bedroom, etc).

I am using GROUP BY(friendly_name) in the query, which gives the following:

The problem is that I want to rename the legend entries automatically to just show the room name (i.e.Kitchen, Lounge, and Main Bedroom, in this example). How can I do this?

If I just add $tag_friendly_name to the Alias field, then it doesn’t quite work:

I want to remove the RHT Sensor (***) text, and just extract the room name.

Thank you!

@ppxtb

Would it be easier to use an Override, like this?

Possibly - what needs to be typed in to the Display Name box?

Does it need to be some kind of regex, in order to extract the room name?

Thanks

There are many options to set up the override. Have you tried working through any of these?

image

Yes, I tried to choose “Fields with name” set to “value”, because every single one of my data have a field called “value”, and so I chose this because I want to override the display name of all traces. Then I tried some kind of regex in the display name box, but I think I am barking up the wrong tree, and don’t really understand it… sorry, I have never used regex!

Can anyone help me solve this? Thanks!

Hi @ppxtb,
In Transform tab (near Query) you can choose Rename by regex and for Match enter:

.*\(([^\)]+).*

and leave Replace as $1.

Grafana regex

Regex breakdown:

.* - means any symbol 0 or more times (basically means match anything)
\( - specifies that after anything there must be a literal symbol (
( - starts matching group
[^)]+ - means match anything until you find ) symbol
) - closes matching group
.* - matches anything till the end

Replace breakdown

$1 - means return what you matched in first group (in this example only 1 group exists)

For checking regex I suggest regex101 site because there you can see what you matched with regex (marked in colors) and explanations. Few days ago I read that Grafana uses Golang flavor of regex so be sure to click on that on left menu:

Regex101

Data from your example:

/RHTsensors\.first {friendly_name: RHT Sensor (Kitchen)}
/RHTsensors\.first {friendly_name: RHT Sensor (Lounge)}
/RHTsensors\.first {friendly_name: RHT Sensor (Main Bedroom)}

Regex:

.*\(([^\)]+).*

Those links helped me to understand how it’s done:

Best regards,
ldrascic

2 Likes

Works like a charm! Thank you! (Great website resource as well for the Regex testing).

What is the difference between the “Transform → Rename by regex” approach vs “Override → Display Name” approach? What is the purpose of each of these?

You are welcome! :slight_smile:

The main difference is that with “Transform → Rename” you are using regex to match specific part of time series name (in your example you were matching part of friendly_name tag) for all queries (A,B,C… - you only have one) you have in panel, while in “Override → Display Name” you are using regex to match specific fields to which you will apply property override (in this case that is Display Name). Display Name option does not support regex (they would be interpreted as literal symbols as you show us on post 5).

Display name option is useful for example if you are using InfluxDB with Flux language (v 2.x) and you want to change field name to value of specific tag ( for example to change field name to value of host tag use ${__field.labels.host} ). That would act same as ALIAS BY = $tag_host in Influx QL (v.1x)

More about display name can be found on:

Best regards,
ldrascic

1 Like

Hi all,

I understand everything perfectly, but for some reason it is not working correctly for me:

These are my results in grafana:

These are my results in regex101

What am I doing wrong?

Thank you very much

Initial data:

Sample regex:

/(.*):\s(\w{2})"}/

Result:

thanks for the answer, but it still does not work. I don’t know if it is because I am using LOKI as data source, it seems that the problem is in the “space” character and the “:” character. I solved it with this regex

.*\W \W* (\w{2}).*

image