alerting icon indicating copy to clipboard operation
alerting copied to clipboard

Add Matrix receiver

Open YourSandwich opened this issue 7 months ago • 2 comments

What

Adds a native Matrix receiver that posts alerts to a Matrix room via the Client-Server API with a pre-issued bot access token. The receiver sends an m.room.message event with both a plaintext body and an HTML formatted_body. The HTML splits alerts into firing and resolved sections and lists each alert with a sorted label summary and a source link, so clients that render HTML (Element, Cinny, FluffyChat) get a readable summary and clients that do not still get the plaintext.

Why

grafana/grafana#19777 has been open since 2019 asking for native Matrix notifications. Two earlier attempts (grafana/grafana#10606 and #29931) were closed because they targeted the legacy alerting engine that has since been removed. Unified alerting has been taking new receivers in this library ever since (mqtt, oncall, jira), so that reason no longer applies.

Until this lands, every Matrix user running Grafana has to put a shim HTTP server in front of the webhook contact point to translate the payload. This PR removes that step.

Scope

Kept deliberately narrow, matching how Slack, Telegram and Discord are wired.

Unencrypted rooms only. E2EE needs olm/megolm, device keys and cross-signing, which is essentially a full Matrix client. Alert rooms are bot rooms and usually have encryption off anyway. The limit is called out in the field description.

Internal room IDs only (!abc:server). No alias resolution, no extra round-trip to /directory/room/{alias}. Aliases get rejected at config time with a clear error.

Pre-issued access token. No login flow, no token refresh. Matches how Slack and Telegram handle credentials.

Config fields

Field Required Secret Notes
homeserverUrl yes no e.g. https://matrix.example.com
accessToken yes yes Bot account access token
roomId yes no Internal room ID, must start with !
messageType no no m.text (default) or m.notice
title no no Templated title, defaults to the shared Grafana title template
message no no Templated body, defaults to the shared Grafana message template

Testing

Table-driven TestNotify against receivers.MockNotificationService asserts the HTTP method, headers, URL shape (including path-escape of !room:server), and the rendered msgtype, body, format, formatted_body fields. Cases cover firing only, mixed firing and resolved with m.notice, title only, and a multi-label case that pins deterministic label ordering.

TestNewConfig covers missing homeserver, invalid URL, missing token, missing room, alias rejection (#public:server), invalid message type, trailing-slash trimming, secret decryption precedence, and a full round-trip against the shared fixture.

TestValidateResponse covers 2xx success, Matrix error JSON decoding (M_FORBIDDEN), and non-JSON error bodies.

The notify integration tests pick up Matrix automatically via notifytest.AllKnownV1ConfigsForTesting. TestGetSecretKeysForContactPointType gets a matrix-v1 case and the homeserverUrl URL override is added to TestHTTPConfig.

make test (race + netgo), make mod-check, and make lint all pass clean.

Follow-up

A companion PR against grafana/grafana will bump the grafana/alerting vendor, register the channel in pkg/services/ngalert/notifier/channels_config/available_channels.go, and add 'matrix' to the frontend GrafanaNotifierType union and the mock notifiers map. That goes out once this PR is merged and a release is tagged.

Related: grafana/grafana#19777


[!NOTE] Medium Risk Adds a new outbound notification integration that sends authenticated HTTP requests to Matrix homeservers, introducing new configuration/templating paths and external-call failure modes.

Overview Adds a new Matrix receiver that posts alerts to a Matrix room via the Client-Server API, sending m.room.message events with both plaintext body and HTML formatted_body (including firing/resolved sections and deterministic label rendering with size/UTF-8-safe truncation).

Wires the new integration through notify (schema, receiver parsing, factory creation) and the test harness (notifytest, schema/receiver tests) including secret-field registration (accessToken) and URL override support (homeserverUrl), and documents the feature in CHANGELOG.md.

Reviewed by Cursor Bugbot for commit 6813534eff4194a4fc3765bf5e50e67e73b49341. Bugbot is set up for automated code reviews on this repo. Configure here.

YourSandwich avatar Apr 13 '26 15:04 YourSandwich

CLA assistant check
All committers have signed the CLA.

cla-assistant[bot] avatar Apr 13 '26 15:04 cla-assistant[bot]

Heads up: receivers.TruncateInBytes (receivers/util.go:134) panics on multi-byte input. The r[:truncationTarget] at line 155 assumes len([]rune(s)) >= n, which only holds for ASCII. CJK or emoji strings go out of bounds:

panic: runtime error: slice bounds out of range [:49149] with capacity 32768

I hit this while addressing the Bugbot finding above, which is why I did not use the helper. This PR works around it with a local rune-safe truncation and does not touch util.go.

YourSandwich avatar Apr 13 '26 15:04 YourSandwich

Is this being look upon? Is there anyone with merge powers even considering of adding this feature? I'm not minding pending merge conflicts, but is it worth a hustle? I'd love to have this feature, I love Matrix, and I'm tired of organizations keep ignoring it as a decent feature.

carl-di-ortus avatar Jun 09 '26 06:06 carl-di-ortus