dadevel / alertmanager-signal-receiver

Signal Messenger Support for Prometheus Alertmanager
MIT License
22 stars 6 forks source link

Can't get Message Template to work #11

Closed spammads closed 2 years ago

spammads commented 2 years ago

Thanks for the Webhook. Works very well for my businesscase. But I am not able to get the Message Templating to work. I pass:

- SIGNAL_RECEIVER_MESSAGE_TEMPLATE={{- if .Summary -}}{{ "\n" }}{{ .Summary }}{{- end -}}{{- if .Description -}}{{ "\n" }}{{ .Description }}{{- end }}``

but service replies with:

template: expansion:1:8: executing "expansion" at <.Summary>: can't evaluate field Summary in type *template.Context

Can you advise a bit how to correctly set the environment variable? Any advice appreciated. Thanks!

dadevel commented 2 years ago

Not sure what's going wrong there, maybe try a minimal example like {{ .Summary }} first. From the syntax in your example, I assume that your using docker-compose. You could try this:

signal-receiver:
  image: ghcr.io/dadevel/signal-receiver
  environment:
    SIGNAL_RECEIVER_MESSAGE_TEMPLATE: |
      {{ .Status | ToUpper }}
      {{ .AlertName }}
      {{ if .Instance -}}
      instance: {{ .Instance }}{{ "\n" }}
      {{- end -}}
      {{- if .Severity -}}
      severity: {{ .Severity }}{{ "\n" }}
      {{- end -}}
      {{- range $key, $value := .Labels -}}
      {{ $key | ToLower }}: {{ $value }}{{ "\n" }}
      {{- end -}}
      {{- range $key, $value := .Annotations -}}
      {{ $key | ToLower }}: {{ $value }}{{ "\n" }}
      {{- end -}}
      {{- if .Summary -}}
      {{ "\n" }}{{ .Summary }}
      {{- end -}}
      {{- if .Description -}}
      {{ "\n" }}{{ .Description }}
      {{- end }}
spammads commented 2 years ago

Thanks for taking your time. I am using docker swarm with a stack file. Tried your minimal example and got: failed to update service signalreceiver_app: Error response from daemon: rpc error: code = InvalidArgument desc = expanding env failed: expanding env "SIGNAL_RECEIVER_MESSAGE_TEMPLATE={{ .Status }}\n": template: expansion:1:3: executing "expansion" at <.Status>: can't evaluate field Status in type *template.Context

You got any idea what else I could try? Thanks. Best regards.

dadevel commented 2 years ago

I verified that specifying the environment variable SIGNAL_RECEIVER_MESSAGE_TEMPLATE={{ .Status }} works as expected, so I think it has to do something with your setup.

Maybe try this steps:

spammads commented 2 years ago

Tried your first suggestion. No luck. I think the problem is, that Docker Swarm uses Go-Templating for Service-Creation: https://docs.docker.com/engine/reference/commandline/service_create/#create-services-using-templates When I start your container with: SIGNAL_RECEIVER_MESSAGE_TEMPLATE={{ .Service.ID }} the creation succeeds & I get no error message. But of course the template does use the swarm-variable instead of the correct variable coming from the alert.

Any idea how to overcome this swarm-parsing?

dadevel commented 2 years ago

Ah, that makes sense. I never used Docker Swarm, but maybe if you use env_file instead of environment?

dadevel commented 2 years ago

Hey @CKranebitter did you found a solution?

spammads commented 2 years ago

Still working on it as docker swarm prohibits the use of env_file

dadevel commented 2 years ago

Ah, alright. Didn't know that. Than I'll keep this open for now.

spammads commented 2 years ago

Ok. Got the solution. It is possible to bypass the docker-go-templating, by putting the environmentvariable in another set of curly braces like this:

SIGNAL_RECEIVER_MESSAGE_TEMPLATE="{{`{{ .Status }}`}}"

Resolved it because of the thread here.

Issue can be closed.