Improve logging

- Actually log useful information at their respective log level.
- Add logs in hot-paths to be able to deep-dive and debug specific
requests (see server/handler.go)
- Remove zerologger and instead use custom logger that doesn't log
JSON (directly inspired by https://codeberg.org/Codeberg/moderation/pulls/7).
- Add more information to existing fields(e.g. the host that the user is
visiting, this was noted by @fnetX).
This commit is contained in:
Gusted 2022-07-24 06:22:52 +02:00
parent 00e8a41c89
commit 1e183d7249
No known key found for this signature in database
GPG key ID: FD821B732837125F
13 changed files with 318 additions and 110 deletions

View file

@ -2,16 +2,16 @@ package server
import (
"bytes"
"fmt"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/valyala/fasthttp"
"codeberg.org/codeberg/pages/html"
"codeberg.org/codeberg/pages/server/cache"
"codeberg.org/codeberg/pages/server/dns"
"codeberg.org/codeberg/pages/server/gitea"
"codeberg.org/codeberg/pages/server/log"
"codeberg.org/codeberg/pages/server/upstream"
"codeberg.org/codeberg/pages/server/utils"
"codeberg.org/codeberg/pages/server/version"
@ -25,7 +25,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
dnsLookupCache, canonicalDomainCache, branchTimestampCache, fileResponseCache cache.SetGetKey,
) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
log := log.With().Str("Handler", string(ctx.Request.Header.RequestURI())).Logger()
logInfo := fmt.Sprintf("[handler=%q]", string(ctx.Request.Header.RequestURI()))
ctx.Response.Header.Set("Server", "CodebergPages/"+version.Version)
@ -83,9 +83,9 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// tryBranch checks if a branch exists and populates the target variables. If canonicalLink is non-empty, it will
// also disallow search indexing and add a Link header to the canonical URL.
tryBranch := func(log zerolog.Logger, repo, branch string, path []string, canonicalLink string) bool {
tryBranch := func(logInfo string, repo, branch string, path []string, canonicalLink string) bool {
if repo == "" {
log.Debug().Msg("tryBranch: repo == ''")
log.Debug("tryBranch%s: repo is empty", logInfo)
return false
}
@ -96,7 +96,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// Check if the branch exists, otherwise treat it as a file path
branchTimestampResult := upstream.GetBranchTimestamp(giteaClient, targetOwner, repo, branch, branchTimestampCache)
if branchTimestampResult == nil {
log.Debug().Msg("tryBranch: branch doesn't exist")
log.Debug("tryBranch%s: branch doesn't exist", logInfo)
return false
}
@ -116,14 +116,14 @@ func Handler(mainDomainSuffix, rawDomain []byte,
)
}
log.Debug().Msg("tryBranch: true")
log.Debug("tryBranch%s: true", logInfo)
return true
}
log.Debug().Msg("preparations")
log.Debug("Preparing %s", logInfo)
if rawDomain != nil && bytes.Equal(trimmedHost, rawDomain) {
// Serve raw content from RawDomain
log.Debug().Msg("raw domain")
log.Debug("Serving raw domain %s", logInfo)
targetOptions.TryIndexPages = false
if targetOptions.ForbiddenMimeTypes == nil {
@ -143,28 +143,28 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// raw.codeberg.org/example/myrepo/@main/index.html
if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") {
log.Debug().Msg("raw domain preparations, now trying with specified branch")
if tryBranch(log,
log.Debug("Preparing raw domain, now trying with specified branch %s", logInfo)
if tryBranch(logInfo,
targetRepo, pathElements[2][1:], pathElements[3:],
giteaRoot+"/"+targetOwner+"/"+targetRepo+"/src/branch/%b/%p",
) {
log.Debug().Msg("tryBranch, now trying upstream 1")
log.Info("tryBranch, now trying upstream 1 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
return
}
log.Debug().Msg("missing branch")
log.Warn("Path missed a branch %s", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
return
}
log.Debug().Msg("raw domain preparations, now trying with default branch")
tryBranch(log,
log.Debug("Preparing raw domain, now trying with default branch %s", logInfo)
tryBranch(logInfo,
targetRepo, "", pathElements[2:],
giteaRoot+"/"+targetOwner+"/"+targetRepo+"/src/branch/%b/%p",
)
log.Debug().Msg("tryBranch, now trying upstream 2")
log.Info("tryBranch, now trying upstream 2 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
@ -172,7 +172,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
} else if bytes.HasSuffix(trimmedHost, mainDomainSuffix) {
// Serve pages from subdomains of MainDomainSuffix
log.Debug().Msg("main domain suffix")
log.Debug("Serve pages from main domain suffix %s", logInfo)
pathElements := strings.Split(string(bytes.Trim(ctx.Request.URI().Path(), "/")), "/")
targetOwner = string(bytes.TrimSuffix(trimmedHost, mainDomainSuffix))
@ -194,16 +194,17 @@ func Handler(mainDomainSuffix, rawDomain []byte,
return
}
log.Debug().Msg("main domain preparations, now trying with specified repo & branch")
if tryBranch(log,
log.Debug("Preparing main domain, now trying with specified repo & branch, %s", logInfo)
if tryBranch(logInfo,
pathElements[0], pathElements[1][1:], pathElements[2:],
"/"+pathElements[0]+"/%p",
) {
log.Debug().Msg("tryBranch, now trying upstream 3")
log.Info("tryBranch, now trying upstream 3 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
} else {
log.Warn("tryBranch%s: upstream 3 failed", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
}
return
@ -212,14 +213,15 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// Check if the first directory is a branch for the "pages" repo
// example.codeberg.page/@main/index.html
if strings.HasPrefix(pathElements[0], "@") {
log.Debug().Msg("main domain preparations, now trying with specified branch")
if tryBranch(log,
log.Debug("Preparing main domain, now trying with specified branch %s", logInfo)
if tryBranch(logInfo,
"pages", pathElements[0][1:], pathElements[1:], "/%p") {
log.Debug().Msg("tryBranch, now trying upstream 4")
log.Info("tryBranch, now trying upstream 4 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
} else {
log.Warn("tryBranch%s: upstream 4 failed", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
}
return
@ -228,10 +230,10 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// Check if the first directory is a repo with a "pages" branch
// example.codeberg.page/myrepo/index.html
// example.codeberg.page/pages/... is not allowed here.
log.Debug().Msg("main domain preparations, now trying with specified repo")
if pathElements[0] != "pages" && tryBranch(log,
log.Debug("main domain preparations, now trying with specified repo %s", logInfo)
if pathElements[0] != "pages" && tryBranch(logInfo,
pathElements[0], "pages", pathElements[1:], "") {
log.Debug().Msg("tryBranch, now trying upstream 5")
log.Info("tryBranch, now trying upstream 5 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
@ -240,10 +242,10 @@ func Handler(mainDomainSuffix, rawDomain []byte,
// Try to use the "pages" repo on its default branch
// example.codeberg.page/index.html
log.Debug().Msg("main domain preparations, now trying with default repo/branch")
if tryBranch(log,
log.Debug("main domain preparations, now trying with default repo/branch %s", logInfo)
if tryBranch(logInfo,
"pages", "", pathElements, "") {
log.Debug().Msg("tryBranch, now trying upstream 6")
log.Info("tryBranch, now trying upstream 6 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
@ -251,6 +253,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
}
// Couldn't find a valid repo/branch
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
return
} else {
@ -272,11 +275,12 @@ func Handler(mainDomainSuffix, rawDomain []byte,
}
// Try to use the given repo on the given branch or the default branch
log.Debug().Msg("custom domain preparations, now trying with details from DNS")
if tryBranch(log,
log.Debug("Preparing custom domain, now trying with details from DNS %s", logInfo)
if tryBranch(logInfo,
targetRepo, targetBranch, pathElements, canonicalLink) {
canonicalDomain, valid := upstream.CheckCanonicalDomain(giteaClient, targetOwner, targetRepo, targetBranch, trimmedHostStr, string(mainDomainSuffix), canonicalDomainCache)
if !valid {
log.Warn("Custom domains, domain from DNS isn't valid/canonical %s", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusMisdirectedRequest)
return
} else if canonicalDomain != trimmedHostStr {
@ -287,17 +291,19 @@ func Handler(mainDomainSuffix, rawDomain []byte,
return
}
log.Warn("Custom domains, targetOwner from DNS is empty", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
return
}
log.Debug().Msg("tryBranch, now trying upstream 7")
log.Info("tryBranch, now trying upstream 7 %s", logInfo)
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, targetBranch, targetPath,
canonicalDomainCache, branchTimestampCache, fileResponseCache)
return
}
log.Warn("Couldn't handle request, none of the options succeed %q", logInfo)
html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
return
}