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

Alerting: Jinja template evalutation of variables is broken on loops #4374

Closed
baldm opened this issue Feb 28, 2024 · 2 comments
Closed

Alerting: Jinja template evalutation of variables is broken on loops #4374

baldm opened this issue Feb 28, 2024 · 2 comments

Comments

@baldm
Copy link

baldm commented Feb 28, 2024

What happened?

When setting a variable in a loop in the "Edit Routing Template" window, the variable in the loop is forgotten once the loop is exited.
An example of this can be seen with the code below

{% set flag = true %}
{% for alert in payload.alerts %}
    {# We set the flag to be false, to demonstrate it doesnt change outside the loop #}
    {% set flag = false %}
    {{ "Confirming we are in a loop." }}
{% endfor %}

{% if flag %}
    The flag is still true.
{% else %}
    The flag is false now.
{% endif %}

Gives the result:

X Confirming we are in a loop. The flag is still true.
Selected alert will not be matched with this route

What did you expect to happen?

The result should be:

X Confirming we are in a loop. The flag is false now.
Selected alert will not be matched with this route

As we set the variable to be false in the loop.

Did this work before?

I dont know

How do we reproduce it?

  1. Copy code from example into the "Edit Routing Template" window.
  2. Select a demo rule with 1 or more alerts in the alerts array. Any array to loop over instead of payload.alerts can be used.
  3. Result is not the expected result

Is the bug inside a dashboard panel?

no

Environment (with versions)?

Grafana: Cloud
OS: MacOS
Browser: Firefox and Safari

Grafana platform?

I use Grafana Cloud

Datasource(s)?

No response

@JohnnyQQQQ JohnnyQQQQ transferred this issue from grafana/grafana May 22, 2024
@JohnnyQQQQ JohnnyQQQQ removed this from Alerting May 22, 2024
@baldm
Copy link
Author

baldm commented Jun 27, 2024

i guess the error is with how Jinja evaluates scopes, and not with the Grafana implementation. I will close the issue.

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

No branches or pull requests

2 participants