From 0e4e5bb0703b753819ee0c8c29eedb1bb80be260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haram=20Nyg=C3=A5rd?= Date: Thu, 30 Nov 2023 12:54:20 +0100 Subject: [PATCH] Add unicode support and runbook url --- pkg/alerter/grafana_oncall.go | 17 +++++++++++++---- pkg/alerter/grafana_oncall_test.go | 8 ++++---- pkg/smseagle/client.go | 2 +- pkg/smseagle/handler_test.go | 2 +- .../oncall_resolved_webhook.json | 4 +++- test_files/grafana_webhooks/oncall_webhook.json | 4 +++- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/alerter/grafana_oncall.go b/pkg/alerter/grafana_oncall.go index c5e4b11..97f8111 100644 --- a/pkg/alerter/grafana_oncall.go +++ b/pkg/alerter/grafana_oncall.go @@ -12,9 +12,18 @@ import ( "time" ) +type AlertPayload struct { + CommonAnnotations Annotation `json:"commonAnnotations"` +} + +type Annotation struct { + RunbookUrl string `json:"runbook_url"` +} + type OncallWebhook struct { - AlertGroup AlertGroup `json:"alert_group"` - Event Event `json:"event"` + AlertGroup AlertGroup `json:"alert_group"` + Event Event `json:"event"` + AlertPayload AlertPayload `json:"alert_payload"` } type Event struct { @@ -124,9 +133,9 @@ func (g *GrafanaOncall) handleRequest(w http.ResponseWriter, r *http.Request, c func createMessage(webhook *OncallWebhook) (string, error) { if webhook.Event.Type == Escalation { - return fmt.Sprintf("Ny Alarm \nId: %s \nOpprettet: %s \nTittel: %s \nAntall: %d\nLenke: %s", + return fmt.Sprintf("Ny Alarm \nId: %s \nOpprettet: %s \nTittel: %s \nAntall: %d\nLenke: %s\nPlaybook: %s", webhook.AlertGroup.Id, webhook.AlertGroup.Created.Format("2006-1-2 15:4:3"), webhook.AlertGroup.Title, - webhook.AlertGroup.AlertsCount, webhook.AlertGroup.Permalinks.Web), nil + webhook.AlertGroup.AlertsCount, webhook.AlertGroup.Permalinks.Web, webhook.AlertPayload.CommonAnnotations.RunbookUrl), nil } else if webhook.Event.Type == Resolve { return fmt.Sprintf("Alarm løst \nId: %s \nLøst: %s \nTittel: %s \nAntall: %d \nLenke: %s", webhook.AlertGroup.Id, webhook.AlertGroup.Resolved.Format("2006-1-2 15:4:3"), webhook.AlertGroup.Title, diff --git a/pkg/alerter/grafana_oncall_test.go b/pkg/alerter/grafana_oncall_test.go index e000846..bdd8ead 100644 --- a/pkg/alerter/grafana_oncall_test.go +++ b/pkg/alerter/grafana_oncall_test.go @@ -90,7 +90,7 @@ var _ = Describe("GrafanaOncall", func() { Expect(mock.message.PhoneNumber).Should(Equal("123")) }) It("message should be correct", func() { - Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY")) + Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY\nPlaybook: https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes")) }) It("should have sms contact type", func() { Expect(mock.message.ContactType).Should(Equal(smseagle.SMS)) @@ -110,7 +110,7 @@ var _ = Describe("GrafanaOncall", func() { Expect(mock.message.PhoneNumber).Should(Equal("456")) }) It("message should be correct", func() { - Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY")) + Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY\nPlaybook: https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes")) }) It("should have sms contact type", func() { Expect(mock.message.ContactType).Should(Equal(smseagle.SMS)) @@ -140,7 +140,7 @@ var _ = Describe("GrafanaOncall", func() { Expect(mock.message.PhoneNumber).Should(Equal("123")) }) It("message should be correct", func() { - Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY")) + Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY\nPlaybook: https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes")) }) It("should have call contact type", func() { Expect(mock.message.ContactType).Should(Equal(smseagle.Call)) @@ -160,7 +160,7 @@ var _ = Describe("GrafanaOncall", func() { Expect(mock.message.PhoneNumber).Should(Equal("456")) }) It("message should be correct", func() { - Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY")) + Expect(mock.message.Message).Should(Equal("Ny Alarm \nId: I57917WDFNGHY \nOpprettet: 2023-10-12 12:17:12 \nTittel: [firing:3] InstanceDown \nAntall: 1\nLenke: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY\nPlaybook: https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes")) }) It("should have call contact type", func() { Expect(mock.message.ContactType).Should(Equal(smseagle.Call)) diff --git a/pkg/smseagle/client.go b/pkg/smseagle/client.go index 1a7a639..3dfb9a8 100644 --- a/pkg/smseagle/client.go +++ b/pkg/smseagle/client.go @@ -8,7 +8,7 @@ import ( ) func sendSMS(cfg *config.ProxyConfig, phoneNumber string, message string, client *http.Client) error { - requestUrl := fmt.Sprintf("%s/http_api/send_sms?access_token=%s&to=%s&message=%s", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message) + requestUrl := fmt.Sprintf("%s/http_api/send_sms?access_token=%s&to=%s&message=%s&unicode=1", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message) slog.Debug("Sending sms", "url", requestUrl) res, err := client.Get(requestUrl) if err != nil { diff --git a/pkg/smseagle/handler_test.go b/pkg/smseagle/handler_test.go index 7a974fe..331e6e8 100644 --- a/pkg/smseagle/handler_test.go +++ b/pkg/smseagle/handler_test.go @@ -39,7 +39,7 @@ var _ = Describe("Handler", func() { It("should make get requests with correct queries to sms and call", func() { msg := SMSEagleMessage{Message: "hei pa deg", PhoneNumber: "123"} exptectedSMSMsg := "hei+pa+deg" - expectedSMSQuery := fmt.Sprintf("access_token=%s&to=%s&message=%s", cfg.SMS.AccessToken, "123", exptectedSMSMsg) + expectedSMSQuery := fmt.Sprintf("access_token=%s&to=%s&message=%s&unicode=1", cfg.SMS.AccessToken, "123", exptectedSMSMsg) expectedCallQuery := fmt.Sprintf("access_token=%s&to=%s", cfg.Call.AccessToken, "123") server.AppendHandlers( ghttp.CombineHandlers( diff --git a/test_files/grafana_webhooks/oncall_resolved_webhook.json b/test_files/grafana_webhooks/oncall_resolved_webhook.json index 0288ebe..62bb32d 100644 --- a/test_files/grafana_webhooks/oncall_resolved_webhook.json +++ b/test_files/grafana_webhooks/oncall_resolved_webhook.json @@ -98,7 +98,9 @@ "alertname": "InstanceDown" }, "truncatedAlerts": 0, - "commonAnnotations": {} + "commonAnnotations": { + "runbook_url": "https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes" + } }, "integration": { "id": "CUHVC65TCDY7K", diff --git a/test_files/grafana_webhooks/oncall_webhook.json b/test_files/grafana_webhooks/oncall_webhook.json index a0cb241..742e30c 100644 --- a/test_files/grafana_webhooks/oncall_webhook.json +++ b/test_files/grafana_webhooks/oncall_webhook.json @@ -93,7 +93,9 @@ "alertname": "InstanceDown" }, "truncatedAlerts": 0, - "commonAnnotations": {}, + "commonAnnotations": { + "runbook_url": "https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes" + }, "title": "Instance localhost:8082 down" }, "integration": {