Skip to content

Commit

Permalink
Add trace-level logging of write to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
otoolep committed Mar 6, 2015
1 parent 76c255a commit 2402467
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
3 changes: 3 additions & 0 deletions cmd/influxd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 25 additions & 6 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -129,17 +132,22 @@ 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)
}

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

0 comments on commit 2402467

Please sign in to comment.