[feature] Add a request ID and include it in logs (#1476)

This adds a lightweight form of tracing to GTS. Each incoming request is
assigned a Request ID which we then pass on and log in all our log
lines. Any function that gets called downstream from an HTTP handler
should now emit a requestID=value pair whenever it logs something.

Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
Daenney
2023-02-17 12:02:29 +01:00
committed by GitHub
parent b5993095fa
commit 68e6d08c76
118 changed files with 813 additions and 591 deletions

View File

@@ -77,7 +77,10 @@ func (m *Module) ServeFile(c *gin.Context) {
return
}
content, errWithCode := m.processor.FileGet(c.Request.Context(), authed, &apimodel.GetContentRequestForm{
// Acquire context from gin request.
ctx := c.Request.Context()
content, errWithCode := m.processor.FileGet(ctx, authed, &apimodel.GetContentRequestForm{
AccountID: accountID,
MediaType: mediaType,
MediaSize: mediaSize,
@@ -101,7 +104,7 @@ func (m *Module) ServeFile(c *gin.Context) {
defer func() {
// Close content when we're done, catch errors.
if err := content.Content.Close(); err != nil {
log.Errorf("ServeFile: error closing readcloser: %s", err)
log.Errorf(ctx, "ServeFile: error closing readcloser: %s", err)
}
}()
@@ -130,15 +133,21 @@ func (m *Module) ServeFile(c *gin.Context) {
return
}
// Set known content-type and serve this file range.
// Set known content-type and serve range.
c.Header("Content-Type", format)
serveFileRange(c.Writer, content.Content, rng, content.ContentLength)
serveFileRange(
c.Writer,
c.Request,
content.Content,
rng,
content.ContentLength,
)
}
// serveFileRange serves the range of a file from a given source reader, without the
// need for implementation of io.Seeker. Instead we read the first 'start' many bytes
// into a discard reader. Code is adapted from https://codeberg.org/gruf/simplehttp.
func serveFileRange(rw http.ResponseWriter, src io.Reader, rng string, size int64) {
func serveFileRange(rw http.ResponseWriter, r *http.Request, src io.Reader, rng string, size int64) {
var i int
if i = strings.IndexByte(rng, '='); i < 0 {
@@ -219,7 +228,7 @@ func serveFileRange(rw http.ResponseWriter, src io.Reader, rng string, size int6
// Dump the first 'start' many bytes into the void...
if _, err := fastcopy.CopyN(io.Discard, src, start); err != nil {
log.Errorf("error reading from source: %v", err)
log.Errorf(r.Context(), "error reading from source: %v", err)
return
}
@@ -239,7 +248,7 @@ func serveFileRange(rw http.ResponseWriter, src io.Reader, rng string, size int6
// Read the "seeked" source reader into destination writer.
if _, err := fastcopy.Copy(rw, src); err != nil {
log.Errorf("error reading from source: %v", err)
log.Errorf(r.Context(), "error reading from source: %v", err)
return
}
}