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
Comments
You could do this with scripted dashboards. But you are welcomed to try to implement it into regular/saved json dashboards. |
+1 |
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. |
+1 |
Merging from #3138
|
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. |
that is just an option to have a friendly name for the variable, not the variable values |
+1 |
1 similar comment
+1 |
+1 |
3 similar comments
+1 |
+1 |
+1 |
What's the status of this ? |
Is this possible for the 3.0 final ? |
+1 |
+1 |
1 similar comment
+1 |
+1 |
+1 |
3 similar comments
+1 |
+1 |
+1 |
I have solved this by creating a MySQL database, I create a table with the items I want in the dropdown, eg Europe, North America etc. In a second field I have a regex that will match the entries I want to match. Then add the MySQL as a datasource and use them to create the variables. It is a hack but actually works quite well. I use it for pretty much the exact thing you are trying to do. |
I appreciate the clever hack, but for us it's not really a solution. Setting up an entirely new database (we don't use MySQL at all, which means operationally this is impossible) to perform a simple key/value substitution that is quite static seems like a lot of hoops to jump through. I was thinking about this a bit more, and there is an even more elegant way of providing this functionality than what I describe above. I'd call it a "variable macro". This again looks like a Custom list, except it allows the administrator to specify that when one (or more) of these macros are selected, then the Variables named will be set, and the Values given will be appended to the existing set of values. This would entirely be a UI-driven model, and would not change the actual variable concept at all - it would just create an auto-completion sugar layer on top of the existing variables. This makes it backwards-compatible with no additional variables needed for creation or integration into queries. A macro that sets the variables would allow the user to see the values as they're being selected, and then would allow the user to open up each variable and see/manipulate the selections or data instead of creating a separate variable as my previous comments imply. This would be much more intuitive. Example: So a variable macro called "North America - Primary Cluster" would set my "Country" variables to "us,ca,mx" and would set my "Cluster:" variable to "primary". Those settings would be visible if I were to pull down each named variable (or not, if they're hidden) so I could add or subtract countries to the Country: list as long as I didn't touch the Macro variable pulldown again. Possibly there is a boolean of "clear named variables before setting" so that if a change is made in the picklist for this Macro, then any other settings of the variables specified would be cleared. This could be useful for lists where it's not obvious that you might include something that was previously set. I suppose that if more than one Variable Macro option were chosen, then the last one in the list to be examined "wins" if there are competing settings of a particular value; no way around that problem. (it's arguable that this clearing action should be specified on a variable-by-variable basis, but that seems to sound a little cluttered... but is it?) Here's my hypothetical example again, where I have pre-existing variables of "Country" and "Cluster". Variable Macro Name: Region Name1: North America - Primary Cluster Name2: Nordics - Secondary Cluster |
I was expecting you would say something like this. The reality is you asked for a hack and this hack does solve the problem, it's not difficult to setup and it's not difficult to reverse in the future. I actually find it quite handy having the MySQL there as we keep adding new data sets to it, it's a convenient place to keep track and update them. If you think about it, if you're going to use these data sets in multiple dashboards and want to have them maintained centrally, then they need to be stored somewhere. If your time series database can't store them then you need something setup to store them. So it actually makes perfect sense to have MySQL. The added bonus is its also very easy to automate the population of MySQL. |
If you have PostgreSQL, you don't need to create an actual table for the mapping, you can do something like:
|
If you want numeric values you need to remove the single quotes, otherwise it is interpreted as text, ie
|
Thanks @GlennMatthys glenn for the answer, but i already found where the problem occurs. SELECT * FROM ( VALUES ( 'OK', 0), ( 'Warning', 1), ('Critical', 2) ) AS t (__text, __value) Configuring Multi-value on: And Multi-value off: |
GlennMatthys - your solution is perfect, many thanks |
For MySQL its: |
What about Mariadb? It is not working for me (mariaDB version: 10.5.5)
Or the one before:
It looks like the Values statement is no longer available... Or is there something else that I do wrong? |
@radoeka For older MariaDB/MySQL
|
@GlennMatthys wow wow wow, what a swift response! And this works. |
This seems to not be supported for Prometheus. |
Reopening this issue as #27829 only solves this for static data. |
Requirements
Heres how I solved this issue. Gonna list the "Journey" as different solutions might be useful to different people (and hopefully someone can improve on them). Attempt #1Problem #1
Solution to #1
Problem #2I want to use a datasource to get my key value pairings. so how will I generate the Solution #2
Problem #3
Solution #3
Problem #4
Solution #4
(I only discovered the text filter while writing this issue, for some reason I kept skipping over it in the docs despite looking for it) Problem #5
Solution #5
Attempt #2
Problem #1
Solution #1
Problem #2
Solution #2
|
It looks like this feature should be addressed by the functionality that was added in 7.4 for regex captures: https://grafana.com/docs/grafana/latest/variables/filter-variables-with-regex/#filter-and-modify-using-named-text-and-value-capture-groups For example, for a database source, you can select a column which is a concatenation of key and value, and then use the regex to split it back apart. Are there any data sources where this doesn't work? |
How would I do this with Prometheus where |
Doesn't this work for you @bitfactory-henno-schooljan https://grafana.com/docs/grafana/latest/variables/filter-variables-with-regex/#filter-and-modify-using-named-text-and-value-capture-groups |
Please reconsider implementing this as it can compensate for Elasticsearch datasource's lack of filters issue: {
"allValue": null,
"current": {
"selected": true,
"text": "No",
"value": "AND NOT _exists_: exception"
},
"description": null,
"error": null,
"hide": 0,
"includeAll": false,
"label": "Is exception?",
"multi": false,
"name": "is_exception",
"options": [
{
"selected": false,
"text": "Doesn't matter",
"value": ""
},
{
"selected": false,
"text": "Yes",
"value": "AND _exists_: exception"
},
{
"selected": true,
"text": "No",
"value": "AND NOT _exists_: exception"
}
],
"query": "Doesn't matter, Yes, No",
"queryValue": "",
"skipUrlSync": false,
"type": "custom"
} |
Thank you very much, I still don't understand why this feature is not built-in |
It is now! (At least for static data), take a look at this PR |
Excellent! |
Please! This is a very needed and common request. Suppose that we have naming mismatch for different services in different data sources (e.g. MSSQL: MyService elasticsearch: my_service). We need to map between these two variables in a dynamic fashion (I'm not talking about value mapping in each dashboard, that's a heck of redundant work) . |
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.
The text was updated successfully, but these errors were encountered: