Skip to content

Commit

Permalink
Merge pull request #9 from stephen-soltesz/pr-add-alert-handler
Browse files Browse the repository at this point in the history
Add -enable-auto-close option to github-receiver
  • Loading branch information
stephen-soltesz authored Dec 6, 2017
2 parents 5a5ae2a + df91b0d commit 9dfa3d3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
14 changes: 12 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ language: go
go:
- 1.8.1

before_install:
# Coverage tools
- go get github.com/mattn/goveralls
- go get github.com/wadey/gocovmerge

# Unconditionally place the repo at GOPATH/src/${go_import_path} to support
# forks.
go_import_path: github.com/m-lab/alertmanager-github-receiver

script:
# Run query "unit tests".
- go test -v github.com/m-lab/alertmanager-github-receiver/...
# Run unit tests.
- go test -covermode=count -coverprofile=alerts.cov -v github.com/m-lab/alertmanager-github-receiver/alerts
- go test -covermode=count -coverprofile=issues.cov -v github.com/m-lab/alertmanager-github-receiver/issues

# Coveralls
- $HOME/gopath/bin/gocovmerge alerts.cov issues.cov > merge.cov
- $HOME/gopath/bin/goveralls -coverprofile=merge.cov -service=travis-ci
14 changes: 7 additions & 7 deletions alerts/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ type ReceiverClient interface {
}

type ReceiverHandler struct {
// Client is an implementation of the ReceiverClient interface. Client is used to handle requests.
Client ReceiverClient

// AutoClose indicates whether resolved issues that are still open should be closed automatically.
AutoClose bool
}

// ServeHTTP receives and processes alertmanager notifications. If the alert
Expand Down Expand Up @@ -106,13 +110,9 @@ func (rh *ReceiverHandler) processAlert(msg *notify.WebhookMessage) error {
return err
}

// TODO(dev): every alert is an incident. So, open issues are a sign of
// either a real problem or a bad alert. Stop auto-closing issues once we
// are confident that the github receiver is well behaved.

// The message is resolved and we found a matching open issue from github,
// so close the issue.
if msg.Data.Status == "resolved" && foundIssue != nil {
// The message is resolved and we found a matching open issue from github.
// If AutoClose is true, then close the issue.
if msg.Data.Status == "resolved" && foundIssue != nil && rh.AutoClose {
// NOTE: there can be multiple "resolved" messages for the same
// alert. Prometheus evaluates rules every `evaluation_interval`.
// And, alertmanager preserves an alert until `resolve_timeout`. So
Expand Down
9 changes: 5 additions & 4 deletions alerts/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/go-github/github"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/google/go-github/github"

"github.com/m-lab/alertmanager-github-receiver/alerts"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"
"github.com/m-lab/alertmanager-github-receiver/alerts"
)

type fakeClient struct {
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestReceiverHandler(t *testing.T) {
createIssue("DiskRunningFull", "body1"),
},
}
handler := alerts.ReceiverHandler{f}
handler := alerts.ReceiverHandler{f, true}
handler.ServeHTTP(rw, req)
resp := rw.Result()

Expand Down Expand Up @@ -142,7 +143,7 @@ func TestReceiverHandler(t *testing.T) {

// No pre-existing issues to close.
f = &fakeClient{}
handler = alerts.ReceiverHandler{f}
handler = alerts.ReceiverHandler{f, true}
handler.ServeHTTP(rw, req)
resp = rw.Result()

Expand Down
14 changes: 8 additions & 6 deletions cmd/github_receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ package main
import (
"flag"
"fmt"
"github.com/m-lab/alertmanager-github-receiver/alerts"
"github.com/m-lab/alertmanager-github-receiver/issues"
"net/http"
"os"

"github.com/m-lab/alertmanager-github-receiver/alerts"
"github.com/m-lab/alertmanager-github-receiver/issues"
)

var (
authtoken = flag.String("authtoken", "", "Oauth2 token for access to github API.")
githubOwner = flag.String("owner", "", "The github user or organization name.")
githubRepo = flag.String("repo", "", "The repository where issues are created.")
authtoken = flag.String("authtoken", "", "Oauth2 token for access to github API.")
githubOwner = flag.String("owner", "", "The github user or organization name.")
githubRepo = flag.String("repo", "", "The repository where issues are created.")
enableAutoClose = flag.Bool("enable-auto-close", false, "Once an alert stops firing, automatically close open issues.")
)

const (
Expand All @@ -51,7 +53,7 @@ func init() {

func serveListener(client *issues.Client) {
http.Handle("/", &issues.ListHandler{client})
http.Handle("/v1/receiver", &alerts.ReceiverHandler{client})
http.Handle("/v1/receiver", &alerts.ReceiverHandler{client, *enableAutoClose})
http.ListenAndServe(":9393", nil)
}

Expand Down
5 changes: 3 additions & 2 deletions issues/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func setupServer() *url.URL {
testMux = http.NewServeMux()
testServer = httptest.NewServer(testMux)

// test server URL is guaranteed to parse successfully.
url, _ := url.Parse(testServer.URL)
// Test server URL is guaranteed to parse successfully.
// The github client library requires that the URL end with a slash.
url, _ := url.Parse(testServer.URL + "/")
return url
}

Expand Down

0 comments on commit 9dfa3d3

Please sign in to comment.