How to alias a template variable value

I have InfluxDB measurements with a tag populated with one of three distinct tag values: “Partner”, “Sandbox”, and “unknown”. (“the first list”) I prompt with a template variable, and then filter by this tag value. All is working as expected.

But, my users are confused by the term “Partner”. They want see the template variable drop down list with the values “Production”, “Sandbox”, and “unknown” (“the second list”) because THOSE are values that they are familiar with.

Any ideas how I could do this? (No, renaming the tag values is not an option.)

One thought I had was to alias those three so that the users see and select one of “the second list”, and the associated one from “the first list” is actually used in the query. But that doesn’t seem to be a feature of template variables, unless someone has a creative way of doing that.

Thoughts?

** InfluxDB user with Grafana 4.6.3 **

Workaround:
Create new “structure” measurement with records with tags (timestamp of the records is not important):

value,text
----------
Partner,Production
Sandbox,Sandbox
unknown,unknown

Create 1st dashboard variable text from text tag values (SHOW TAG VALUES WITH KEY = "text").
Create 2nd hidden dashboard variable value with query SHOW TAG VALUES WITH KEY = "value" WHERE "text"=$text.

=> Grafana will select in the background correct value for your InfluxDB querier - you have to use $value variable in your queries.

1 Like

@jangaraj - your workaround was brilliant. Thanks. It worked like a charm when I mapped the original and translated values in a 1:1 fashion. But, things broke down when I attempted to do one of two things (sorry, I didn’t know these use cases until I had your workaround :roll_eyes:)

  1. map a wild card value, e.g. an All function, or
  2. map 1:n values

I’m going to focus only on the “All” function here, as that will solve my issue. Here is my test case:

a.) build translate table in InfluxDB, with two tags, “xlat” and “orig”

My intention is that the tag “xlat” is the translated value that the user sees, and the tag “orig” is the value that is passed to InfluxDB.

[~]$ influx -database my_database
Connected to http://localhost:8086 version 1.6.3
InfluxDB shell version: 1.6.3

> drop measurement xlat_partner_to_prod
> insert xlat_partner_to_prod,orig=Partner,xlat=Production count=1 0
> insert xlat_partner_to_prod,orig=.*,xlat=All count=1 0
> select orig, xlat, count from xlat_partner_to_prod

name: xlat_partner_to_prod
time orig    xlat       count
---- ----    ----       -----
0    .*      All        1
0    Partner Production 1

b.) template queries In Grafana (v4.6.3)

  • The query for the VISIBLE field is:
    $otype show tag values from xlat_partner_to_prod with key="xlat"
  • The query for the HIDDEN field is:
    $org_wsdl show tag values from xlat_partner_to_prod with key="orig" WHERE "xlat" =~ /$otype/

c.) dashboard testing

When I select otype of “Production”, I see the value in $org_wsdl flip to “Partner”. This is good. Here is the query emitted from Grafana:

SELECT sum("count") FROM "my_measurement" WHERE "org_wsdl" =~ /^Partner$/ AND time >= 1535903435154ms and time ...

When I select otype of “All”, I see the value in $org_wsdl flip to “.*” which LOOKS correct. But the period and the astrisk are escaped and thus invalid as a regex. Here is the query emitted from Grafana:

SELECT sum("count") FROM "my_measurement" WHERE "org_wsdl" =~ /^\.\*$/ AND time >= 1535903435154ms and time ...

Any thoughts?

You may use advanced variable formatting ${variable:raw}, so variable value won’t be escaped.

But I don’t recommend to have “All” option in “xlat_partner_to_prod” measurement. You can configure it in the variable configuration easily - and yes, .* is the correct custom value for All option.

Thanks for the response, @jangaraj. I’ve noticed that the advance variable formatting is available in 5.1, I’m still on 4.6.3. Short of an upgrade, can you think of another other options here?

In any case, I’m not sure how to implement your suggestion:

You can configure it in the variable configuration easily - and yes, .* is the correct custom value for All option.

I know how to do that with a “normal” template variable, but I’m not clear how to do that in this proposed workaround scenario.

You are right. You need it in the measurement. And also you need Grafana upgrade - advanced formatting is the key feature for you.

Thanks again @jangaraj. I have some upgrading to do!

Upgraded to Grafana 5.2.4 and got what I wanted.

Here is the translate table in influxdb

drop measurement xlat_wsdl
insert xlat_wsdl,orig=Partner,xlat=Production count=1 0
insert xlat_wsdl,orig=.*,xlat=All count=1 0
select orig, xlat, count from xlat_wsdl

Here is the template variables

$wsdl_xlat 	show tag values from xlat_wsdl with key="xlat" <<*displayed*
$org_wsdl 	show tag values from xlat_wsdl with key="orig" WHERE "xlat" =~ /$wsdl_xlat/   <<*hidden*

Here is the variable syntax to be inserted into queries

/^${org_wsdl:raw}$/

Thanks again.

P.S. I would love to see a “translate” type of template variable in Grafana. I envision a table interface with two column where you could define the before and after of values. Would also need to handle an “all” condition, too.

Another option is to use MySQL/PostgreSQL datasource with support for text=>value dashboard variables:

SELECT hostname AS __text, id AS __value FROM my_host

Real table is not required for small dataset:

SELECT '.*' AS __value, 'All' AS __text
UNION
SELECT 'Partner' AS __value, 'Production ' AS __text
UNION
...

Unfortunately, as I know InfluxDB is not able to return data in this kind of text=>value format.

Hello @jangaraj @johnh

I have a similar problem where I just want to rename one value and keeping all as it is (none --> others).
To begin with, I created a new measurement
insert rename,orig=none,new=others count=1

when I checked on the dashboard,
SHOW TAG VALUES FROM rename WITH KEY = "new" it’s giving the correct value

So should I need to add all entries in this new measurement, just like I did for “none”? If so, then how will it going to take any new entry coming in my original measurement.