Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom mapping of template variable value -> display text #1032

Open
mbell697 opened this issue Nov 7, 2014 · 115 comments

Comments

Projects
None yet
@mbell697
Copy link
Contributor

commented Nov 7, 2014

Use Case: You may store metrics based on an 'ID' property but wish to have the template variable selection UI use a more human friendly label. e.g. You track metrics by domain with an internal domain ID but wish to use the domain's URL in the template variable selector UI.

@torkelo I can take a cut at implementing this, what are your thoughts on implementation? For my specific use case I would want to be able to provide an arbitrary JS function to perform the value -> text conversion as I need to hit an external service for the lookup. I was thinking an initial implementation could be adding a config value in the dashboard JSON that defines the mapping function. UI support could be added later to handle more trivial mappings with pre built mapping functions (e.g. regex substitutions).

Also connected to this would be the ability to edit the full dashboard JSON via the UI, although export -> edit -> import would function as a workaround if this proves to be difficult.

@torkelo

This comment has been minimized.

Copy link
Member

commented Nov 7, 2014

You could do this with scripted dashboards. But you are welcomed to try to implement it into regular/saved json dashboards.

@dongshengbc

This comment has been minimized.

Copy link

commented Nov 11, 2014

+1

@Iker-Jimenez

This comment has been minimized.

Copy link

commented Apr 7, 2015

I too could use at least a simpler version of this. Something like a configured mapping from A -> B

In my scenario I want to select an entity name in the variable dropdown (CustomerName1, CustomerName2, etc.) but use a numeric id internally when it comes to the metric name.
app.requests.$customer1_ID.count

@timgriffiths

This comment has been minimized.

Copy link

commented Jul 6, 2015

+1

@Krinkle

This comment has been minimized.

Copy link
Contributor

commented Nov 4, 2015

Merging from #3138

E.g. when creating a custom template variable with values fooBar and baz_quuxInternal, to be able to have the UI display the checkboxes as "Foo bar" and "Baz".

This is especially common when using repeated rows and a custom variable to reduce duplication, but the top level metrics may not be user-friendly.

One could potentially support this for queried template values (e.g. graphite properties) as well by using a regex (if the replacement is generic). Regex support already exists, but it applies to both the value used and the label. Having it used for the value only would be valuable.

For example, if a graphite query expands kafka.messagesByTopic.myservice_* for use in templates, then one may want the user interface to strip the prefix. But when used in the actual panels, the prefix should be included. This can be worked around (in Grafana 2.x and later) now that template variables can be embedded in a metric property, so one could hardcode the prefix in all metrics in all panels and rows, but that's better to avoid.

Once these "label" values exist, it would be useful to be able to access them inside the panels as well. Such when embedding a variable in a Row and/or Panel title. Either we can make it use the label by default (if embedded in a title field), or perhaps with some alternate syntax (e.g. $$Variable, or some whatever)

@bluecmd

This comment has been minimized.

Copy link

commented Nov 13, 2015

http://play.grafana.org/dashboard/db/test?editview=templating shows "Variable Label" as an option. Can this be closed?

EDIT: I misunderstood the bug, sorry! :) Carry on.

@torkelo

This comment has been minimized.

Copy link
Member

commented Nov 13, 2015

that is just an option to have a friendly name for the variable, not the variable values

@malnor

This comment has been minimized.

Copy link

commented Nov 27, 2015

+1

1 similar comment
@rjromay

This comment has been minimized.

Copy link

commented Nov 28, 2015

+1

@m4dc4p

This comment has been minimized.

Copy link

commented Dec 3, 2015

+1

3 similar comments
@aalleexxeeii

This comment has been minimized.

Copy link

commented Mar 3, 2016

+1

@tboeghk

This comment has been minimized.

Copy link

commented Mar 7, 2016

+1

@GautamGupta

This comment has been minimized.

Copy link

commented Apr 5, 2016

+1

@sbarale

This comment has been minimized.

Copy link

commented Apr 19, 2016

What's the status of this ?
Is it possible to do so ?

@cosmos78

This comment has been minimized.

Copy link

commented Apr 26, 2016

Is this possible for the 3.0 final ?
It looks like a feature many people are waiting for. Including me :-)

@musskopf

This comment has been minimized.

Copy link

commented Apr 28, 2016

+1
I normally use part of regular expressions which looks awful for users.

