Skip to content

Commit

Permalink
Cherry-pick #22605 to 7.x: Add support for customized monitoring API (#…
Browse files Browse the repository at this point in the history
…23279)

Co-authored-by: Peter Deng <[email protected]>
  • Loading branch information
Steffen Siering and newly12 authored Dec 30, 2020
1 parent 7a5c9b6 commit c04dd7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Update Go version to 1.14.7. {pull}20508[20508]
- Add packaging for docker image based on UBI minimal 8. {pull}20576[20576]
- Make the mage binary used by the build process in the docker container to be statically compiled. {pull}20827[20827]
- Add support for customized monitoring API. {pull}22605[22605]

15 changes: 15 additions & 0 deletions libbeat/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}

0 comments on commit c04dd7f

Please sign in to comment.