azure-monitor-datasource icon indicating copy to clipboard operation
azure-monitor-datasource copied to clipboard

$__contains macro doesn't expand correctly with variable of type custom

Open tjmoore opened this issue 6 years ago • 3 comments

I have an application logging custom events with Application Insights and have Grafana (self hosted, v5.3.4) set up with Application Insights data source. I have a multi-value variable defined as $room on the dashboard as a custom type with the following value:

Room 1, Room 2, Room 3, Room 4

I then have a query such as (simplified for example):

customEvents
| where $__contains(customDimensions.roomName, $room)
| project customDimensions.start, customDimensions.roomName

Query inspector shows the query parameter for the API call expands out to:

customEvents
| where customDimensions.roomName in ({Room 1,Room 2,Room 3,Room 4})
| project customDimensions.start, customDimensions.roomName

This is invalid according to Log Analytics query in Azure Portal. One reason I can see is the values are not quoted.

So if I change the variable to use quotes: 'Room 1', 'Room 2', 'Room 3', 'Room 4' (not ideal as it shows the quotes in the drop down in dashboard).

This expands to:

customEvents
| where customDimensions.roomName in ({'Room 1','Room 2','Room 3','Room 4'})
| project customDimensions.start, customDimensions.roomName

This is also invalid: Query could not be parsed at '{' on line [2,38]

If I remove the { and } in Azure Portal query, then it runs fine.

It appears to be broken and doesn't correspond to the documentation:

$__contains(colName, $myVar) - is to be used with multi-value template variables. If $myVar has the value 'value1','value2', it expands to: colName in ('value1','value2').

p.s. The custom variable I'm just using at the moment to hard code a set of values. I'm having a hard time trying to get a variable to pick values from a query. I just get an error "Template variables could not be initialized: undefined" whatever query I use.

tjmoore avatar Dec 20 '18 17:12 tjmoore

I think the problem here is with the variable definition in Grafana rather than the macro. Using the custom variable type specifying a series of comma separated values, the variable has { and } surrounding it. Also it doesn't put quotes around the values, but manually adding quotes makes the quotes appear in the selection drop down in the UI also.

tjmoore avatar Jan 02 '19 14:01 tjmoore

I've found a workaround using trims and a split. The trims to remove { and }, and the split to convert the comma separated string into an array.

let rooms = split(trim("}", trim("{", "$room")), ",");
customEvents
| where $__contains(customDimensions.roomName, rooms)
| project customDimensions.start, customDimensions.roomName

tjmoore avatar Jan 02 '19 15:01 tjmoore

Okay, I realise now why the brackets pass through. Having read the right bit of the documentation, I need to use "${room:csv}" to produce a comma separated list without the brackets. Still, $__contains macro needs the list as an array object it seems so have to do:

let rooms = split("${room:csv}", ",")

At the least, this is a documentation issue, as currently the Azure Monitor plugin page says:

$__contains(colName, $myVar) - is to be used with multi-value template variables. If $myVar has the value 'value1','value2', it expands to: colName in ('value1','value2').

which doesn't work.

tjmoore avatar Jan 15 '19 13:01 tjmoore