Template variables on Ad-hoc variable

Hi community,

I was playing with variables and I’ve tried to use a regular template variable on a ad-hoc variable in the following way:

Note: Let’s take for example: https://play.grafana.org/d/000000014/elasticsearch-metrics

I create the following variables:

  • $host: {“find”: “terms”, “field”: “@hostname”}
  • $Filters: (regular Ad-hoc filter, already present in the dashboard)

If then I add a new filter

@hostname = $host

The query doesn’t work, looking at the Query Inspector I can see that the value of $Filters gets replaced but not correctly:

... {"match_phrase":{"@hostname":{"query":"{server 20}"}}} ...

I was going through the code and I noticed that the issue seems to be on this line:

That line replaces variables in a “generic” way and since value is an Array it surrounds it by curly braces and separates by comma here:

this.templateSrv.replace("$hostname", t.scopedVars, "lucene")
> "("cm\-20")"
this.templateSrv.replace("$hostname", t.scopedVars)
> "{cm-20}"

What do you guys think? Is it a bug or I’m just trying too hard to use it in a way that wasn’t meant to be?

Why I’m trying to use template variables on an ad-hoc variable, well, it’s easy, this would allow me to create an ad-hoc with all the regular variables, hide it and automatically have them apply to my elasticsearch queries, without having to include each variable on each graph.

Thanks.

Probably a bug, but why don’t you use the adhoc filter drop down functionality instead of template variable???

Marcus

Hey @mefraimsson, thanks for taking the time

The drop down functionality only shows me the different values of the field, but I can’t (except writting it myself) set “all” or select “many” like we can for regular template variables.

For example, suppose I have a @host field and I want to select host1.com and host2.com, if I use a regular template variable there is no problem, but I have to add that variable to the query of each graph in my dashboard. If I use an ad-hoc variable I don’t have to add it to every graph, but it’s not flexible enough as a regular template variable to choose values.

I’ve found a workaround while I was typing this answer, but only work for Grafana v5.1+.

I can use advance formatting of the $host variable to turn it into a regex and then use the regex operation (=~) in the ad-hoc variable. It works as expected!

Note: Because of the way the query is created using the ad-hoc variables, the lucene formatting of the variable can’t be used in this problem.

Understood. But can’t you add multiple ad hoc filters for hostname then or is it more convenient to use a drop down in template variable?

Marcus

I find it easier (not all of my users are technical) to just tell them to select the values they want from the dropdown.

I don’t follow… wouldn’t multiple ad-hoc filters with different hostnames produce basically empty results? I mean, events are either from hostA or hostB in my case…

Sorry thought that you could select OR instead of ADD in the ad hoc filters.

Marcus

Hi ,

Need help with the following problem:

I have a post request to send data to influxdb as below:

“curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_connect,url={params.TEST_URL},Connect_min={value_min[0]},Connect_max={value_max[0]},Connect_mean={value_mean[0]} {loadtestStartTime}000000’”)
sh(script: “curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_processing,url={params.TEST_URL},metric=processing Processing_min={value_min[1]},Processing_max={value_max[1]},Processing_mean={value_mean[1]} {loadtestStartTime}000000’”)
sh(script: “curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_waiting,url={params.TEST_URL},metric=waiting Waiting_min={value_min[2]},Waiting_max={value_max[2]},Waiting_mean={value_mean[2]} {loadtestStartTime}000000’”)

I have grafana dashboard plotted but need to templatise the metrics of all of them How do I do that? so as the template will show selective metrics for a time range of for example last 7 days

Hi ,

Need help with the following problem:

I have a post request to send data to influxdb as below:

“curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_connect,url={params.TEST_URL},Connect_min={value_min[0]},Connect_max={value_max[0]},Connect_mean={value_mean[0]} {loadtestStartTime}000000’”)
sh(script: “curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_processing,url={params.TEST_URL},metric=processing Processing_min={value_min[1]},Processing_max={value_max[1]},Processing_mean={value_mean[1]} {loadtestStartTime}000000’”)
sh(script: “curl -s -X POST {TELEGRAF_URL} --data-binary 'ab_loadtest_waiting,url={params.TEST_URL},metric=waiting Waiting_min={value_min[2]},Waiting_max={value_max[2]},Waiting_mean={value_mean[2]} {loadtestStartTime}000000’”)

I have grafana dashboard plotted but need to templatise the metrics of all of them How do I do that? so as the template will show selective metrics for a time range of for example last 7 days