From 4c7de04295a3addf965e11ea514a0efb0dd2443d Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Tue, 19 Dec 2023 12:30:28 -0500 Subject: [PATCH] Log canceled client requests at info level (#4947) "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" } --- service/request.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/service/request.go b/service/request.go index cc40661844..c9b6d85afe 100644 --- a/service/request.go +++ b/service/request.go @@ -1,6 +1,7 @@ package service import ( + "context" "encoding/json" "errors" "io/fs" @@ -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 { @@ -215,6 +217,7 @@ type ResponseWriter struct { Logger *zap.Logger zw zio.WriteCloser marshaler *zson.MarshalZNGContext + request *Request written int32 } @@ -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))