@redredgroovy

This comment has been minimized.

Copy link

commented May 9, 2016

+1

1 similar comment
@srpatatas

This comment has been minimized.

Copy link

commented Jun 23, 2016

+1

@inbilla

This comment has been minimized.

Copy link

commented Jul 7, 2016

+1
Without this, regex expressions are unusable in template variables.

@vincenzomos

This comment has been minimized.

Copy link

commented Aug 8, 2016

+1

3 similar comments
@blak3r2

This comment has been minimized.

Copy link

commented Aug 11, 2016

+1

@daaru00

This comment has been minimized.

Copy link

commented Oct 4, 2016

+1

@lucadistefano

This comment has been minimized.

Copy link

commented Oct 17, 2016

+1

@wlay

This comment has been minimized.

Copy link

commented Jul 10, 2018

+1

@wolfhong

This comment has been minimized.

Copy link

commented Jul 10, 2018

+1, Very much in need of this feature.

@serggp

This comment has been minimized.

Copy link

commented Jul 10, 2018

+1

4 similar comments
@davidohana

This comment has been minimized.

Copy link

commented Jul 16, 2018

+1

@progan

This comment has been minimized.

Copy link

commented Jul 19, 2018

+1

@laozhang007

This comment has been minimized.

Copy link

commented Jul 27, 2018

+1

@gabrielayaelrodriguez

This comment has been minimized.

Copy link

commented Jul 27, 2018

+1

@johnymachine

This comment has been minimized.

Copy link

commented Jul 31, 2018

This works for MySql and PostgreSQL:

Another option is a query that can create a key/value variable. The query should return two columns that are named __text and __value. The __text column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with hostname as the text and id as the value:

SELECT hostname AS __text, id AS __value FROM my_host

http://docs.grafana.org/features/datasources/mysql/#query-variable

I would suggest custom variable to be:

[{
		"__text": "Server 1",
		"__value": 1
	},
	{
		"__text": "Server 2",
		"__value": 2
	}
]

Maybe new type called JSON?

@MGinshe

This comment has been minimized.

Copy link

commented Aug 2, 2018

Thanks @johnymachine! That worked beautifully with the PostgreSQL datasource.

As an extension of this, is there any way to retrieve the __text section of the variable? This would be really useful for repeating graphs.

@johnymachine

This comment has been minimized.

Copy link

commented Aug 2, 2018

Hey @MGinshe, this works fine (displays text uses value) for me:

image

@psarossy

This comment has been minimized.

Copy link

commented Aug 20, 2018

EDIT: misunderstood the initial context of this bug, ignore the comment below, it was more meant for #9292

@torkelo @nmaniwa

#12609 seems to implement what most people are asking for here, any reason why that is closed and never got merged?

@torkelo

This comment has been minimized.

Copy link
Member

commented Aug 20, 2018

No, that issue is about something totally different, or did you link to wrong issue?

@dmayan

This comment has been minimized.

Copy link

commented Sep 21, 2018

Still no news about this? Come on, we are in 2018 !! Thanks!

@hiddenrebel

This comment has been minimized.

Copy link

commented Sep 27, 2018

@dmayan it was answered by @johnymachine . You can use this.

This works for MySql and PostgreSQL:

Another option is a query that can create a key/value variable. The query should return two columns that are named __text and __value. The __text column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with hostname as the text and id as the value:

SELECT hostname AS __text, id AS __value FROM my_host

http://docs.grafana.org/features/datasources/mysql/#query-variable

I would suggest custom variable to be:

[{
		"__text": "Server 1",
		"__value": 1
	},
	{
		"__text": "Server 2",
		"__value": 2
	}
]

Maybe new type called JSON?

@dmayan

This comment has been minimized.

Copy link

commented Sep 27, 2018

@wfhu

This comment has been minimized.

Copy link

commented Sep 30, 2018

pretty useful feature, If this feature is out there, It could help us a lot here. Because graphite can not store Chinese character. So we have to use English in Graphite, but we would really like to show the Chinese in the grafana dashboard, so the user experience would be much better.

Hope this feature will come true shortly.

@FdeFabricio

This comment has been minimized.

Copy link

commented Oct 2, 2018

I managed to do the ID->name mapping by using two variables. The first variable lists all possible IDs (value) while the second variable lists the name (display text) that matches the ID. It's not the ideal or pretty, but it does the trick.

