Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grafana] Improve dashboard variable substitution #3120

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

toanju
Copy link

@toanju toanju commented May 6, 2024

The Grafana dashboard variable syntax allows varnames without curly braces ($datasource and ${datasource}). A substitution can only be applied for the latter. This enables extended regular expressions for sed to allow both substitutions to take place to include dashboards that are written in either form.

@CLAassistant
Copy link

CLAassistant commented May 6, 2024

CLA assistant check
All committers have signed the CLA.

@toanju toanju force-pushed the main branch 2 times, most recently from 433f758 to d7eaea2 Compare May 8, 2024 14:50
@toanju toanju changed the title Improve dashboard variable substitution [grafana] Improve dashboard variable substitution May 8, 2024
@toanju
Copy link
Author

toanju commented Jun 11, 2024

I am happy to update this once more, but I'd really appreciate if this time I could have a 2nd review. Many thanks @zanhsieh for already looking into this.

@zanhsieh
Copy link
Collaborator

@zalegrala Can you review this plz?

@zalegrala
Copy link
Contributor

Have you had a chance to test this? It seems like we should be matching the closing brace also.

@toanju
Copy link
Author

toanju commented Jul 1, 2024

The matches were modified to match braces as optional. Hence, the closing brace will match if it is there, e.g ${DATASOURCE} and $DATASOURCE will match. So far only ${DATASOURCE} was matching.

@zalegrala
Copy link
Contributor

Looking at the old regex, I can test from the CLI with the following.

echo '$variable' | sed '/-- .* --/! s/${{"{"}}{{ .name }}}/{{ .value }}/g'
$variable

With the new regex and the -E flag on sed, I get the following.

echo '$variable' | sed -E '/-- .* --/! s/\$\{{"{"}}?{{ .name }}\}?/{{ .value }}/g'
sed: -e expression #1, char 54: Invalid content of \{\}

How can we validate this before merge?

@toanju
Copy link
Author

toanju commented Jul 9, 2024

Please be aware that this is a helm chart and hence first processed by helm. Thus {{"{"}} will replaced with only { as in the original regex*. In addition {{ .name }} and {{ .value }} will be replaced according to the desired substitution.

*) in the new regex this could be omitted since there are no longer 3 { in a row, but I left it to ease comparison with the original regex

toanju added 2 commits August 5, 2024 12:39
The Grafana dashboard variable syntax allows varnames without curly
braces ($datasource and ${datasource}). A substitution can only be
applied for the latter. This enables extended regular expressions for
sed to allow both substitutions to take place to include dashboards that
are written in either form.

Signed-off-by: Tobias Jungel <[email protected]>
Signed-off-by: Tobias Jungel <[email protected]>
@toanju
Copy link
Author

toanju commented Aug 5, 2024

Hi @zalegrala,
was the above description helpful or do you have any additional questions? In addition, I updated the branch once more.

Copy link
Collaborator

@jkroepke jkroepke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My question is, would that sed also work on an alpine environment? It is not known to me, if sed E is a gnu extension?

If not, 2 simple sed commands are needed.

@toanju
Copy link
Author

toanju commented Aug 6, 2024

Yes, this works on Alpine which eventually uses busybox sed. The latter has extended regex expressions (ERE) implemented. Thus, -E should be treated similar on different sed variants supporting ERE.

Though, thinking about the split of the regex made me think regarding the variable naming. One issue that might be related to this having a datasource variable name that is the prefix of another datasource variable, e.g. ds and ds_something.

This could be mitigated by having to sed commands. The first as it was so far and the second without the curly braces but including the quotes.

What do you think?

@jkroepke
Copy link
Collaborator

jkroepke commented Aug 6, 2024

This could be mitigated by having to sed commands. The first as it was so far and the second without the curly braces but including the quotes.

I'm more a fan of practical solution over cool solutions. If there is already an potential use case, that could break and it can be avoided, then we go of this.

Pretty sure there is a 1 in 100 that a dashboard using ds as prefix for something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants