Skip to content

Commit

Permalink
Add unicode support and runbook url
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhny committed Nov 30, 2023
1 parent c844e0f commit 0e4e5bb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
17 changes: 13 additions & 4 deletions pkg/alerter/grafana_oncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions pkg/alerter/grafana_oncall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/smseagle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/smseagle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion test_files/grafana_webhooks/oncall_resolved_webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion test_files/grafana_webhooks/oncall_webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 0e4e5bb

Please sign in to comment.