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

Variables: Adds support for key/value mapping in Custom variable #27829

Merged
merged 9 commits into from Oct 1, 2020

Conversation

sartaj10
Copy link
Contributor

@sartaj10 sartaj10 commented Sep 25, 2020

What this PR does / why we need it:

This PR adds a new variable type where the user can enter key-value pairs as comma separated values where the key-value pairs are separated by colon (:). This would be useful where the user wants the dropdown values to be user-friendly while the query expects them in a different format. Eg : The user-friendly dropdown value could be 100 MB while the query expects it to be in bits (800000000)

Which issue(s) this PR fixes:

Fixes #1988
Fixes #1032

Special notes for your reviewer:

@hugohaggmark

Some screenshots are attached below

Screen Shot 2020-09-28 at 3 09 46 PM

Screen Shot 2020-09-28 at 3 09 39 PM

@CLAassistant
Copy link

CLAassistant commented Sep 25, 2020

CLA assistant check
All committers have signed the CLA.

@sartaj10 sartaj10 changed the title Variable Type: Add a new variable that allows custom key/value mapping WIP / Variable Type: Add a new variable that allows custom key/value mapping Sep 25, 2020
@sartaj10
Copy link
Contributor Author

@hugohaggmark

@hugohaggmark
Copy link
Contributor

@sartaj10 thanks for making this contribution, I don't think we need a new Variable Type for this. This behavior belongs to the custom variable type. WDTH?

@hugohaggmark hugohaggmark added this to In Review in Frontend Platform Backlog via automation Sep 28, 2020
@hugohaggmark hugohaggmark added this to the 7.3 milestone Sep 28, 2020
@hugohaggmark
Copy link
Contributor

@sartaj10 not sure what the best separator char would be for this, maybe | is also a candidate as it might be more common to use : in the label?

@torkelo
Copy link
Member

torkelo commented Sep 28, 2020

yea, this feels like an extension of the custom variable

@sartaj10
Copy link
Contributor Author

sartaj10 commented Sep 28, 2020

@hugohaggmark @torkelo
Yeah, it does make sense to include this as an extension to the custom variable. I'll work on this and update the PR.

Regarding the separator, the semicolon seems more intuitive (similar to a hashmap). In the code, the user can escape the semicolon if it's being used as either a key or value using \

@sartaj10
Copy link
Contributor Author

@hugohaggmark

Updated the PR. It is now possible to include key/value pairs within the custom variable type and it takes care of the splitting whenever that is present.

@sartaj10 sartaj10 changed the title WIP / Variable Type: Add a new variable that allows custom key/value mapping WIP / Variable: Update custom variable to allow key/value mapping Sep 28, 2020
Copy link
Contributor

@hugohaggmark hugohaggmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic work on this @sartaj10 and thank you for contributing ⭐ I left a small comment and you also need to update the docs here:
https://github.com/grafana/grafana/blob/master/docs/sources/variables/variable-types/add-custom-variable.md

@hugohaggmark hugohaggmark changed the title WIP / Variable: Update custom variable to allow key/value mapping Variables: Adds support for key/value mapping in Custom variable Sep 29, 2020
@hugohaggmark hugohaggmark added add to changelog pr/external This PR is from external contributor labels Sep 29, 2020
@hugohaggmark
Copy link
Contributor

@sartaj10 I'm going to change this Draft PR into Ready for Review, ok?

@sartaj10
Copy link
Contributor Author

@hugohaggmark
Sounds good

@hugohaggmark hugohaggmark marked this pull request as ready for review September 30, 2020 04:42
@hugohaggmark hugohaggmark requested a review from a team September 30, 2020 04:42
Copy link
Contributor

@hugohaggmark hugohaggmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Before we merge this I'd like for the technical writers to approve as well.

@sartaj10
Copy link
Contributor Author

Perfect, thank you.

Copy link
Contributor

@oddlittlebird oddlittlebird left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor edit, then good to go!

Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
@sartaj10
Copy link
Contributor Author

Thanks, Done 👍

@hugohaggmark hugohaggmark merged commit 4e4242d into grafana:master Oct 1, 2020
Frontend Platform Backlog automation moved this from In Review to Done Oct 1, 2020
@wbrowne wbrowne mentioned this pull request Oct 15, 2020
@mareksimunek
Copy link

would be possible to access also a key as value?
E.g I have
namespaceName : dev

and sometimes I want in query namespaceName value and sometimes I want dev value, but still with 1 custom variable

