Skip to content

Commit

Permalink
Add some better logs and test for failed request
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhny committed Dec 7, 2023
1 parent 1766771 commit 8fe39c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/alerter/grafana_oncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (g *GrafanaOncall) handleRequest(w http.ResponseWriter, r *http.Request, c
slog.Debug("Checking for phonenumber")
phoneNumbers := getPhoneNumber(webhook, g.cfg.Users)
if len(phoneNumbers) == 0 {
slog.Warn("No phonenumber found", "users", webhook.UsersToNotify, "users in config", g.cfg.Users)
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Missing or invalid users")
failedOncallRequestsCounter.Inc()
Expand Down
6 changes: 6 additions & 0 deletions pkg/smseagle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func sendSMS(cfg *config.ProxyConfig, phoneNumber string, message string, client
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("response code not 200. Request: %s/http_api/send_sms?access_token=%s&to=%s&message=%s&unicode=1", cfg.SMS.Url, "x", phoneNumber, message)
}
slog.Debug("sms request successful", "response code", res.StatusCode, "response text", res.Status)
return nil
}
Expand All @@ -25,6 +28,9 @@ func call(cfg *config.ProxyConfig, phoneNumber string, client *http.Client) erro
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("response code not 200. Request: %s/http_api/call_with_termination?access_token=%s&to=%s&duration=30", cfg.Call.Url, "x", phoneNumber)
}
slog.Debug("Call request successful", "response code", res.StatusCode, "response text", res.Status)
return nil
}
18 changes: 18 additions & 0 deletions pkg/smseagle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ var _ = Describe("Handler", func() {
err := smseagle.Notify(&msg)
Expect(err).ShouldNot(HaveOccurred())
})
It("should throw error if response is not 200", func() {
msg := SMSEagleMessage{Message: "hei pa deg, urltest: https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook+for+SKIP-alarmer#HostOutOfInodes", PhoneNumber: "123"}
exptectedSMSMsg := "hei+pa+deg,+urltest:+https://kartverket.atlassian.net/wiki/spaces/SKIP/pages/713359536/Playbook%2Bfor%2BSKIP-alarmer%23HostOutOfInodes"
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(
ghttp.VerifyRequest("GET", "/http_api/send_sms", expectedSMSQuery),
ghttp.RespondWith(http.StatusBadRequest, "Bad"),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/http_api/call_with_termination", expectedCallQuery),
ghttp.RespondWith(http.StatusBadRequest, "Bad"),
),
)
err := smseagle.Notify(&msg)
Expect(err).Should(HaveOccurred())
})
})

})

0 comments on commit 8fe39c8

Please sign in to comment.