From 4e31df2c2bc51a7d1057d20ffb48e6f49491c6ab Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 1 Jul 2015 11:26:45 -0700 Subject: [PATCH] etcdhttp: add config/local/log endpoint PUT on the endpoint sets the GlobalDebugLevel to json level value. The action overwrites the origianl log level setting from users. We need to write doc to warn this. --- etcdserver/etcdhttp/client.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/etcdserver/etcdhttp/client.go b/etcdserver/etcdhttp/client.go index 32391e843..41843d9ce 100644 --- a/etcdserver/etcdhttp/client.go +++ b/etcdserver/etcdhttp/client.go @@ -27,6 +27,7 @@ import ( "strings" "time" + "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork" "github.com/coreos/etcd/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" @@ -52,6 +53,7 @@ const ( metricsPath = "/metrics" healthPath = "/health" versionPath = "/version" + configPath = "/config" ) // NewClientHandler generates a muxed http.Handler with the given parameters to serve etcd client requests. @@ -98,6 +100,7 @@ func NewClientHandler(server *etcdserver.EtcdServer) http.Handler { mux.HandleFunc(statsPrefix+"/self", sh.serveSelf) mux.HandleFunc(statsPrefix+"/leader", sh.serveLeader) mux.HandleFunc(varsPath, serveVars) + mux.HandleFunc(configPath+"/local/log", logHandleFunc) mux.Handle(metricsPath, prometheus.Handler()) mux.Handle(membersPrefix, mh) mux.Handle(membersPrefix+"/", mh) @@ -383,6 +386,30 @@ func serveVersion(w http.ResponseWriter, r *http.Request, clusterV string) { w.Write(b) } +func logHandleFunc(w http.ResponseWriter, r *http.Request) { + if !allowMethod(w, r.Method, "PUT") { + return + } + + in := struct{ Level string }{} + + d := json.NewDecoder(r.Body) + if err := d.Decode(&in); err != nil { + writeError(w, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid json body")) + return + } + + logl, err := capnslog.ParseLevel(strings.ToUpper(in.Level)) + if err != nil { + writeError(w, httptypes.NewHTTPError(http.StatusBadRequest, "Invalid log level "+in.Level)) + return + } + + plog.Noticef("globalLogLevel set to %q", logl.String()) + capnslog.SetGlobalLogLevel(logl) + w.WriteHeader(http.StatusNoContent) +} + // parseKeyRequest converts a received http.Request on keysPrefix to // a server Request, performing validation of supplied fields as appropriate. // If any validation fails, an empty Request and non-nil error is returned.