[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
commit 68e6d08c76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
118 changed files with 813 additions and 591 deletions

View file

@ -102,6 +102,9 @@ func (m *Module) getAssetETag(filePath string, fs http.FileSystem) (string, erro
// package along with the other middlewares
func (m *Module) assetsCacheControlMiddleware(fs http.FileSystem) gin.HandlerFunc {
return func(c *gin.Context) {
// Acquire context from gin request.
ctx := c.Request.Context()
// set this Cache-Control header to instruct clients to validate the response with us
// before each reuse (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control)
c.Header(cacheControlHeader, cacheControlNoCache)
@ -118,7 +121,7 @@ func (m *Module) assetsCacheControlMiddleware(fs http.FileSystem) gin.HandlerFun
// either fetch etag from ttlcache or generate it
eTag, err := m.getAssetETag(assetFilePath, fs)
if err != nil {
log.Errorf("error getting ETag for %s: %s", assetFilePath, err)
log.Errorf(ctx, "error getting ETag for %s: %s", assetFilePath, err)
return
}

View file

@ -34,7 +34,7 @@ func newETagCache() cache.Cache[string, eTagCacheEntry] {
eTagCache := cache.New[string, eTagCacheEntry](0, 1000, 0)
eTagCache.SetTTL(time.Hour, false)
if !eTagCache.Start(time.Minute) {
log.Panic("could not start eTagCache")
log.Panic(nil, "could not start eTagCache")
}
return eTagCache
}

View file

@ -61,14 +61,16 @@ func (m *Module) GetRSSETag(urlPath string, lastModified time.Time, getRSSFeed f
return eTag, nil
}
func extractIfModifiedSince(header string) time.Time {
if header == "" {
func extractIfModifiedSince(r *http.Request) time.Time {
hdr := r.Header.Get(ifModifiedSinceHeader)
if hdr == "" {
return time.Time{}
}
t, err := http.ParseTime(header)
t, err := http.ParseTime(hdr)
if err != nil {
log.Errorf("couldn't parse if-modified-since %s: %s", header, err)
log.Errorf(r.Context(), "couldn't parse if-modified-since %s: %s", hdr, err)
return time.Time{}
}
@ -95,7 +97,7 @@ func (m *Module) rssFeedGETHandler(c *gin.Context) {
}
ifNoneMatch := c.Request.Header.Get(ifNoneMatchHeader)
ifModifiedSince := extractIfModifiedSince(c.Request.Header.Get(ifModifiedSinceHeader))
ifModifiedSince := extractIfModifiedSince(c.Request)
getRssFeed, accountLastPostedPublic, errWithCode := m.processor.AccountGetRSSFeedForUsername(ctx, username)
if errWithCode != nil {

View file

@ -79,7 +79,7 @@ func (m *Module) Route(r router.Router, mi ...gin.HandlerFunc) {
// so that they can use the same cache control middleware.
webAssetsAbsFilePath, err := filepath.Abs(config.GetWebAssetBaseDir())
if err != nil {
log.Panicf("error getting absolute path of assets dir: %s", err)
log.Panicf(nil, "error getting absolute path of assets dir: %s", err)
}
fs := fileSystem{http.Dir(webAssetsAbsFilePath)}
assetsGroup := r.AttachGroup(assetsPathPrefix)