Skip to content

Commit

Permalink
Merge pull request #40 from devopyio/add-better-logging
Browse files Browse the repository at this point in the history
Improve error logging
  • Loading branch information
devopsjonas authored May 26, 2019
2 parents c9abff4 + 14e895e commit 230a271
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions zabbixsender/zabbixsvc/zabbixsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
yaml "gopkg.in/yaml.v2"
)

type ZabbixSenderRequest struct {
// AlertmanageRequest this is request received from Alertmanager.
type AlertmanagerRequest struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
Status string `json:"status"`
Expand All @@ -28,6 +29,7 @@ type ZabbixSenderRequest struct {
Alerts []Alert `json:"alerts"`
}

// Alert is alert received from alertmanager.
type Alert struct {
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
Expand All @@ -40,7 +42,7 @@ type ZabbixResponse struct {
Info string `json:"info"`
}

//JSONHandler handles alerts
// JSONHandler handles alerts
type JSONHandler struct {
Sender *zabbixsnd.Sender
KeyPrefix string
Expand Down Expand Up @@ -70,7 +72,7 @@ func (h *JSONHandler) HandlePost(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
defer r.Body.Close()

var req ZabbixSenderRequest
var req AlertmanagerRequest
if err := dec.Decode(&req); err != nil {
alertsErrorsTotal.WithLabelValues("", "").Inc()

Expand Down Expand Up @@ -111,8 +113,8 @@ func (h *JSONHandler) HandlePost(w http.ResponseWriter, r *http.Request) {

res, err := h.zabbixSend(metrics)
if err != nil {
alertsErrorsTotal.WithLabelValues(req.Status, host).Inc()
log.Errorf("failed to send to server: %s", err)
alertsErrorsTotal.WithLabelValues(req.Status, host).Add(float64(len(req.Alerts)))
log.Errorf("failed to send to server: %s, request: %v", err, req)
http.Error(w, "failed to send to server", http.StatusInternalServerError)
return
}
Expand All @@ -137,7 +139,7 @@ func (h *JSONHandler) zabbixSend(metrics []*zabbixsnd.Metric) (*ZabbixResponse,

failed := strings.Trim(infoSplit[3], ";")
if failed != "0" || zres.Response != "success" {
return nil, errors.Errorf("failed to fulfill the requests: %v", failed)
return nil, errors.Errorf("failed to fulfill the requests: %v, info: %v, Data: %v", failed, zres.Info, zres.Response)
}

return &zres, nil
Expand Down

0 comments on commit 230a271

Please sign in to comment.