-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for customized monitoring API #22605
Changes from all commits
b358184
bfbed50
b1d3bc9
ebc290c
8222f53
340a67a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should error/panic if the api is already in use. A new handlerFunc should not be allowed to replace an older handlerFunc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now considered in the new implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverted to previous implementation and added check in this function.. |
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this PR is already merged but I just noticed this while reviewing the backport PR. Should these three API routes be added via
AddHandlerFunc
too? That would ensure that someone doesn't try to override these routes when registering a custom one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comments.
As mentioned in this reply, the usage of
AddHandlerFunc
would be registering other routes in a module's "init" phase, that makes customized routes to be shown up before "default" routes, and the reason "default" routes can not be done in "init" phase is the handler functions relies onns lookupFunc
which is passed only inNewWithDefaultRoutes
.I think what can be done is
AddHandlerFunc
for default routes as well so at least errors can be logged when there are conflicts.ns
to bemonitoring.GetNamespace
and change the interface ofNewWithDefaultRoutes
.Any thoughts?