@HigH-HawK
Copy link

Hi,

I have updated my Grafana to the latest version and tried the key/value mapping, which is working fine. I also tried working with the variable data in my panels but seem to do something wrong, as it doesn't do what i want it to do.

Example:

Constellation: srv-a is the grafana machine with Prometheus installed - srv-b and srv-c are the node_exporters, sending the data to srv-a

Prometheus has the IP addresses of srv-b and srv-c setup as targets

In my Dashboard on Grafana I setup a variable with the new mapping feature such as:

Variable name: host
Variable Label: Hosts
Variable Type: Custom
Variable Values: srv-b : IP, srv-c : IP

How can I query the IP (value) of the variable host in my panels? If I just put $host, it just returns the name (key)....I'm pretty sure I need to add something to $host but am just not sure what :(

@torkelo
Copy link
Member

torkelo commented Nov 6, 2020

This breaks all existing dashboards using custom variabes with values that include a colon : , think we have to revert or figure out another way to make this and not break existing dashboards

ryantxu pushed a commit that referenced this pull request Nov 18, 2020
)

* Initial work on adding new mapping variable type

* Remove add button

* Update custom variable type to include key/value mapping

* Update custom variable docs

* Try to fix failing build

* Update reducer.ts

* Fix failing build

* Remove expansion of the colon

* Update docs

Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>

Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
@argais
Copy link

argais commented Nov 20, 2020

would be possible to access also a key as value?
E.g I have
namespaceName : dev

and sometimes I want in query namespaceName value and sometimes I want dev value, but still with 1 custom variable

@mareksimunek I came here looking for this exact same behaviour, did you manage to get this?

@argais
Copy link

argais commented Nov 21, 2020

Well, answering myself and @mareksimunek , if you do ${namespace:text} it'll return the key.

@HigH-HawK
Copy link

How can I query the value of the variable in my panels?

If I put $variable_name, it just returns the name (key) but I can't seem to find a way, to get the value to use it in queries. Any help would be appreciated.

@maard
Copy link

maard commented Nov 25, 2020

How about do not introduce a separator at all, and still use commas: '100MB, 800000000',
and include a new boolean flag: 'Dictionary' (perhaps, under 'Selection Ooptions')?

@mingmamma
Copy link

How can I query the value of the variable in my panels?

If I put $variable_name, it just returns the name (key) but I can't seem to find a way, to get the value to use it in queries. Any help would be appreciated.

I am facing the same problem and let me give a more practical example. My custom variables set up is as the following:
Region={LAX:us-region, SYD:australia-southeast1}
Capture2
I can use !{Region:text} to reference the key and get the returned value as either LAX or SYD with no issue.
Capture

Looking at the code change in the PR, I would assume that I can use !{Region:value} to get either us-region or australia-southeast1. However, that would throw an error.
Capture1

@offbyone
Copy link

@mingmamma did you find a solution for this? I just bumped into the same issue.

@hugohaggmark
Copy link
Contributor

@mingmamma & @offbyone we use GitHub for issues and feature requests. For questions we kindly request that you ask your question at the community site or in our external Slack here

Regarding your question the ${Region:text} doesn't format the "text" value but the "value" value so there is no ${Region:value}. All the formatting options are described in https://grafana.com/docs/grafana/latest/variables/advanced-variable-format-options/

All the best

@offbyone
Copy link

@hugohaggmark I understand not wanting to use github issues for support, that's fine. Given that, would someone from the Grafana dev crew mind having a look at the support question for this topic?

@majorvin
Copy link

majorvin commented Feb 17, 2021

I just installed the latest version 7.4.2 and not seeing this feature.

EDIT: Ok, it needs to have a space after the key and after colon. e.g. A : value

@hugohaggmark
Copy link
Contributor

I just installed the latest version 7.4.2 and not seeing this feature.

EDIT: Ok, it needs to have a space after the key and after colon. e.g. A : value

Thank you for noticing this @majorvin, I've opened a PR that tries to clarify this #31303

@hilmialf
Copy link

hilmialf commented Dec 14, 2022

Is it possible to make the selected one based on key rather than value?
I recently use this kv pairs:
key1 : value 1, key2 : value2, key3 : value2
*notice I use value2 in both key2 and key3.
When I select key2 in my dashboard, key3 also got highlighted. Then I can no longer select key3 from there.
I can only select key3 if my previous selected key was key1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow custom mapping of template variable value -> display text Allow key/value in template variables