Skip to content

Commit

Permalink
Implement sending allert from handler to alertmaner
Browse files Browse the repository at this point in the history
even if still to cleanup
  • Loading branch information
MalloZup committed Sep 26, 2020
1 parent 6462813 commit 5fe43fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
13 changes: 4 additions & 9 deletions alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -51,7 +48,9 @@ type AlertFire struct {
}

func (alert *AlertFire) sendAlert(url string) {
body := &alert
alerts := make([]AlertFire, 1)
alerts[0] = *alert
body := alerts
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, err := http.NewRequest("POST", url, buf)
Expand All @@ -63,10 +62,6 @@ func (alert *AlertFire) sendAlert(url string) {
if err != nil {
log.Error(err)
}

log.Infof("Alert from handler to alertmanager sent %s", alert.Labels.Alertname)
defer res.Body.Close()

fmt.Println("response Status:", res.Status)
// Print the body to the stdout
io.Copy(os.Stdout, res.Body)
}
10 changes: 5 additions & 5 deletions alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestAlertFiring(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// read body json from Prometheus alertmanager
decoder := json.NewDecoder(r.Body)
var alert AlertFire
alert := make([]AlertFire, 1)
decoder.Decode(&alert)
t.Log(alert.Status)
t.Log(alert.Labels.Component)
if alert.GeneratorURL != "unit-test" {
t.Errorf("got %s expected unit-test", alert.GeneratorURL)
t.Log(alert[0].Status)
t.Log(alert[0].Labels.Component)
if alert[0].GeneratorURL != "unit-test" {
t.Errorf("got %s expected unit-test", alert[0].GeneratorURL)
}
}))
defer ts.Close()
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func main() {

log.Infof("starting handler on port: %s", handlerWebSrvPort)

var a *AlertFire
a = new(AlertFire)
a.Status = "firing"
a.Labels.Alertname = "FOO-ALERT"
a.Labels.Component = "unit-test component"
a.Labels.Severity = "critical"
a.Labels.Instance = "test instance"
a.Annotations.Summary = "just a test"
a.GeneratorURL = "unit-test"

a.sendAlert("http://10.162.31.2:9093/api/v1/alerts")

// register the various handler
h := new(HanaDiskFull)
// make sure we run only 1 handler until it finish.
Expand Down

0 comments on commit 5fe43fd

Please sign in to comment.