Referencing Key/Value variable with another variable as the key

Hi Folks,

I thought that what I was trying to do was going to be quite straightforward, but I cannot figure it out to save my life.

I have a variable, “environment” which can have one of three values: test, staging, or production. The user selects the proper value for the dashboard.

I have another variable, “destURL” which is hidden, and used to create links on the dashboard and is dependant on the “environment” variable.

So, I have:

image

and

image

But I cannot figure out how to reference the value (url3.company.com) to create datalinks in a table when someone chooses the “prod” environment. I have searched and searched, and tried every combination of variables and formats that I can come up with, but have yet to come up with the magic sauce.

Is this possible? If not, then the whole benefit of the ability to put a key/value pair in a variable is lost on me.

I am running a little bit older version of Grafana: 8.1.3, so hopefully it won’t require an upgrade since that requires moving heaven and earth these days.

Thanks in advance!

1 Like

You need only one variable. Use key/value pairs (follow syntax from the doc exactly e.g. key1 : value1,key2 : value2 - don’t add additional spaces) and then you can use ${var1:text} to access a key and ${var1:value} to access value.

3 Likes

Thanks, but I saw a similar answer online, and I just do not understand it. (And I read the docs so many times my head hurt.)

If the destURL is dependent on the environment… and the environment is selected by the user of the dashboard. How can I only have a single variable? The ${environment} variable will contain the key that dictates which of the destURL Values to use.

I am really sorry for being so dense about this.

Thanks for your help… and patience!

Wow… now that makes total sense! Thank You!!!

My only problem is that the “Environment” variable that I was going to use for the key comes from a MS SQL lookup which I use to determine the datasource name.

image

I think I am going to have to go with separate dashboards for each environment as I don’t see how it’d be possible to populate both the Datasource and that key/value variable from a single dropdown.

By asking if that is possible to do. And yes you can do that but you will run into some issues.

Which user will you be using to query data? The same user for both prod and pre-prod? That will result in you to start looking for a new job :wink:

That wont be recommended. So you will need to sort that out first

1 Like

That’s even better. You can use SQL to create key/value pairs. See doc: Microsoft SQL Server template variables | Grafana documentation

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 allow you to have a friendly name as text and an id as the value.

Example (sorry, no MSSQL syntax, standard PostgreSQL):

Use your MSSQL knowledge and code required business logic in the SQL (e.g. CASE, comparison, subqueries, …) based on input datasource variable.

1 Like

Yosiasz… thanks for looking out for my livelihood… honestly. :slight_smile:

I actually misspoke when I said that it was a MS SQL database query that determined the datasource… it was actually just a pattern match against MS SQL TYPE datasources. (So, there is no SQL query involved… and the credentials used in each datasource are different.)

I actually got to a working solution through a bit more research, a lot of help from jangaraj, and considerable sleep. :smiley:

Basically, I have a variable, “env” defined as such:

Type: Custom
Label: Environment
Values: Test : urlTest, Staging : urlStaging, Production : urlProd

And a “datasource” variable defined as:

Type: Data source
Hide: Variable
Datasource Type: Microsoft SQL Server
Instance name filter: /MOS - ${env:text}/

This properly populates the ${datasource} variable and allows me to access the environment and url stuff via ${env:text} and ${env:value}

So far it seems to be working!

Thanks so much for your help!

1 Like

how do you differentiate/sort out credentials for each using what you got? Slowly creeping towards the exit door @gehogan3 :slight_smile:

The credentials for each datasource is in the datasource description. The choice of which datasource to use is a simple “Instance name filter.” Everyone who has permission to view the dashboard for one environment has permission to view them all.

But, the SQL login credentials for each environment (the datasources) are different.

I am not exactly sure what the issue would be other than folks who can see the TEST data dashboard will also be able to see the STAGING and PRODUCTION dashboards, but in our environment that is acceptable.

1 Like

this does not work

a: 1, b: 2

this works

a : 1, b : 2

very finicky about spacing. This is what you should see

According to image if you set it up correctly you should not see thr values on preview so it’s not set up properly