Skip to content

Commit

Permalink
Log canceled client requests at info level (#4947)
Browse files Browse the repository at this point in the history
"zed serve" logs canceled client requests (specifically, those that end
due to a context.Canceled error appearing to originate from the request
context) at warn level, which includes a stack trace, with an alarming
message.

    {
      "level":"warn",
      "ts":1702997509.358625,
      "logger":"core",
      "msg":"Error",
      "request_id":"2ZlXC74Oeg2zEXQXX8ol8Ma1Lyd",
      "status":500,
      "error":"context canceled",
      "stacktrace":"..."
    }

Change the level to info, thereby omitting the stack trace, and change
the message to one less alarming.

    {
      "level":"info",
      "ts":1702998051.5594501,
      "logger":"core",
      "msg":"Request context canceled",
      "request_id":"2ZlYICXG8lrypWxyIxUBoM84qjT"
    }
  • Loading branch information
nwt authored Dec 19, 2023
1 parent a246bfe commit 4c7de04
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions service/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"context"
"encoding/json"
"errors"
"io/fs"
Expand Down Expand Up @@ -43,6 +44,7 @@ func newRequest(w http.ResponseWriter, r *http.Request, c *Core) (*ResponseWrite
ResponseWriter: w,
Logger: req.Logger,
marshaler: m,
request: req,
}
ss := strings.Split(r.Header.Get("Accept"), ",")
if len(ss) == 0 {
Expand Down Expand Up @@ -215,6 +217,7 @@ type ResponseWriter struct {
Logger *zap.Logger
zw zio.WriteCloser
marshaler *zson.MarshalZNGContext
request *Request
written int32
}

Expand Down Expand Up @@ -256,6 +259,10 @@ func (w *ResponseWriter) Respond(status int, body interface{}) bool {
}

func (w *ResponseWriter) Error(err error) {
if err == context.Canceled && err == w.request.Context().Err() {
w.Logger.Info("Request context canceled")
return
}
status, res := errorResponse(err)
if status >= 500 {
w.Logger.Warn("Error", zap.Int("status", status), zap.Error(err))
Expand Down

0 comments on commit 4c7de04

Please sign in to comment.