From 18832e40d102e4435622b6590aac2be186cbb7b7 Mon Sep 17 00:00:00 2001 From: Peter Deng Date: Mon, 21 Dec 2020 21:34:26 +0800 Subject: [PATCH] Add support for customized monitoring API (#22605) (cherry picked from commit 82c11d4c342d5c97b108f2aaf2a713d4e66a9f6f) --- CHANGELOG.next.asciidoc | 2 ++ libbeat/api/routes.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 49d3c5ff12b5..1d7075b29056 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -384,6 +384,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Added new `rate_limit` processor for enforcing rate limits on event throughput. {pull}22883[22883] - Allow node/namespace metadata to be disabled on kubernetes metagen and ensure add_kubernetes_metadata honors host {pull}23012[23012] - Improve equals check. {pull}22778[22778] +- Honor kube event resysncs to handle missed watch events {pull}22668[22668] +- Add support for customized monitoring API. {pull}22605[22605] *Auditbeat* diff --git a/libbeat/api/routes.go b/libbeat/api/routes.go index 8cb35cfc23ca..bc72347cf28b 100644 --- a/libbeat/api/routes.go +++ b/libbeat/api/routes.go @@ -30,6 +30,8 @@ import ( type handlerFunc func(http.ResponseWriter, *http.Request) type lookupFunc func(string) *monitoring.Namespace +var handlerFuncMap = make(map[string]handlerFunc) + // NewWithDefaultRoutes creates a new server with default API routes. func NewWithDefaultRoutes(log *logp.Logger, config *common.Config, ns lookupFunc) (*Server, error) { mux := http.NewServeMux() @@ -38,6 +40,10 @@ func NewWithDefaultRoutes(log *logp.Logger, config *common.Config, ns lookupFunc mux.HandleFunc("/state", makeAPIHandler(ns("state"))) mux.HandleFunc("/stats", makeAPIHandler(ns("stats"))) mux.HandleFunc("/dataset", makeAPIHandler(ns("dataset"))) + + for api, h := range handlerFuncMap { + mux.HandleFunc(api, h) + } return New(log, mux, config) } @@ -73,3 +79,12 @@ func prettyPrint(w http.ResponseWriter, data common.MapStr, u *url.URL) { fmt.Fprintf(w, data.String()) } } + +// AddHandlerFunc provides interface to add customized handlerFunc +func AddHandlerFunc(api string, h handlerFunc) error { + if _, exist := handlerFuncMap[api]; exist { + return fmt.Errorf("%s already exist", api) + } + handlerFuncMap[api] = h + return nil +}