Skip to content

Commit

Permalink
Merge pull request #30 from m-lab/sandbox-roberto-improve-metrics
Browse files Browse the repository at this point in the history
Add amreceiver_alerts_total metric
  • Loading branch information
robertodauria authored May 29, 2019
2 parents b98ff0e + ac81e66 commit 03ebae4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
27 changes: 27 additions & 0 deletions alerts/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ import (

"github.com/google/go-github/github"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
receivedAlerts = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "githubreceiver_alerts_total",
Help: "Number of incoming alerts from AlertManager.",
},
[]string{"alertname", "status"},
)

createdIssues = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "githubreceiver_created_issues_total",
Help: "Number of firing issues for which an alert has been created.",
},
// Here "status" can only be "firing" thus it's not a label.
[]string{"alertname"},
)
)

// ReceiverClient defines all issue operations needed by the ReceiverHandler.
Expand Down Expand Up @@ -112,11 +133,17 @@ func (rh *ReceiverHandler) processAlert(msg *notify.WebhookMessage) error {
}
}

var alertName = msg.Data.GroupLabels["alertname"]
receivedAlerts.WithLabelValues(alertName, msg.Data.Status).Inc()

// The message is currently firing and we did not find a matching
// issue from github, so create a new issue.
if msg.Data.Status == "firing" && foundIssue == nil {
msgBody := formatIssueBody(msg)
_, err := rh.Client.CreateIssue(rh.getTargetRepo(msg), msgTitle, msgBody, rh.ExtraLabels)
if err == nil {
createdIssues.WithLabelValues(alertName).Inc()
}
return err
}

Expand Down
7 changes: 7 additions & 0 deletions alerts/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"github.com/m-lab/go/prometheusx/promtest"

"github.com/google/go-github/github"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"
Expand Down Expand Up @@ -245,3 +247,8 @@ func TestReceiverHandler_ServeHTTP(t *testing.T) {
})
}
}

func TestMetrics(t *testing.T) {
receivedAlerts.WithLabelValues("x", "y")
promtest.LintMetrics(t)
}

0 comments on commit 03ebae4

Please sign in to comment.