DRuggeri / alertmanager_gotify_bridge

A bridge between Prometheus AlertManager and a Gotify server
Apache License 2.0
65 stars 9 forks source link

feature(markdown): Enables markdown notifications support #32

Closed thomas-maurice closed 9 months ago

thomas-maurice commented 9 months ago

This pull request enables the support for markdown being rendered properly on the client side via a --markdown flag (or the corresponding MARKDOWN environment variable). This also removes the support for HTML as it is not supported on gotify mobile.

This has the side effect of not allowing to add colours in the notifications, but that didn't work initially anyways.

Similarly it allows to set a --click_to_generator flag (CLICK_TO_GENERATOR environment variable) that allows to make the client notification clickable when opened on mobile. Previously this behaviour implied the use of --extended_details, but this would also force the use of HTML, which is not supported (as per #31 and my own testing).

End result, the following alert would show up as follows

{
  "receiver": "webhook",
  "status": "firing",
  "alerts": [
    {
      "status": "firing",
      "labels": {
        "alertname": "TestAlertName",
        "dc": "eu-west-1",
        "instance": "somehost.fr",
        "job": "prometheus"
      },
      "annotations": {
        "description": "some description **but with** _markdown_ ~~test~~ [a link](https://google.fr)",
        "summary": "This is the alert summary, something is really bad",
        "priority": "critical"
      },
      "startsAt": "2018-08-03T09:52:26.739266876+02:00",
      "endsAt": "0001-01-01T00:00:00Z",
      "generatorURL": "https://google.fr"                                                                                  
    }
  ],
  "externalURL": "http://example.com:9093",
  "version": "4",
  "groupKey": "{}:{alertname=\"Test\", job=\"prometheus\"}"
}

On web: Screenshot from 2024-02-17 12-29-23

On the app: image

The notification sends me to the generator URL specified in the alert when I click on it on mobile, and nothing happens when the generator URL is absent from the json.

For reproducibility, the templates I used are: title.tmpl:

{{ define "title=TOKEN" }}
{{- if eq (.Status) ("firing") }}🔥 {{ .Labels.alertname }}{{else}}✅ {{ .Labels.alertname }}{{end}}
{{end}}

main.tmpl:

{{- define "TOKEN" -}}
{{- .Annotations.summary }}

{{ .Annotations.description }}

**Labels**:
{{ range $key, $value := .Labels -}}
{{ $key }} = {{ $value }}
{{ end }}
{{- end }}
thomas-maurice commented 9 months ago

Thanks for the review!

I'd also like to float an idea out there... any interest in just replacing the extendedDetails HTML implementation with markdown while in the neighborhood?

I can definitely do that.

My alerts are also pretty vanilla but it is nice to add some markdown in the templates for links, highlighting important labels and so on, so i figured why not add a bit of style to my notifications saying that my homelab is down

I'll get to it over the weekend :)

thomas-maurice commented 9 months ago

@DRuggeri fixed, and updated the pull request description to reflect the change/give you all you need to test it on your side if you want to !