Skip to content

Commit

Permalink
refactor alert ingestion
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Oct 16, 2023
1 parent 8eca79d commit 3805655
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
19 changes: 19 additions & 0 deletions api/v1/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
package v1

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/oklog/ulid/v2"
"github.com/whawty/alerts/store"
)

Expand All @@ -51,6 +53,23 @@ func (api *API) ListAlerts(c *gin.Context) {
c.JSON(http.StatusOK, AlertsListing{alerts})
}

func (api *API) CreateAlert(c *gin.Context) {
alert := &store.Alert{}
err := json.NewDecoder(c.Request.Body).Decode(alert)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding alert: " + err.Error()})
return
}
alert.State = store.StateNew
alert.ID = ulid.Make().String()

if alert, err = api.store.CreateAlert(alert); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusCreated, alert)
}

func (api *API) ReadAlert(c *gin.Context) {
id := c.Param("alert-id")

Expand Down
3 changes: 2 additions & 1 deletion api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ func InstallHTTPHandler(r *gin.RouterGroup, st *store.Store) {
alerts := r.Group("alerts")
{
alerts.GET("", api.ListAlerts)
alerts.POST("", api.CreateAlert)
alerts.GET(":alert-id", api.ReadAlert)
alerts.PATCH(":alert-id/state", api.UpdateAlertState)
alerts.DELETE(":alert-id", api.DeleteAlert)
}

submit := r.Group("submit")
{
submit.POST("alertmanager", api.SubmitAlertmanager)
submit.POST("prometheus", api.SubmitPrometheus)
}
}
11 changes: 6 additions & 5 deletions api/v1/submit_alertmanager.go → api/v1/submit_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ import (
"github.com/whawty/alerts/store"
)

func alertFromAlertmanagerMessage(msg *amWebhook.Message) *store.Alert {
func alertFromPrometheusAlertmanagerMessage(msg *amWebhook.Message) *store.Alert {
// TODO: implement this

return &store.Alert{}
}

func (api *API) SubmitAlertmanager(c *gin.Context) {
func (api *API) SubmitPrometheus(c *gin.Context) {
msg := &amWebhook.Message{}
err := json.NewDecoder(c.Request.Body).Decode(msg)
if err != nil {
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding alertmanager message: " + err.Error()})
c.JSON(http.StatusBadRequest, ErrorResponse{Error: "error decoding prometheus-alertmanager message: " + err.Error()})
return
}

alert := alertFromAlertmanagerMessage(msg)
alert := alertFromPrometheusAlertmanagerMessage(msg)
if alert, err = api.store.CreateAlert(alert); err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusCreated, alert)
c.JSON(http.StatusCreated, nil)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/alertmanager v0.26.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down

0 comments on commit 3805655

Please sign in to comment.