Skip to content

Commit

Permalink
better error messages from external alerting services
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Feb 3, 2016
1 parent f575d3a commit d1bd416
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
10 changes: 8 additions & 2 deletions services/alerta/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -83,11 +84,16 @@ func (s *Service) Alert(token, resource, event, environment, severity, status, g
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Error string `json:"error"`
}
r := &response{Error: "failed to understand Alerta response"}
dec := json.NewDecoder(resp.Body)
r := &response{Error: "failed to understand Alerta response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Error)
}
Expand Down
10 changes: 8 additions & 2 deletions services/hipchat/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -86,11 +87,16 @@ func (s *Service) Alert(room, token, message string, level kapacitor.AlertLevel)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Error string `json:"error"`
}
r := &response{Error: "failed to understand HipChat response"}
dec := json.NewDecoder(resp.Body)
r := &response{Error: "failed to understand HipChat response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Error)
}
Expand Down
10 changes: 8 additions & 2 deletions services/opsgenie/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -107,11 +108,16 @@ func (s *Service) Alert(teams []string, recipients []string, messageType, messag
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Message string `json:"message"`
}
r := &response{Message: "failed to understand OpsGenie response"}
dec := json.NewDecoder(resp.Body)
r := &response{Message: "failed to understand OpsGenie response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Message)
}
Expand Down
10 changes: 8 additions & 2 deletions services/pagerduty/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"

Expand Down Expand Up @@ -72,11 +73,16 @@ func (s *Service) Alert(incidentKey, desc string, details interface{}) error {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Message string `json:"message"`
}
r := &response{Message: "failed to understand PagerDuty response"}
dec := json.NewDecoder(resp.Body)
r := &response{Message: "failed to understand PagerDuty response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Message)
}
Expand Down
13 changes: 10 additions & 3 deletions services/sensu/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/influxdata/kapacitor"
"io/ioutil"
"log"
"net/http"

"github.com/influxdata/kapacitor"
)

type Service struct {
Expand Down Expand Up @@ -65,11 +67,16 @@ func (s *Service) Alert(name, output string, level kapacitor.AlertLevel) error {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Error string `json:"error"`
}
r := &response{Error: "failed to understand Sensu response"}
dec := json.NewDecoder(resp.Body)
r := &response{Error: "failed to understand Sensu response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Error)
}
Expand Down
11 changes: 8 additions & 3 deletions services/slack/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"

Expand Down Expand Up @@ -83,11 +83,16 @@ func (s *Service) Alert(channel, message string, level kapacitor.AlertLevel) err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Error string `json:"error"`
}
r := &response{Error: fmt.Sprintf("failed to understand Slack response %s", err)}
dec := json.NewDecoder(resp.Body)
r := &response{Error: "failed to understand Slack response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Error)
}
Expand Down
10 changes: 8 additions & 2 deletions services/victorops/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -72,11 +73,16 @@ func (s *Service) Alert(routingKey, messageType, message, entityID string, t tim
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
type response struct {
Message string `json:"message"`
}
r := &response{Message: "failed to understand VictorOps response"}
dec := json.NewDecoder(resp.Body)
r := &response{Message: "failed to understand VictorOps response: " + string(body)}
b := bytes.NewReader(body)
dec := json.NewDecoder(b)
dec.Decode(r)
return errors.New(r.Message)
}
Expand Down

0 comments on commit d1bd416

Please sign in to comment.