From 82c11d4c342d5c97b108f2aaf2a713d4e66a9f6f 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) --- CHANGELOG.next.asciidoc | 1 + libbeat/api/routes.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 1e1e9ad0926..4268362a21f 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -562,6 +562,7 @@ 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] - 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 8cb35cfc23c..bc72347cf28 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 +}