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

Add Sprig template functions #108

Closed
wants to merge 1 commit into from

Conversation

jquick
Copy link
Contributor

@jquick jquick commented Jul 3, 2023

The current Alerting template functions are very few and make it hard to do any kind of re-mapping/arithmetic for your templates. I found myself unable to do basic counters or uniques on lists for my alert groups.

This PR adds in the Sprig template pack which is used by many projects to add some basic useful arithmetic, dictionary, list, etc functions. You can find more info here:
http://masterminds.github.io/sprig/

This implementation adds the alertmanager functions after to make sure to overwrite any conflicts and keep all current templates stable.

Here is also an example of where Helm is also importing these functions:
https://github.com/helm/helm/blob/main/pkg/engine/funcs.go#L44
I have also disable the env calls due to potential security risks (same as Helm).

With this addition I was able to spin up local Grafana and create a workable template like:

  {{- /* look for environments */ -}}
  {{- $envs := dict -}}
  {{- range $alerts -}}
    {{- if gt (len .Labels.environment) 0 -}}
      {{- $_ := set $envs .Labels.environment 1 -}}
    {{- end -}}
  {{- end -}}
  {{- if gt (len ($envs | keys)) 0 -}}
  Multiple Environments Firing:
    {{ range ($envs | keys) }}
      {{ . }}
    {{ end }}
  {{- end -}}

This is really nice for our usage and would be a great addition to our team.

Signed-off-by: Jared Quick <jared.quick@salesforce.com>
@CLAassistant
Copy link

CLAassistant commented Jul 3, 2023

CLA assistant check
All committers have signed the CLA.

@jquick
Copy link
Contributor Author

jquick commented Jul 25, 2023

@armandgrillet - Thanks for your requests on this. Is there anything I can do to help push these PRs forward?

Copy link
Contributor

@grobinson-grafana grobinson-grafana left a comment

Choose a reason for hiding this comment

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

I think having some Sprig functions are useful, but I'm concerned about using denylists for functions such as env. Are there some functions in particular that you are interested in, as perhaps we could add them ourselves?

@jquick
Copy link
Contributor Author

jquick commented Aug 16, 2023

The main ones I think would be helpful are:
Integer Math Functions: add, max, mul, etc.
Lists and List Functions: list, first, uniq
String Functions: trim, wrap, randAlpha, plural
Date Functions: now, date
Dictionaries and Dict Functions: get, set, dict, hasKey, pluck, dig

We could do a inclusion list from sprig instead if thats easier/safer.

@grobinson-grafana
Copy link
Contributor

Thanks! Would you be able to first request this feature to the Alertmanager maintainers?

The reasoning for this is that Grafana uses Alertmanager, but over time Grafana has made some changes to it's version of Alertmanager that means it's not longer 100% compatible. Instead, we are working to consolidate Grafana with the upstream Alertmanager code, but that means we cannot accept changes in Grafana that are not also accepted in Alertmanager.

@gotjosh
Copy link
Collaborator

gotjosh commented Aug 21, 2023

@jquick thank you very much for your contribution - as my colleague @grobinson-grafana just said I'd love to get a sense of which of these functions you consider useful and perhaps we can consider proposing them upstream (in https://github.com/prometheus/alertmanager).

For us, it's hard to maintain the blind inclusion of these functions without a proper analysis of how common is their usage. I can appreciate the helm reference, but as you might understand these projects (helm vs alerting) have completely different purposes.

I'm closing this for now, but feel free to let us know if we're perhaps missing something.

@gotjosh gotjosh closed this Aug 21, 2023
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.

None yet

4 participants