alerting icon indicating copy to clipboard operation
alerting copied to clipboard

feature request: add sprig library or other library for templating support

Open Kamilcuk opened this issue 1 year ago • 2 comments

Why is this needed:

The Grafana templates are low in features, with not fully oddly documented template functions. They are not documented https://grafana.com/docs/grafana/latest/alerting/fundamentals/notifications/templates/ not at https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/reference/ , but at https://grafana.com/docs/grafana/latest/alerting/alerting-rules/templating-labels-annotations/ .

There are not enough functions for good templating.

In particular I am writing grafana alerts where different people have to be tagged on Microsoft Teams depending on if the input label matches a specific regex. It is not possible to if match a label with a regex, it is not possible to split a string.

What would you like to be added:

There is sprig library https://masterminds.github.io/sprig/ . Please add it in all templating contexts to be available.

Who is this feature for?

For any programmers that write grafana alerts.

For example hashicorp/nomad added all sprig library with sprig_ prefix for templating not to break backward compatibility: https://github.com/hashicorp/nomad/blob/0f6561bdfe93d2a5d387c5b0ebbbc946130ed436/command/data_format.go#L108

Thanks!

Kamilcuk avatar Oct 16 '24 10:10 Kamilcuk

Related https://github.com/grafana/alerting/issues/166 would be possible with sprig_fromJson

Kamilcuk avatar Oct 16 '24 10:10 Kamilcuk

Hi @Kamilcuk, we've recently added a couple new functions alongside templateable webhook payloads. This feature is based on templating and required some extra helper functions to make creating valid JSON tolerable.

We considered at length which template library to import, with the default being the oft-used Sprig. However, in the end we decided against importing Sprig wholesale as the library is fairly old and has accumulated a decent amount of hard-to-fix cruft in the name of backwards compatibility (not that backwards compatibility is a bad thing). Given, our use is greenfield and functions are much easier to add than to remove/modify, it felt wrong to jump to Sprig right away.

We looked at a few newer and more actively maintained alternatives such as Sprout and Slim Sprig but they are either a bit too new, make too many sacrifices to be compatible with Sprig (meant less for greenfield and more to migrate off of Sprig), or lack some features we consider necessary (such as namespacing).

In the end, we leaned toward Gomplate but are taking the cautious approach to add functions as-needed.

TLDR: We introduced the following new namespaced functions and will consider adding more functions individually given they are namespaced:

  • coll namespace for collection operations (Dict, Slice, Append)
  • data namespace for JSON operations (JSON, ToJSON, ToJSONPretty)
  • tmpl namespace for template operations compatible with pipelines (Inline, Exec)
  • time namespace for time operations (Now)

JacobsonMT avatar Sep 15 '25 19:09 JacobsonMT