web/api: add alertmanager api

This commit is contained in:
Frederic Branczyk 2017-01-13 10:20:11 +01:00
parent 6ce97837ab
commit 389c6d0043
No known key found for this signature in database
GPG Key ID: CA14788B1E48B256
3 changed files with 58 additions and 12 deletions

View File

@ -71,6 +71,10 @@ type targetRetriever interface {
Targets() []*retrieval.Target Targets() []*retrieval.Target
} }
type alertmanagerRetriever interface {
Alertmanagers() []string
}
type response struct { type response struct {
Status status `json:"status"` Status status `json:"status"`
Data interface{} `json:"data,omitempty"` Data interface{} `json:"data,omitempty"`
@ -93,20 +97,22 @@ type API struct {
Storage local.Storage Storage local.Storage
QueryEngine *promql.Engine QueryEngine *promql.Engine
targetRetriever targetRetriever targetRetriever targetRetriever
alertmanagerRetriever alertmanagerRetriever
context func(r *http.Request) context.Context context func(r *http.Request) context.Context
now func() model.Time now func() model.Time
} }
// NewAPI returns an initialized API type. // NewAPI returns an initialized API type.
func NewAPI(qe *promql.Engine, st local.Storage, tr targetRetriever) *API { func NewAPI(qe *promql.Engine, st local.Storage, tr targetRetriever, ar alertmanagerRetriever) *API {
return &API{ return &API{
QueryEngine: qe, QueryEngine: qe,
Storage: st, Storage: st,
targetRetriever: tr, targetRetriever: tr,
context: route.Context, alertmanagerRetriever: ar,
now: model.Now, context: route.Context,
now: model.Now,
} }
} }
@ -139,6 +145,7 @@ func (api *API) Register(r *route.Router) {
r.Del("/series", instr("drop_series", api.dropSeries)) r.Del("/series", instr("drop_series", api.dropSeries))
r.Get("/targets", instr("targets", api.targets)) r.Get("/targets", instr("targets", api.targets))
r.Get("/alertmanagers", instr("alertmanagers", api.alertmanagers))
} }
type queryData struct { type queryData struct {
@ -375,6 +382,25 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError) {
return res, nil return res, nil
} }
type AlertmanagerDiscovery struct {
ActiveAlertmanagers []*AlertmanagerTarget `json:"activeAlertmanagers"`
}
type AlertmanagerTarget struct {
URL string `json:"url"`
}
func (api *API) alertmanagers(r *http.Request) (interface{}, *apiError) {
urls := api.alertmanagerRetriever.Alertmanagers()
ams := &AlertmanagerDiscovery{ActiveAlertmanagers: make([]*AlertmanagerTarget, len(urls))}
for i := range urls {
ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: urls[i]}
}
return ams, nil
}
func respond(w http.ResponseWriter, data interface{}) { func respond(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -39,6 +39,12 @@ func (f targetRetrieverFunc) Targets() []*retrieval.Target {
return f() return f()
} }
type alertmanagerRetrieverFunc func() []string
func (f alertmanagerRetrieverFunc) Alertmanagers() []string {
return f()
}
func TestEndpoints(t *testing.T) { func TestEndpoints(t *testing.T) {
suite, err := promql.NewTest(t, ` suite, err := promql.NewTest(t, `
load 1m load 1m
@ -71,11 +77,16 @@ func TestEndpoints(t *testing.T) {
} }
}) })
ar := alertmanagerRetrieverFunc(func() []string {
return []string{"http://alertmanager.example.com:8080/api/v1/alerts"}
})
api := &API{ api := &API{
Storage: suite.Storage(), Storage: suite.Storage(),
QueryEngine: suite.QueryEngine(), QueryEngine: suite.QueryEngine(),
targetRetriever: tr, targetRetriever: tr,
now: func() model.Time { return now }, alertmanagerRetriever: ar,
now: func() model.Time { return now },
} }
start := model.Time(0) start := model.Time(0)
@ -437,6 +448,15 @@ func TestEndpoints(t *testing.T) {
Health: "unknown", Health: "unknown",
}, },
}, },
}, {
endpoint: api.alertmanagers,
response: &AlertmanagerDiscovery{
ActiveAlertmanagers: []*AlertmanagerTarget{
&AlertmanagerTarget{
URL: "http://alertmanager.example.com:8080/api/v1/alerts",
},
},
},
}, },
} }

View File

@ -155,7 +155,7 @@ func New(o *Options) *Handler {
storage: o.Storage, storage: o.Storage,
notifier: o.Notifier, notifier: o.Notifier,
apiV1: api_v1.NewAPI(o.QueryEngine, o.Storage, o.TargetManager), apiV1: api_v1.NewAPI(o.QueryEngine, o.Storage, o.TargetManager, o.Notifier),
now: model.Now, now: model.Now,
} }