Variable format with custom delimeter

  • Grafana v10.4.3 (0bfd547800) from DockerHub

  • What are you trying to achieve?

I want to query my HTTP-backend with request parameter like Name=foo+bar+baz with values (foo, bar, and baz) taken from the variable with Multi-value selection

So I want to be able to concatenate variable values with “+” sign

Or, extrapolating, join an array of values with any sign, as any programming language does, e.g:

name = ['foo', 'bar', 'baz']
String to interpolate: '${name:join(+)}'
Interpolation result: "foo+bar+baz"

or

name = ['foo', 'bar', 'baz']
String to interpolate: '${name:join(, )}'
Interpolation result: "foo, bar, baz"

or

name = ['foo', 'bar', 'baz']
String to interpolate: '${name:join( AND )}'
Interpolation result: "foo AND bar AND baz"

Seems that this feature can be more powerful than all of CSV, Pipe, and Text Formats combined, but I can’t find the solution (and the existence of CSV, Pipe, and Text Formats makes me guess, that id doesn’t exists).

(Ref: Dashboards > Variables > Variable syntax)

Should I open a feature request for this?

1 Like

what visualization will you be using for this?

 ${var_name:text}

will give you

foo + bar + baz

Is custom delimiter really needed?

I hope this will allow me to show the Table panel with dynamic set of rows, e.g

  • show the last values for sites Foo, Bar, and Baz” (Name=foo+bar+baz → 3 rows) or
  • show the last value just for Foo and Baz” (Name=foo+baz → 2 rows)

My JSON-over-HTTP backend will return a list with required number of objects (3 or 2 in this example)

Also I hope this will allow me to make repetitions for Stat panel according to the list of values, too.

Yes, it will. But spaces are will be urlcoded too (foo%20%2B%20bar%20%2B%20baz), so backend will reject this like bad request

I think this is a great feature but until that gets implemented you need a resolution right?

As a workaround you can do that by adding extra variable and using datasource capabilities. Example for InfluxDB and Flux:

import "strings"
import "array"

array.from(rows: [{result : strings.replaceAll(v: "${var_name:text}", t: " ", u: "")}])

var_name_2 doesn’t have spaces:
var_name_2

1 Like

Definitely! This small feature would allow me to create a useful dashboard for users who are far from IT - for poultry farmers =)

Looks good, like exactly the workaround I need! But, where do I have to place a piece of code like yours? (My datasource plugin is Infinity)

In Query field of a variable:

1 Like

Looks like I misunderstood something important

(I have no real InfluxDB installed, so I receive “missing organization in datasource configuration error performing flux query” error when save it with fake http://localhost:8086 URL)

изображение

изображение

изображение

(Grafana v10.4.3, InfluxDB plugin version 5.0.0)

Can you please export your dashboard, if it doesn’t contain sensitive data?

May be it doesn’t work because you don’t have InfluxDB installed.

Because it should look like this:

My main message was to try to use capabilities of string processing that your data source provides

Yep, I’ve connected it to https://cloud2.influxdata.com and it works now.

Yes, that’s clear! But Flux became the first datasource with string processing for me, I will search for another one now

Thank you for the idea!!

I was also going to state the obvious that one must choose between Infinity or InfluxDB (with Flux). With Flux, you can parse JSON into Flux types. This is part of an incredibly powerful feature of Flux called dynamic types that was built specifically for working with JSON data. The issue with JSON data in Flux is that Flux doesn’t know exactly how JSON data should be typed. Dynamic types act as a temporary type that you can then use to explicitly cast JSON types to Flux types.

1 Like

op needs to make an http call not sure how bringing in influxdb in the mix helps him make an http call?

InfluxDB was used just as an example of variable processing by datasource in case if it can’t be implemented by Grafana means

For example, to remove spaces from a variable with several values selected, and use this new variable in the http call…

1 Like

this way:

http://my-backend?Name=${var_name_2}

will produce:

http://my-backend?Name=foo+bar+baz

(using naming from previous examples)

Now I’m looking for suitable string processing functions in JSONata, etc…

might want look into the volkov plugins to use javascript to get what you want. This below is using Dynamic text plugin

yes that is to get the url string you want but then how do you make the http call from the url you get from influxdb?

IRL example with the Dashboard variable (set with Influx Query) and Infinity datasource:

Table panel updates according to selected parameters

1 Like