How Do I Create A Variable That References Another Variable?

I am am trying to figure out to create a variable that contains the value of another variable. One variable will contain the “root” name of an api and the actual api application name. Here’s what I mean.

Fully Qualified API Name: MyAPICollection.MyApi

The Problem:
There are cases where I need to use only the application name (“MyApi”) and other cases where I need to use the fully qualified name(“MyApiCollection”).

What I’ve tried:

  1. I created on constant variable called: AppName with all the possible apps I’d like the user to select (e.g. “MyApp1”, MyApp2", “MyApp3”).
  2. I created another constant variable called “CollectionName” with a value of “MyApiCollection”. This variable won’t be displayed.
  3. I created another constant variable called FullyQualifiedName. This will not be displayed but will be used to pass on to my Lucene query. So, I used the value of: $CollectionName.$AppName

Result: Am i simply getting a literal value of: $CollectionName.$AppName

What I’ve tried to make it work:
First off, is this even possible to do in Grafana (please say yes!)?

  1. I’ve tried: $CollectionName.$AppName
  2. I’ve tried: {$CollectionName.$AppName}
  3. I’ve tried: {{$CollectionName.$AppName}}
  4. I’ve tried: {$CollectionName}.{$AppName}
  5. and finally: {{$CollectionName}.{$AppName}}

Why am I doing this?
I have one dashboard that is presently supporting 4 api and are part of a collection. However there is some cases when I only need a subset of the fully qualified name.

Example: MyCollection.MyApi1 will be passed to ElasticSearch because the fields.Application field is tagged with “MyCollection.MyApi1”.

In other cases, I need only the the application part of the fully qualified name with different casing requirements (e.g. to references cloudwatch logs).

Example: To specified the CloudWatch query in need “myapi1” so it’s the lower-cases version of MyApi.

I can’t think that what I’m doing is all that unusual. I want to be able to have one dashboard that supports all the APIs that are in the collection and variable will hopefully get me to that goal.

Any ideas?

hello can i ask you to support me in first installation? i have understanding you have similar beginner questinons already overcome. thank you

Sorry Matthias, I’m not sure if I understand your question. Are you asking me for additional information about my installation?

@kshull I’m not sure I totally follow your setup, but it sounds like the first issue you’re facing is simply getting your variable values to be correctly interpolated in your queries. I don’t think any of the 5 syntaxes you attempted are valid unfortunately - I think what you want is

${CollectionName}.${AppName}

(see Variable syntax | Grafana Labs)

Hopefully that helps you get started. I do think the overall approach you describe should be doable.

Thanks for your reply. The info on the variable syntax was useful since I’m just learning Grafana presently. The tips you mention do indeex work for a query, instead of instrumenting my queries with {CollectionName}.{AppName} I wanted to be able to use another variable that would container these expressions (e.g. concatenating the CollectionName with the AppName).

So, to get back to the fully-qualified API name variable, I want to create a precalculated variable which references the $CollectionName and $AppName.

  1. I create a new variable called: QualifiedApiName, I assume the variable type would be “constant”.
  2. The value of this new variable would be a formula: ${CollectionName).{AppName).

This is a little off-topic, but it’s related to what I’m trying to do. How can I convert the case of a variable value on-the fly? As I mentioned in the first post, The use-case is I first have a variable defined that the user is able to select the AppName. The value of this variable will be a list of AppNames, but the user can only select one of them. The AppName they select will be in upper case. However, there are cases where I want to use that value as lower case. Is there a function to change the case of the selected value? (e.g. Regex maybe?).

Thanks for your help!

This sort of “formula-generated variable” isn’t (to my knowledge) something that Grafana supports. Though based on what you’ve said, I wonder if you actually need it. Is there a big drawback to simply using ${CollectionName}.${AppName} in your queries?

Grafana does support chained variables - where you use the values of one or more existing variables to generate the list of options for a new variable - but that’s done through a data source query, and I don’t think that this is what you want here. The examples in the documentation are hopefully clear.

Finally, having a mapping between what’s displayed in the drop-down and the actual variable value should be possible since recently - check Variables: Adds support for key/value mapping in Custom variable by sartaj10 · Pull Request #27829 · grafana/grafana · GitHub for a description. I haven’t used it myself but it sounds like it’s what you need.

Good luck!

Happy Monday.
Unknowingly, I’m actually using chained variables now. One thing I’d like to do is change the case of the referenced variable’s value. Is that possible? So, if the user selects from the “parent” variable and it is displayed as upper case (to the user). I’d like to new chained variable to use the value but I wan’t to convert it to lower case.

Parent Variable: Environment (user selects “DEV”)
Chained Variable: references Environment. I wan’t to change the case from “DEV” to “dev”.

So, the purpose of this chained variable would essentially be to change it case.

Possible?

I don’t think Grafana provides value editing via regex or something like that. The only place where regex plays a role is for filtering (out) certain value options.

Maybe check out the key/value functionality I linked to in my last answer? Or check out https://github.com/grafana/grafana/issues/1032 for a more comprehensive discussion of various options - including reading the mapping from a datasource.

Hi
What about defining a variable root with two possible values, one is an empty string, the other is ‘your_collection_name.’ (including the point).
Then you can try to use $root$AppName for the application field with $root set as you need it.

May be something like this will work for you?

That works thank you!