Skip to content

Commit d69efe0

Browse files
authored
handler: Pass request context to logger when logging (#1237)
* chore: Pass request context to logger when logging This allows the logger passed to the tusd handler to access the request specific context and pull out things like sentry information or trace information. Implements #1236 * chore: bump exp and update log to use Context
1 parent 3cb8a70 commit d69efe0

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/stretchr/testify v1.10.0
3030
github.com/tus/lockfile v1.2.0
3131
github.com/vimeo/go-util v1.4.1
32-
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
32+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
3333
golang.org/x/net v0.33.0
3434
golang.org/x/sync v0.10.0
3535
google.golang.org/api v0.214.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ss
308308
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
309309
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
310310
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
311+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
312+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
311313
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
312314
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
313315
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

pkg/handler/unrouted_handler.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler {
177177
// We also update the write deadline, but makes sure that it is larger than the read deadline, so we
178178
// can still write a response in the case of a read timeout.
179179
if err := c.resC.SetReadDeadline(time.Now().Add(handler.config.NetworkTimeout)); err != nil {
180-
c.log.Warn("NetworkControlError", "error", err)
180+
c.log.WarnContext(c, "NetworkControlError", "error", err)
181181
}
182182
if err := c.resC.SetWriteDeadline(time.Now().Add(2 * handler.config.NetworkTimeout)); err != nil {
183-
c.log.Warn("NetworkControlError", "error", err)
183+
c.log.WarnContext(c, "NetworkControlError", "error", err)
184184
}
185185

186186
// Allow overriding the HTTP method. The reason for this is
@@ -190,7 +190,7 @@ func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler {
190190
r.Method = newMethod
191191
}
192192

193-
c.log.Info("RequestIncoming")
193+
c.log.InfoContext(c, "RequestIncoming")
194194

195195
handler.Metrics.incRequestsTotal(r.Method)
196196

@@ -405,7 +405,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
405405

406406
handler.Metrics.incUploadsCreated()
407407
c.log = c.log.With("id", id)
408-
c.log.Info("UploadCreated", "size", size, "url", url)
408+
c.log.InfoContext(c, "UploadCreated", "size", size, "url", url)
409409

410410
if handler.config.NotifyCreatedUploads {
411411
handler.CreatedUploads <- newHookEvent(c, info)
@@ -572,7 +572,7 @@ func (handler *UnroutedHandler) PostFileV2(w http.ResponseWriter, r *http.Reques
572572

573573
handler.Metrics.incUploadsCreated()
574574
c.log = c.log.With("id", id)
575-
c.log.Info("UploadCreated", "size", info.Size, "url", url)
575+
c.log.InfoContext(c, "UploadCreated", "size", info.Size, "url", url)
576576

577577
if handler.config.NotifyCreatedUploads {
578578
handler.CreatedUploads <- newHookEvent(c, info)
@@ -891,7 +891,7 @@ func (handler *UnroutedHandler) writeChunk(c *httpContext, resp HTTPResponse, up
891891
maxSize = length
892892
}
893893

894-
c.log.Info("ChunkWriteStart", "maxSize", maxSize, "offset", offset)
894+
c.log.InfoContext(c, "ChunkWriteStart", "maxSize", maxSize, "offset", offset)
895895

896896
var bytesWritten int64
897897
var err error
@@ -907,12 +907,12 @@ func (handler *UnroutedHandler) writeChunk(c *httpContext, resp HTTPResponse, up
907907
// Update the read deadline for every successful read operation. This ensures that the request handler
908908
// keeps going while data is transmitted but that dead connections can also time out and be cleaned up.
909909
if err := c.resC.SetReadDeadline(time.Now().Add(handler.config.NetworkTimeout)); err != nil {
910-
c.log.Warn("NetworkTimeoutError", "error", err)
910+
c.log.WarnContext(c, "NetworkTimeoutError", "error", err)
911911
}
912912

913913
// The write deadline is updated accordingly to ensure that we can also write responses.
914914
if err := c.resC.SetWriteDeadline(time.Now().Add(2 * handler.config.NetworkTimeout)); err != nil {
915-
c.log.Warn("NetworkTimeoutError", "error", err)
915+
c.log.WarnContext(c, "NetworkTimeoutError", "error", err)
916916
}
917917
}
918918

@@ -935,7 +935,7 @@ func (handler *UnroutedHandler) writeChunk(c *httpContext, resp HTTPResponse, up
935935
// it in the response, if the store did not also return an error.
936936
bodyErr := c.body.hasError()
937937
if bodyErr != nil {
938-
c.log.Error("BodyReadError", "error", bodyErr.Error())
938+
c.log.ErrorContext(c, "BodyReadError", "error", bodyErr.Error())
939939
if err == nil {
940940
err = bodyErr
941941
}
@@ -947,12 +947,12 @@ func (handler *UnroutedHandler) writeChunk(c *httpContext, resp HTTPResponse, up
947947
if terminateErr := handler.terminateUpload(c, upload, info); terminateErr != nil {
948948
// We only log this error and not show it to the user since this
949949
// termination error is not relevant to the uploading client
950-
c.log.Error("UploadStopTerminateError", "error", terminateErr.Error())
950+
c.log.ErrorContext(c, "UploadStopTerminateError", "error", terminateErr.Error())
951951
}
952952
}
953953
}
954954

955-
c.log.Info("ChunkWriteComplete", "bytesWritten", bytesWritten)
955+
c.log.InfoContext(c, "ChunkWriteComplete", "bytesWritten", bytesWritten)
956956

957957
// Send new offset to client
958958
newOffset := offset + bytesWritten
@@ -1003,7 +1003,7 @@ func (handler *UnroutedHandler) emitFinishEvents(c *httpContext, resp HTTPRespon
10031003
resp = resp.MergeWith(resp2)
10041004
}
10051005

1006-
c.log.Info("UploadFinished", "size", info.Size)
1006+
c.log.InfoContext(c, "UploadFinished", "size", info.Size)
10071007
handler.Metrics.incUploadsFinished()
10081008

10091009
if handler.config.NotifyCompleteUploads {
@@ -1239,7 +1239,7 @@ func (handler *UnroutedHandler) terminateUpload(c *httpContext, upload Upload, i
12391239
handler.TerminatedUploads <- newHookEvent(c, info)
12401240
}
12411241

1242-
c.log.Info("UploadTerminated")
1242+
c.log.InfoContext(c, "UploadTerminated")
12431243
handler.Metrics.incUploadsTerminated()
12441244

12451245
return nil
@@ -1253,7 +1253,7 @@ func (handler *UnroutedHandler) sendError(c *httpContext, err error) {
12531253
var detailedErr Error
12541254

12551255
if !errors.As(err, &detailedErr) {
1256-
c.log.Error("InternalServerError", "message", err.Error())
1256+
c.log.ErrorContext(c, "InternalServerError", "message", err.Error())
12571257
detailedErr = NewError("ERR_INTERNAL_SERVER_ERROR", err.Error(), http.StatusInternalServerError)
12581258
}
12591259

@@ -1271,7 +1271,7 @@ func (handler *UnroutedHandler) sendError(c *httpContext, err error) {
12711271
func (handler *UnroutedHandler) sendResp(c *httpContext, resp HTTPResponse) {
12721272
resp.writeTo(c.res)
12731273

1274-
c.log.Info("ResponseOutgoing", "status", resp.StatusCode, "body", resp.Body)
1274+
c.log.InfoContext(c, "ResponseOutgoing", "status", resp.StatusCode, "body", resp.Body)
12751275
}
12761276

12771277
// Make an absolute URLs to the given upload id. If the base path is absolute
@@ -1432,7 +1432,7 @@ func (handler *UnroutedHandler) lockUpload(c *httpContext, id string) (Lock, err
14321432

14331433
// No need to wrap this in a sync.OnceFunc because c.cancel will be a noop after the first call.
14341434
releaseLock := func() {
1435-
c.log.Info("UploadInterrupted")
1435+
c.log.InfoContext(c, "UploadInterrupted")
14361436
c.cancel(ErrUploadInterrupted)
14371437
}
14381438

0 commit comments

Comments
 (0)