I reckon it's called nested variables. And then you can hide one of the variable selectors.

@R-Studio

This comment has been minimized.

Copy link

commented Nov 20, 2018

+1

1 similar comment
@mixvin

This comment has been minimized.

Copy link

commented Dec 5, 2018

+1

@bassie1995

This comment has been minimized.

Copy link

commented Jan 10, 2019

@FdeFabricio You used InfluxDB? Does it automatically update one value when changing the other? If so, how do you do that?

@FdeFabricio

This comment has been minimized.

Copy link

commented Jan 10, 2019

@bassie1995

You used InfluxDB?

Yes

Does it automatically update one value when changing the other?

Yes

If so, how do you do that?

You create a query type variable that selects the item that matches the already selected value (e.g. SELECT "name" FROM playlists WHERE ("id" =~ /^$playlist_id$/). So now you will have two variables: one with the ID and other with the name.

@MGinshe

This comment has been minimized.

Copy link

commented Jan 10, 2019

@bassie1995 @FdeFabricio You could also do this in reverse:

  • A visible variable, which lets you select the playlist name (i.e. Girl Power)
  • A hidden variable, which finds the playlist ID from the name (something like SELECT "id" FROM playlists WHERE ("name" =~ /^$playlist_name$/))

This way, your users see an option to select the playlist by its name, but the playlist ID is hidden. You can still access the playlist ID programatically, in order to search for playlist items, etc.

The __name and __value syntax from the PostgreSQL/MySQL datasource is still ideal though, as it prevents any id->name ambiguity, and reduces the number of database queries required. It should be a base feature IMO.

@MikeKulls2

This comment has been minimized.

Copy link

commented Jan 21, 2019

I found another workaround based off @johnymachine 's comment above. There is support for this in some data sources. If it's not supported in your data source you can create a MySQL database, add the data there and write a query to return __value and __text. This works if the data is static, in my case state (geographic state). For anyone who wants this feature for variables defined as custom then I think this is a good workaround and possibly a better solution anyway. It allows all variable lists to be stored in 1 location and changed more easily. Only downside is it requires installing MySQL.

SELECT SingleChar AS __value, ShortName AS __text FROM TSDB.State

I should add this solution works for repeated graphs, repeated rows and also if the variable has multi-select. The example above using 2 variable to show "Girl Power" doesn't work in these situations.

@thinrope

This comment has been minimized.

Copy link

commented Jan 24, 2019

I was just fiddling with that issue and found a somewhat hackinsh workaround...
I needed a hash, that in Perl notation is written as:

 %units = (
    'μSv/h' => 1.0,
    'mrem/h' => 0.1 );

I created a dashboard variable Type:Custom, Values separated by comma:mrem/h, μSv/h and then edited the JSON Model like that:

      {
        "allValue": null,
        "current": {
          "tags": [],
          "text": "mrem/h",
          "value": "0.1"
        },
        "hide": 0,
        "includeAll": false,
        "label": null,
        "multi": false,
        "name": "units",
        "options": [
          {
            "selected": true,
            "text": "mrem/h",
            "value": "0.1"
          },
          {
            "selected": false,
            "text": "μSv/h",
            "value": "1.0"
          }
        ],
        "query": "mrem/h, μSv/h",
        "skipUrlSync": false,
        "type": "custom"
      }

While this does work (for some time), editing the dashboard via the GUI changes it back to non-working state :-|

@mbell697

This comment has been minimized.

Copy link
Contributor Author

commented Feb 12, 2019

Another option for working around this if you have another system that can provide the data:

Use a JSON datasource plugin, I'm using: https://grafana.com/plugins/simpod-json-datasource

Implement only the / health-check endpoint and the /search endpoint in some system that has the needed data. The /search endpoint should return JSON that looks like: [{ "text": "A Human Name", "value": "123456" }, ...]. The text property will be what appears in the variable select drop down and the value will be what is used in the metrics query.

Then setup your dashboard variable to query against this new datasource, it's kinda hacky but you can use the target field for the datasource to tell the backend what it should return if you have multiple datasets you want to use this with.

@KorkyPlunger

This comment has been minimized.

Copy link

commented Apr 5, 2019

+1 on this. I would really like a way to do this, but one that was not backend-dependent (we use graphite).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.