Skip to content

Comments

Webhook Improvements - Templateable Payloads#309

Merged
JacobsonMT merged 9 commits intomainfrom
jacobsonmt/templated-webhooks-squashed
Apr 11, 2025
Merged

Webhook Improvements - Templateable Payloads#309
JacobsonMT merged 9 commits intomainfrom
jacobsonmt/templated-webhooks-squashed

Conversation

@JacobsonMT
Copy link
Contributor

Overview

This PR introduces enhancements to the webhook notifier, focusing on three main areas:

  1. Customizable webhook payloads using templates
  2. Support for custom HTTP headers
  3. New template functions to provide support for creating well-formatted, valid JSON in payloads.

Key Changes

1. Custom Payload Templates

  • Added support for custom JSON payload templates in webhook notifications
  • Users can now define their own payload structure using templates
  • Includes a new CustomPayload field with:
    • Template field for defining the payload structure
    • Vars field for providing additional template variables accessible in context via .Vars.<variable_name>
  • Provides an example payload template which is equivalent to the default payload format for demonstration and regression testing

2. Custom HTTP Headers

  • Added support for custom HTTP headers in webhook requests, example usage would be to set the Content-Type header for your custom payload.
  • Implemented security measures to prevent misuse:
    • Restricted headers list to prevent security issues
    • Headers like Authorization, Cookie, Host are protected

3. New Template Functions

  • Introduced new template functions inspired by gomplate:
    • 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)
  • These functions enable more complex and flexible payload formatting, especially when creating JSON.
  • Currently gomplate does not allow modularized use of select functions or namespaces, so importing it directly would require a significant increase in dependencies. In addition, certain necessary functions have extended functionality in gomplate that makes them risky to use as-is such as the eJSON decryption capability of data.JSON which reads from environment variables. That being said, none of this is insurmountable, and the plan should be to eventually incorporate the gomplate library directly and more broadly.

4. Related Improvements

  • Added new fields to ExtendedData and ExtendedAlert to make sure custom payloads have access to all of the information the default webhook payload has:
    • ExtendedAlert.OrgId
    • ExtendedData.GroupKey
    • ExtendedData.TruncatedAlerts

Related Requests

Issues for supporting new notifiers in Grafana are fairly common and are generally reasonable requests. However, they can come with significant burden of maintenance. Some examples:

Requests for related features are some of the most popular in Alerting:

Upstream Prometheus Alertmanager also has significant community requests, although consensus so far has been to keep the webhook notifier as-is with recommendations to set up a small web server to act as a webhook bridge:

Future Work

  • Import gomplate and expand template support to more namespaces.

Custom Payload Examples

Could be as simple as writing JSON directly:

{
  "alert_name": "{{ .CommonLabels.alertname }}",
  "status": "{{ .Status }}",
  "environment": "{{ .Vars.environment }}",
  "custom_field": "{{ .Vars.custom_field }}"
}

Or using new functions to help craft valid JSON:

  {{ coll.Dict 
  "receiver" .Receiver
  "status" .Status
  "alerts" .Alerts
  "groupLabels" .GroupLabels
  "commonLabels" .CommonLabels
  "commonAnnotations" .CommonAnnotations
  "externalURL" .ExternalURL
  "version" "1"
  "orgId"  (index .Alerts 0).OrgId
  "truncatedAlerts"  .TruncatedAlerts
  "groupKey" .GroupKey
  "state"  (tmpl.Inline "{{ if eq .Status \"resolved\" }}ok{{ else }}alerting{{ end }}" . )
  "title" (tmpl.Exec "default.title" . )
  "message" (tmpl.Exec "default.message" . )
  | data.ToJSONPretty " "}}

Review Tips

  • New gomplate-based functions are nearly 1:1 with the library, so testing was skipped given we're going to eventually remove these in favour of and import. Any differences are pointed out in comments.
  • Grafana-side PR will be easier to test with as it will include UI support and enhancements to the template builder to help when creating JSON.

@JacobsonMT JacobsonMT requested a review from a team as a code owner April 9, 2025 18:14
@github-project-automation github-project-automation bot moved this to In review in Alerting Apr 9, 2025
@JacobsonMT JacobsonMT requested review from moustafab and rwwiv April 9, 2025 18:17
Copy link
Contributor

@moustafab moustafab left a comment

Choose a reason for hiding this comment

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

Looks great! Nice work!

My only thoughts on the implementation are around further improvements in the supported functions. If we anticipate significant churn here it may be good to establish an experimental namespace which we document to allow for a bit of safer additions. I'm hesitant to support to much without clear needs and I think this approach you've taken so far is far preferable to pulling in an entire library until clear need is well documented/established.

Also, templating header values might come in handy in the future but maybe we can wait for a request from the community before adding that.

@JacobsonMT JacobsonMT force-pushed the jacobsonmt/templated-webhooks-squashed branch from 76be299 to 0e98a49 Compare April 10, 2025 00:33
Copy link
Contributor

@titolins titolins left a comment

Choose a reason for hiding this comment

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

weekly board maintenance ✅ 😂
great work 👏

- Keep original case of headers
- Remove redundant tmplErr nil check but keep comment
@JacobsonMT JacobsonMT merged commit cad0d38 into main Apr 11, 2025
2 checks passed
@JacobsonMT JacobsonMT deleted the jacobsonmt/templated-webhooks-squashed branch April 11, 2025 13:52
@github-project-automation github-project-automation bot moved this from In review to Done in Alerting Apr 11, 2025
davidfrickert pushed a commit to davidfrickert/alerting that referenced this pull request Apr 24, 2025
* Select gomplate template functions

* Webhook custom payload template

* Custom headers

* Remove need for .Extra on ExtendedData by adding actual fields

* Support empty payload

* Test fix

* Linting

* Address review comments

- Keep original case of headers
- Remove redundant tmplErr nil check but keep comment

* Add license info to individual files copied from gomplate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants