From 2402467e63a416d6fa19062d93f86844d24b7a76 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Thu, 5 Mar 2015 17:01:02 -0800 Subject: [PATCH] Add trace-level logging of write to handler --- CHANGELOG.md | 1 + cmd/influxd/run.go | 3 +++ httpd/handler.go | 31 +++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8645f3944f..b48aec90ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Features - [#1755](https://github.com/influxdb/influxdb/pull/1848): Support JSON data ingest over UDP - [#1857](https://github.com/influxdb/influxdb/pull/1857): Support retention policies with infinite duration +- [#1858](https://github.com/influxdb/influxdb/pull/1858): Enable detailed tracing of write path ## v0.9.0-rc7 [2015-03-02] diff --git a/cmd/influxd/run.go b/cmd/influxd/run.go index 5792565e113..712177c20d5 100644 --- a/cmd/influxd/run.go +++ b/cmd/influxd/run.go @@ -80,6 +80,9 @@ func Run(config *Config, join, version string, logWriter *os.File) (*messaging.B // Start the server handler. Attach to broker if listening on the same port. if s != nil { sh := httpd.NewHandler(s, config.Authentication.Enabled, version) + sh.SetLogOutput(logWriter) + sh.WriteTrace = config.Logging.WriteTraceEnabled + if h != nil && config.BrokerAddr() == config.DataAddr() { h.serverHandler = sh } else { diff --git a/httpd/handler.go b/httpd/handler.go index 94749b7a18d..2cb601e2bb7 100644 --- a/httpd/handler.go +++ b/httpd/handler.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "log" "math" "net/http" @@ -43,6 +44,9 @@ type Handler struct { routes []route mux *pat.PatternServeMux requireAuthentication bool + + Logger *log.Logger + WriteTrace bool // Detailed logging of write path } // NewHandler returns a new instance of Handler. @@ -51,10 +55,9 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool, version string) server: s, mux: pat.New(), requireAuthentication: requireAuthentication, + Logger: log.New(os.Stderr, "[http] ", log.LstdFlags), } - weblog := log.New(os.Stderr, `[http] `, 0) - h.routes = append(h.routes, route{ "query", // Query serving route. @@ -129,9 +132,9 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool, version string) handler = cors(handler) handler = requestID(handler) if r.log { - handler = logging(handler, r.name, weblog) + handler = logging(handler, r.name, h.Logger) } - handler = recovery(handler, r.name, weblog) // make sure recovery is always last + handler = recovery(handler, r.name, h.Logger) // make sure recovery is always last h.mux.Add(r.method, r.pattern, handler) } @@ -139,7 +142,12 @@ func NewHandler(s *influxdb.Server, requireAuthentication bool, version string) return h } -//ServeHTTP responds to HTTP request to the handler. +// SetLogOutput sets writer for all handler log output. +func (h *Handler) SetLogOutput(w io.Writer) { + h.Logger = log.New(w, "[http] ", log.LstdFlags) +} + +// ServeHTTP responds to HTTP request to the handler. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.mux.ServeHTTP(w, r) } @@ -168,8 +176,19 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user *influ // serveWrite receives incoming series data and writes it to the database. func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *influxdb.User) { var bp influxdb.BatchPoints + var dec *json.Decoder - dec := json.NewDecoder(r.Body) + if h.WriteTrace { + b, err := ioutil.ReadAll(r.Body) + if err != nil { + h.Logger.Print("write handler failed to read bytes from request body") + } else { + h.Logger.Printf("write body received by handler: %s", string(b)) + } + dec = json.NewDecoder(strings.NewReader(string(b))) + } else { + dec = json.NewDecoder(r.Body) + } var writeError = func(result influxdb.Result, statusCode int) { w.WriteHeader(statusCode)