web/api: add alertmanager api
This commit is contained in:
parent
6ce97837ab
commit
389c6d0043
|
@ -71,6 +71,10 @@ type targetRetriever interface {
|
|||
Targets() []*retrieval.Target
|
||||
}
|
||||
|
||||
type alertmanagerRetriever interface {
|
||||
Alertmanagers() []string
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Status status `json:"status"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
|
@ -93,20 +97,22 @@ type API struct {
|
|||
Storage local.Storage
|
||||
QueryEngine *promql.Engine
|
||||
|
||||
targetRetriever targetRetriever
|
||||
targetRetriever targetRetriever
|
||||
alertmanagerRetriever alertmanagerRetriever
|
||||
|
||||
context func(r *http.Request) context.Context
|
||||
now func() model.Time
|
||||
}
|
||||
|
||||
// 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{
|
||||
QueryEngine: qe,
|
||||
Storage: st,
|
||||
targetRetriever: tr,
|
||||
context: route.Context,
|
||||
now: model.Now,
|
||||
QueryEngine: qe,
|
||||
Storage: st,
|
||||
targetRetriever: tr,
|
||||
alertmanagerRetriever: ar,
|
||||
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.Get("/targets", instr("targets", api.targets))
|
||||
r.Get("/alertmanagers", instr("alertmanagers", api.alertmanagers))
|
||||
}
|
||||
|
||||
type queryData struct {
|
||||
|
@ -375,6 +382,25 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError) {
|
|||
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{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
|
|
@ -39,6 +39,12 @@ func (f targetRetrieverFunc) Targets() []*retrieval.Target {
|
|||
return f()
|
||||
}
|
||||
|
||||
type alertmanagerRetrieverFunc func() []string
|
||||
|
||||
func (f alertmanagerRetrieverFunc) Alertmanagers() []string {
|
||||
return f()
|
||||
}
|
||||
|
||||
func TestEndpoints(t *testing.T) {
|
||||
suite, err := promql.NewTest(t, `
|
||||
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{
|
||||
Storage: suite.Storage(),
|
||||
QueryEngine: suite.QueryEngine(),
|
||||
targetRetriever: tr,
|
||||
now: func() model.Time { return now },
|
||||
Storage: suite.Storage(),
|
||||
QueryEngine: suite.QueryEngine(),
|
||||
targetRetriever: tr,
|
||||
alertmanagerRetriever: ar,
|
||||
now: func() model.Time { return now },
|
||||
}
|
||||
|
||||
start := model.Time(0)
|
||||
|
@ -437,6 +448,15 @@ func TestEndpoints(t *testing.T) {
|
|||
Health: "unknown",
|
||||
},
|
||||
},
|
||||
}, {
|
||||
endpoint: api.alertmanagers,
|
||||
response: &AlertmanagerDiscovery{
|
||||
ActiveAlertmanagers: []*AlertmanagerTarget{
|
||||
&AlertmanagerTarget{
|
||||
URL: "http://alertmanager.example.com:8080/api/v1/alerts",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -155,7 +155,7 @@ func New(o *Options) *Handler {
|
|||
storage: o.Storage,
|
||||
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,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue