Enable alert API endpoints

This commit is contained in:
Fabian Reinartz 2015-09-26 11:12:59 +02:00
parent 4b58d30f4d
commit 3b7f880f60
2 changed files with 50 additions and 44 deletions

91
api.go
View File

@ -4,24 +4,29 @@ import (
"encoding/json"
"fmt"
"net/http"
// "time"
"time"
"github.com/prometheus/common/route"
"golang.org/x/net/context"
"github.com/prometheus/alertmanager/provider"
"github.com/prometheus/alertmanager/types"
)
type API struct {
alerts provider.Alerts
// context is an indirection for testing.
context func(r *http.Request) context.Context
}
func NewAPI(r *route.Router) *API {
func NewAPI(r *route.Router, alerts provider.Alerts) *API {
api := &API{
context: route.Context,
alerts: alerts,
}
// r.Get("/alerts", api.listAlerts)
// r.Post("/alerts", api.addAlerts)
r.Get("/alerts", api.listAlerts)
r.Post("/alerts", api.addAlerts)
// r.Get("/silences", api.listSilences)
// r.Post("/silences", api.addSilence)
@ -51,49 +56,49 @@ func (e *apiError) Error() string {
return fmt.Sprintf("%s: %s", e.typ, e.err)
}
// func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
// alerts, err := api.state.Alert().GetAll()
// if err != nil {
// respondError(w, apiError{
// typ: errorBadData,
// err: err,
// }, nil)
// return
// }
// respond(w, alerts)
// }
func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
alerts, err := api.alerts.All()
if err != nil {
respondError(w, apiError{
typ: errorBadData,
err: err,
}, nil)
return
}
respond(w, alerts)
}
// func (api *API) addAlerts(w http.ResponseWriter, r *http.Request) {
// var alerts []*Alert
// if err := receive(r, &alerts); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// return
// }
// for _, alert := range alerts {
// now := time.Now()
func (api *API) addAlerts(w http.ResponseWriter, r *http.Request) {
var alerts []*types.Alert
if err := receive(r, &alerts); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, alert := range alerts {
now := time.Now()
// if alert.Timestamp.IsZero() {
// alert.Timestamp = now
// }
// if alert.CreatedAt.IsZero() {
// alert.CreatedAt = now
// }
// if alert.ResolvedAt.IsZero() {
// alert.ResolvedAt = alert.CreatedAt.Add(ResolveTimeout)
// }
// }
if alert.Timestamp.IsZero() {
alert.Timestamp = now
}
if alert.CreatedAt.IsZero() {
alert.CreatedAt = now
}
if alert.ResolvedAt.IsZero() {
alert.ResolvedAt = alert.CreatedAt.Add(ResolveTimeout)
}
}
// // TODO(fabxc): validate input.
// if err := api.state.Alert().Add(alerts...); err != nil {
// respondError(w, apiError{
// typ: errorBadData,
// err: err,
// }, nil)
// return
// }
// TODO(fabxc): validate input.
if err := api.alerts.Put(alerts...); err != nil {
respondError(w, apiError{
typ: errorBadData,
err: err,
}, nil)
return
}
// respond(w, nil)
// }
respond(w, nil)
}
// func (api *API) addSilence(w http.ResponseWriter, r *http.Request) {
// var sil Silence

View File

@ -40,10 +40,11 @@ func main() {
defer disp.Stop()
disp.ApplyConfig(conf)
go disp.Run()
router := route.New()
NewAPI(router.WithPrefix("/api"))
NewAPI(router.WithPrefix("/api"), memAlerts)
http.ListenAndServe(":9091", router)
}