Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Chandler <wchandler@gitlab.com>2023-08-19 00:03:31 +0300
committerWill Chandler <wchandler@gitlab.com>2023-08-19 00:03:31 +0300
commita85e5fbd6058755774ea9e8a18d31b4a4266887b (patch)
treef967e3d0705831d62ec3dd6fa0c8cf872c75a80c
parent46892cb4fa5b0e86b7e86fbb7acda5effac412d6 (diff)
parent74dda50d3c052e952de07e9ae1775dbfc98fccfc (diff)
Merge branch 'pks-log-drop-with' into 'master'
log: Disallow usage of `logrus.With` functions See merge request https://gitlab.com/gitlab-org/gitaly/-/merge_requests/6240 Merged-by: Will Chandler <wchandler@gitlab.com> Approved-by: Will Chandler <wchandler@gitlab.com> Reviewed-by: karthik nayak <knayak@gitlab.com> Co-authored-by: Patrick Steinhardt <ps@pks.im>
-rw-r--r--.golangci.yml2
-rw-r--r--cmd/gitaly-backup/main.go7
-rw-r--r--cmd/gitaly-blackbox/main.go37
-rw-r--r--cmd/gitaly-git2go/main.go11
-rw-r--r--cmd/gitaly-hooks/hooks.go15
-rw-r--r--cmd/gitaly-ssh/main.go6
-rw-r--r--cmd/gitaly-wrapper/main.go11
-rw-r--r--internal/bootstrap/starter/starter.go4
-rw-r--r--internal/cache/diskcache.go5
-rw-r--r--internal/cache/diskcache_test.go17
-rw-r--r--internal/cache/walker.go9
-rw-r--r--internal/cache/walker_test.go8
-rw-r--r--internal/cli/gitaly/check.go5
-rw-r--r--internal/cli/gitaly/hooks.go4
-rw-r--r--internal/cli/gitaly/serve.go11
-rw-r--r--internal/cli/praefect/serve.go9
-rw-r--r--internal/git/command_factory.go10
-rw-r--r--internal/git/execution_environment.go16
-rw-r--r--internal/git/execution_environment_test.go16
-rw-r--r--internal/gitaly/config/prometheus/config.go6
-rw-r--r--internal/gitaly/server/auth_test.go7
-rw-r--r--internal/gitaly/server/server.go8
-rw-r--r--internal/gitaly/server/server_factory_test.go10
-rw-r--r--internal/gitaly/service/smarthttp/inforefs_test.go2
-rw-r--r--internal/gitaly/storage/counter/counter.go8
-rw-r--r--internal/grpc/middleware/cache/cache.go12
-rw-r--r--internal/grpc/middleware/cache/cache_test.go4
-rw-r--r--internal/grpc/middleware/panichandler/panic_handler.go39
-rw-r--r--internal/log/logger.go21
-rw-r--r--internal/log/logger_test.go2
-rw-r--r--internal/praefect/config/log.go6
-rw-r--r--internal/praefect/middleware/methodtype.go12
-rw-r--r--internal/praefect/server.go8
-rw-r--r--internal/testhelper/configure.go5
-rw-r--r--internal/testhelper/testserver/gitaly.go2
35 files changed, 210 insertions, 145 deletions
diff --git a/.golangci.yml b/.golangci.yml
index d25c2d8f5..db6e94145 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -76,6 +76,8 @@ linters-settings:
msg: Use the logger provided by `log.Default()` or `ctxlogrus.Extract()`.
- p: ^logrus\.New$
msg: Use the logger provided by `log.Default()` or `ctxlogrus.Extract()`.
+ - p: ^logrus\.With(Context|Error|Field|Fields|Time)$
+ msg: Use the logger provided by `log.Default()` or `ctxlogrus.Extract()`.
analyze-types: true
paralleltest:
# Ignore missing calls to `t.Parallel()` and only report incorrect uses of it.
diff --git a/cmd/gitaly-backup/main.go b/cmd/gitaly-backup/main.go
index 287342dec..a37121a3e 100644
--- a/cmd/gitaly-backup/main.go
+++ b/cmd/gitaly-backup/main.go
@@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
+ "fmt"
"io"
"os"
@@ -22,7 +23,11 @@ var subcommands = map[string]subcmd{
}
func main() {
- logger := log.Configure(os.Stdout, "json", "")
+ logger, err := log.Configure(os.Stdout, "json", "")
+ if err != nil {
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
+ }
flags := flag.NewFlagSet("gitaly-backup", flag.ExitOnError)
_ = flags.Parse(os.Args)
diff --git a/cmd/gitaly-blackbox/main.go b/cmd/gitaly-blackbox/main.go
index 9173f4e42..d9d604e0b 100644
--- a/cmd/gitaly-blackbox/main.go
+++ b/cmd/gitaly-blackbox/main.go
@@ -6,7 +6,6 @@ import (
"os"
"github.com/prometheus/client_golang/prometheus"
- "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/blackbox"
"gitlab.com/gitlab-org/gitaly/v16/internal/log"
"gitlab.com/gitlab-org/gitaly/v16/internal/version"
@@ -35,26 +34,36 @@ func main() {
os.Exit(1)
}
- if err := run(flag.Arg(0)); err != nil {
- logrus.WithError(err).Fatal()
- }
-}
-
-func run(configPath string) error {
- configRaw, err := os.ReadFile(configPath)
+ cfg, err := readConfig(flag.Arg(0))
if err != nil {
- return err
+ fmt.Printf("reading configuration: %v", err)
+ os.Exit(1)
}
- config, err := blackbox.ParseConfig(string(configRaw))
+ logger, err := log.Configure(os.Stdout, cfg.Logging.Format, cfg.Logging.Level)
if err != nil {
- return err
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
}
- bb := blackbox.New(config)
+ bb := blackbox.New(cfg)
prometheus.MustRegister(bb)
- log.Configure(os.Stdout, config.Logging.Format, config.Logging.Level)
+ if err := bb.Run(); err != nil {
+ logger.WithError(err).Fatal()
+ }
+}
+
+func readConfig(path string) (blackbox.Config, error) {
+ contents, err := os.ReadFile(path)
+ if err != nil {
+ return blackbox.Config{}, err
+ }
+
+ cfg, err := blackbox.ParseConfig(string(contents))
+ if err != nil {
+ return blackbox.Config{}, err
+ }
- return bb.Run()
+ return cfg, nil
}
diff --git a/cmd/gitaly-git2go/main.go b/cmd/gitaly-git2go/main.go
index 17b4d046c..73e79c99d 100644
--- a/cmd/gitaly-git2go/main.go
+++ b/cmd/gitaly-git2go/main.go
@@ -68,10 +68,15 @@ func main() {
correlationID = correlation.SafeRandomID()
}
- glog.Configure(os.Stderr, logFormat, logLevel)
-
ctx := correlation.ContextWithCorrelation(context.Background(), correlationID)
- logger := glog.Default().WithFields(logrus.Fields{
+
+ logger, err := glog.Configure(os.Stderr, logFormat, logLevel)
+ if err != nil {
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
+ }
+
+ logger = logger.WithFields(logrus.Fields{
"command.name": git2go.BinaryName,
"correlation_id": correlationID,
"enabled_feature_flags": enabledFeatureFlags,
diff --git a/cmd/gitaly-hooks/hooks.go b/cmd/gitaly-hooks/hooks.go
index 20dfba9ce..a19873cf0 100644
--- a/cmd/gitaly-hooks/hooks.go
+++ b/cmd/gitaly-hooks/hooks.go
@@ -76,7 +76,11 @@ func main() {
ctx, finished := labkittracing.ExtractFromEnv(ctx)
defer finished()
- logger := configureLogger(ctx)
+ logger, err := configureLogger(ctx)
+ if err != nil {
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
+ }
if err := run(ctx, os.Args); err != nil {
var hookError hookError
@@ -100,7 +104,7 @@ func main() {
// configureLogger configures the logger used by gitaly-hooks. As both stdout and stderr might be interpreted by Git, we
// need to log to a file instead. If the `log.GitalyLogDirEnvKey` environment variable is set, we thus log to a file
// contained in the directory pointed to by it, otherwise we discard any log messages.
-func configureLogger(ctx context.Context) logrus.FieldLogger {
+func configureLogger(ctx context.Context) (logrus.FieldLogger, error) {
writer := io.Discard
if logDir := os.Getenv(log.GitalyLogDirEnvKey); logDir != "" {
@@ -113,7 +117,12 @@ func configureLogger(ctx context.Context) logrus.FieldLogger {
}
}
- return log.Configure(writer, "text", "info").WithField(correlation.FieldName, correlation.ExtractFromContext(ctx))
+ logger, err := log.Configure(writer, "text", "info")
+ if err != nil {
+ return nil, err
+ }
+
+ return logger.WithField(correlation.FieldName, correlation.ExtractFromContext(ctx)), nil
}
// Both stderr and stdout of gitaly-hooks are streamed back to clients. stdout is processed by client
diff --git a/cmd/gitaly-ssh/main.go b/cmd/gitaly-ssh/main.go
index f5f0fad92..121186378 100644
--- a/cmd/gitaly-ssh/main.go
+++ b/cmd/gitaly-ssh/main.go
@@ -42,7 +42,11 @@ type gitalySSHCommand struct {
// GITALY_USE_SIDECHANNEL=1 if desired
// gitaly-ssh upload-pack <git-garbage-x2>
func main() {
- logger := log.Configure(os.Stderr, "", "info")
+ logger, err := log.Configure(os.Stderr, "", "info")
+ if err != nil {
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
+ }
// < 4 since git throws on 2x garbage here
if n := len(os.Args); n < 4 {
diff --git a/cmd/gitaly-wrapper/main.go b/cmd/gitaly-wrapper/main.go
index dfc101dc1..b83af7bb9 100644
--- a/cmd/gitaly-wrapper/main.go
+++ b/cmd/gitaly-wrapper/main.go
@@ -29,7 +29,12 @@ func main() {
logFormat = "json"
}
- logger := log.Configure(os.Stdout, logFormat, "").WithField("wrapper", os.Getpid())
+ logger, err := log.Configure(os.Stdout, logFormat, "")
+ if err != nil {
+ fmt.Printf("configuring logger failed: %v", err)
+ os.Exit(1)
+ }
+ logger = logger.WithField("wrapper", os.Getpid())
if len(os.Args) < 2 {
logger.Fatalf("usage: %s forking_binary [args]", os.Args[0])
@@ -102,7 +107,7 @@ func findProcess(pidFilePath string) (*os.Process, error) {
return nil, nil
}
-func spawnProcess(logger *logrus.Entry, bin string, args []string) (*os.Process, error) {
+func spawnProcess(logger logrus.FieldLogger, bin string, args []string) (*os.Process, error) {
cmd := exec.Command(bin, args...)
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=true", bootstrap.EnvUpgradesEnabled))
@@ -128,7 +133,7 @@ func isRuntimeSig(s os.Signal) bool {
return s == unix.SIGURG
}
-func forwardSignals(gitaly *os.Process, log *logrus.Entry) {
+func forwardSignals(gitaly *os.Process, log logrus.FieldLogger) {
sigs := make(chan os.Signal, 1)
go func() {
for sig := range sigs {
diff --git a/internal/bootstrap/starter/starter.go b/internal/bootstrap/starter/starter.go
index b2e7f586f..ead1a998a 100644
--- a/internal/bootstrap/starter/starter.go
+++ b/internal/bootstrap/starter/starter.go
@@ -110,7 +110,7 @@ type Server interface {
}
// New creates a new bootstrap.Starter from a config and a GracefulStoppableServer
-func New(cfg Config, server Server) bootstrap.Starter {
+func New(cfg Config, server Server, logger logrus.FieldLogger) bootstrap.Starter {
return func(listenWithHandover bootstrap.ListenFunc, errCh chan<- error, connTotal *prometheus.CounterVec) error {
listen := listenWithHandover
if !cfg.HandoverOnUpgrade {
@@ -128,7 +128,7 @@ func New(cfg Config, server Server) bootstrap.Starter {
return err
}
- logrus.WithField("address", cfg.Addr).Infof("listening at %s address", cfg.Name)
+ logger.WithField("address", cfg.Addr).Infof("listening at %s address", cfg.Name)
l = wrap(cfg.Name, l, connTotal)
go func() {
diff --git a/internal/cache/diskcache.go b/internal/cache/diskcache.go
index 60c3d59fd..84e75f49f 100644
--- a/internal/cache/diskcache.go
+++ b/internal/cache/diskcache.go
@@ -10,6 +10,7 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/prometheus/client_golang/prometheus"
+ "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
@@ -75,6 +76,7 @@ func withDisabledWalker() Option {
// DiskCache stores and retrieves byte streams for repository related RPCs
type DiskCache struct {
+ logger logrus.FieldLogger
locator storage.Locator
storages []config.Storage
keyer leaseKeyer
@@ -98,13 +100,14 @@ type DiskCache struct {
}
// New will create a new DiskCache with the given Keyer.
-func New(cfg config.Cfg, locator storage.Locator, opts ...Option) *DiskCache {
+func New(cfg config.Cfg, locator storage.Locator, logger logrus.FieldLogger, opts ...Option) *DiskCache {
var cacheConfig cacheConfig
for _, opt := range opts {
opt(&cacheConfig)
}
cache := &DiskCache{
+ logger: logger,
locator: locator,
storages: cfg.Storages,
af: activeFiles{
diff --git a/internal/cache/diskcache_test.go b/internal/cache/diskcache_test.go
index 2db13ecde..3c97d9eb6 100644
--- a/internal/cache/diskcache_test.go
+++ b/internal/cache/diskcache_test.go
@@ -58,6 +58,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
})
gittest.WriteCommit(t, cfg, repoPath2, gittest.WithMessage("two"), gittest.WithBranch("master"))
+ logger := testhelper.NewDiscardingLogEntry(t)
locator := config.NewLocator(cfg)
req1 := &gitalypb.InfoRefsRequest{
@@ -68,20 +69,20 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
}
t.Run("empty cache", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
expectGetMiss(ctx, cache, req1)
expectGetMiss(ctx, cache, req2)
})
t.Run("store and retrieve", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
storeAndRetrieve(ctx, cache, req1, "content-1")
storeAndRetrieve(ctx, cache, req2, "content-2")
})
t.Run("invalidation", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
storeAndRetrieve(ctx, cache, req1, "content-1")
storeAndRetrieve(ctx, cache, req2, "content-2")
@@ -93,7 +94,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
})
t.Run("overwrite existing entry", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
storeAndRetrieve(ctx, cache, req1, "content-1")
@@ -102,7 +103,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
})
t.Run("feature flags affect caching", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
ctxWithFF := featureflag.IncomingCtxWithFeatureFlag(ctx, featureflag.FeatureFlag{
Name: "meow",
@@ -118,7 +119,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
})
t.Run("critical section", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
storeAndRetrieve(ctx, cache, req2, "unrelated")
@@ -146,7 +147,7 @@ func TestStreamDBNaiveKeyer(t *testing.T) {
})
t.Run("nonexisteng repository", func(t *testing.T) {
- cache := New(cfg, locator)
+ cache := New(cfg, locator, logger)
nonexistentRepo := &gitalypb.Repository{
StorageName: repo1.StorageName,
@@ -166,7 +167,7 @@ func TestLoserCount(t *testing.T) {
cfg := cfgBuilder.Build(t)
locator := config.NewLocator(cfg)
- cache := New(cfg, locator)
+ cache := New(cfg, locator, testhelper.NewDiscardingLogEntry(t))
req := &gitalypb.InfoRefsRequest{
Repository: &gitalypb.Repository{
diff --git a/internal/cache/walker.go b/internal/cache/walker.go
index fdeca47fe..b9f15b8b4 100644
--- a/internal/cache/walker.go
+++ b/internal/cache/walker.go
@@ -13,17 +13,14 @@ import (
"path/filepath"
"time"
- "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
- "gitlab.com/gitlab-org/gitaly/v16/internal/log"
)
func (c *DiskCache) logWalkErr(err error, path, msg string) {
c.walkerErrorTotal.Inc()
- log.Default().
- WithField("path", path).
+ c.logger.WithField("path", path).
WithError(err).
Warn(msg)
}
@@ -106,7 +103,7 @@ func (c *DiskCache) cleanWalk(path string) error {
const cleanWalkFrequency = 10 * time.Minute
func (c *DiskCache) walkLoop(walkPath string) {
- logger := logrus.WithField("path", walkPath)
+ logger := c.logger.WithField("path", walkPath)
logger.Infof("Starting file walker for %s", walkPath)
walkTick := time.NewTicker(cleanWalkFrequency)
@@ -149,7 +146,7 @@ func (c *DiskCache) moveAndClear(storage config.Storage) error {
return nil
}
- logger := logrus.WithField("storage", storage.Name)
+ logger := c.logger.WithField("storage", storage.Name)
logger.Info("clearing disk cache object folder")
tempPath, err := c.locator.TempDir(storage.Name)
diff --git a/internal/cache/walker_test.go b/internal/cache/walker_test.go
index c6a6fce72..7b71ca73d 100644
--- a/internal/cache/walker_test.go
+++ b/internal/cache/walker_test.go
@@ -52,7 +52,7 @@ func TestDiskCacheObjectWalker(t *testing.T) {
}
}
- cache := New(cfg, locator, withDisabledMoveAndClear())
+ cache := New(cfg, locator, testhelper.NewDiscardingLogEntry(t), withDisabledMoveAndClear())
require.NoError(t, cache.StartWalkers())
defer cache.StopWalkers()
@@ -81,7 +81,7 @@ func TestDiskCacheInitialClear(t *testing.T) {
require.NoError(t, os.MkdirAll(filepath.Dir(canary), perm.SharedDir))
require.NoError(t, os.WriteFile(canary, []byte("chirp chirp"), perm.PublicFile))
- cache := New(cfg, locator, withDisabledWalker())
+ cache := New(cfg, locator, testhelper.NewDiscardingLogEntry(t), withDisabledWalker())
require.NoError(t, cache.StartWalkers())
defer cache.StopWalkers()
@@ -91,7 +91,7 @@ func TestDiskCacheInitialClear(t *testing.T) {
func TestCleanWalkDirNotExists(t *testing.T) {
cfg := testcfg.Build(t)
- cache := New(cfg, config.NewLocator(cfg))
+ cache := New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t))
err := cache.cleanWalk("/path/that/does/not/exist")
assert.NoError(t, err, "cleanWalk returned an error for a non existing directory")
@@ -124,7 +124,7 @@ func TestCleanWalkEmptyDirs(t *testing.T) {
}
cfg := testcfg.Build(t)
- cache := New(cfg, config.NewLocator(cfg))
+ cache := New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t))
require.NoError(t, cache.cleanWalk(tmp))
diff --git a/internal/cli/gitaly/check.go b/internal/cli/gitaly/check.go
index c29df5a72..7e7153030 100644
--- a/internal/cli/gitaly/check.go
+++ b/internal/cli/gitaly/check.go
@@ -43,7 +43,10 @@ func checkAction(ctx *cli.Context) error {
return fmt.Errorf("loading configuration %q: %w", configPath, err)
}
- logger := log.Configure(os.Stderr, "text", "error")
+ logger, err := log.Configure(os.Stderr, "text", "error")
+ if err != nil {
+ return cli.Exit(fmt.Errorf("configuring logger failed: %w", err), 1)
+ }
fmt.Fprint(ctx.App.Writer, "Checking GitLab API access: ")
info, err := checkAPI(cfg, logger)
diff --git a/internal/cli/gitaly/hooks.go b/internal/cli/gitaly/hooks.go
index db4675eed..e73ff0178 100644
--- a/internal/cli/gitaly/hooks.go
+++ b/internal/cli/gitaly/hooks.go
@@ -73,7 +73,9 @@ func setHooksAction(ctx *cli.Context) error {
return fmt.Errorf("load config: %w", err)
}
- gitalylog.Configure(os.Stdout, cfg.Logging.Format, cfg.Logging.Level)
+ if _, err := gitalylog.Configure(os.Stdout, cfg.Logging.Format, cfg.Logging.Level); err != nil {
+ return fmt.Errorf("configuring loggers: %w", err)
+ }
storage := ctx.String(flagStorage)
if storage == "" {
diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.go
index bc6356433..813014b71 100644
--- a/internal/cli/gitaly/serve.go
+++ b/internal/cli/gitaly/serve.go
@@ -120,10 +120,13 @@ func configure(configPath string) (config.Cfg, logrus.FieldLogger, error) {
"UpdateRemoteMirror",
)
- logger := log.Configure(os.Stdout, cfg.Logging.Format, cfg.Logging.Level, urlSanitizer)
+ logger, err := log.Configure(os.Stdout, cfg.Logging.Format, cfg.Logging.Level, urlSanitizer)
+ if err != nil {
+ return config.Cfg{}, nil, fmt.Errorf("configuring logger failed: %w", err)
+ }
sentry.ConfigureSentry(logger, version.GetVersion(), sentry.Config(cfg.Logging.Sentry))
- cfg.Prometheus.Configure()
+ cfg.Prometheus.Configure(logger)
labkittracing.Initialize(labkittracing.WithServiceName("gitaly"))
preloadLicenseDatabase(logger)
@@ -265,7 +268,7 @@ func run(cfg config.Cfg, logger logrus.FieldLogger) error {
defer catfileCache.Stop()
prometheus.MustRegister(catfileCache)
- diskCache := cache.New(cfg, locator)
+ diskCache := cache.New(cfg, locator, logger)
prometheus.MustRegister(diskCache)
if err := diskCache.StartWalkers(); err != nil {
return fmt.Errorf("disk cache walkers: %w", err)
@@ -374,7 +377,7 @@ func run(cfg config.Cfg, logger logrus.FieldLogger) error {
BackupSink: backupSink,
BackupLocator: backupLocator,
})
- b.RegisterStarter(starter.New(c, srv))
+ b.RegisterStarter(starter.New(c, srv, logger))
}
if addr := cfg.PrometheusListenAddr; addr != "" {
diff --git a/internal/cli/praefect/serve.go b/internal/cli/praefect/serve.go
index 7bbc6cdc8..21394b3a5 100644
--- a/internal/cli/praefect/serve.go
+++ b/internal/cli/praefect/serve.go
@@ -86,7 +86,10 @@ func run(appName string, logger *logrus.Entry, configPath string) error {
return err
}
- conf.ConfigureLogger()
+ if _, err := conf.ConfigureLogger(); err != nil {
+ return fmt.Errorf("configuring logger: %w", err)
+ }
+
configure(logger, appName, conf)
starterConfigs, err := getStarterConfigs(conf)
@@ -150,7 +153,7 @@ func configure(logger logrus.FieldLogger, appName string, conf config.Config) {
tracing.Initialize(tracing.WithServiceName(appName))
if conf.PrometheusListenAddr != "" {
- conf.Prometheus.Configure()
+ conf.Prometheus.Configure(logger)
}
sentry.ConfigureSentry(logger, version.GetVersion(), conf.Sentry)
@@ -440,7 +443,7 @@ func server(
}
defer srv.Stop()
- b.RegisterStarter(starter.New(cfg, srv))
+ b.RegisterStarter(starter.New(cfg, srv, logger))
}
if conf.PrometheusListenAddr != "" {
diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go
index 3edf54598..56b0e33d9 100644
--- a/internal/git/command_factory.go
+++ b/internal/git/command_factory.go
@@ -163,7 +163,7 @@ func NewExecCommandFactory(cfg config.Cfg, logger logrus.FieldLogger, opts ...Ex
}
cleanups = append(cleanups, cleanup)
- execEnvs, cleanup, err := setupGitExecutionEnvironments(cfg, factoryCfg)
+ execEnvs, cleanup, err := setupGitExecutionEnvironments(cfg, factoryCfg, logger)
if err != nil {
return nil, nil, fmt.Errorf("setting up Git execution environment: %w", err)
}
@@ -197,7 +197,7 @@ func NewExecCommandFactory(cfg config.Cfg, logger logrus.FieldLogger, opts ...Ex
// setupGitExecutionEnvironments assembles a Git execution environment that can be used to run Git
// commands. It warns if no path was specified in the configuration.
-func setupGitExecutionEnvironments(cfg config.Cfg, factoryCfg execCommandFactoryConfig) ([]ExecutionEnvironment, func(), error) {
+func setupGitExecutionEnvironments(cfg config.Cfg, factoryCfg execCommandFactoryConfig, logger logrus.FieldLogger) ([]ExecutionEnvironment, func(), error) {
sharedEnvironment := []string{
// Force English locale for consistency on output messages and to help us debug in
// case we get bug reports from customers whose system-locale would be different.
@@ -252,7 +252,7 @@ func setupGitExecutionEnvironments(cfg config.Cfg, factoryCfg execCommandFactory
}
execEnv.EnvironmentVariables = append(execEnv.EnvironmentVariables, sharedEnvironment...)
- logrus.WithFields(logrus.Fields{
+ logger.WithFields(logrus.Fields{
"resolvedPath": execEnv.BinaryPath,
}).Warn("Git has not been properly configured, falling back to Git found on PATH")
@@ -261,7 +261,9 @@ func setupGitExecutionEnvironments(cfg config.Cfg, factoryCfg execCommandFactory
return execEnvs, func() {
for _, execEnv := range execEnvs {
- execEnv.Cleanup()
+ if err := execEnv.Cleanup(); err != nil {
+ logger.WithError(err).Error("execution environment cleanup failed")
+ }
}
}, nil
}
diff --git a/internal/git/execution_environment.go b/internal/git/execution_environment.go
index 378e0deea..e613020ff 100644
--- a/internal/git/execution_environment.go
+++ b/internal/git/execution_environment.go
@@ -8,7 +8,6 @@ import (
"os/exec"
"path/filepath"
- "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/featureflag"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"golang.org/x/sys/unix"
@@ -47,14 +46,15 @@ type ExecutionEnvironment struct {
EnvironmentVariables []string
isEnabled func(context.Context) bool
- cleanup func()
+ cleanup func() error
}
// Cleanup cleans up any state set up by this ExecutionEnvironment.
-func (e ExecutionEnvironment) Cleanup() {
+func (e ExecutionEnvironment) Cleanup() error {
if e.cleanup != nil {
- e.cleanup()
+ return e.cleanup()
}
+ return nil
}
// IsEnabled checks whether the ExecutionEnvironment is enabled in the given context. An execution
@@ -180,14 +180,16 @@ func (c BundledGitEnvironmentConstructor) Construct(cfg config.Cfg) (_ Execution
return ExecutionEnvironment{}, fmt.Errorf("creating Git exec path: %w", err)
}
- cleanup := func() {
+ cleanup := func() error {
if err := os.RemoveAll(gitExecPath); err != nil {
- logrus.WithError(err).Error("cleanup of Git execution environment failed")
+ return fmt.Errorf("removal of execution path: %w", err)
}
+
+ return nil
}
defer func() {
if returnedErr != nil {
- cleanup()
+ _ = cleanup()
}
}()
diff --git a/internal/git/execution_environment_test.go b/internal/git/execution_environment_test.go
index ebe0002f8..0be3c0df0 100644
--- a/internal/git/execution_environment_test.go
+++ b/internal/git/execution_environment_test.go
@@ -31,7 +31,7 @@ func TestDistributedGitEnvironmentConstructor(t *testing.T) {
},
})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, "/foo/bar", execEnv.BinaryPath)
require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
@@ -42,7 +42,7 @@ func TestDistributedGitEnvironmentConstructor(t *testing.T) {
execEnv, err := constructor.Construct(config.Cfg{})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, "/foo/bar", execEnv.BinaryPath)
require.Equal(t, []string{
@@ -59,7 +59,7 @@ func TestDistributedGitEnvironmentConstructor(t *testing.T) {
},
})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, "config", execEnv.BinaryPath)
require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
@@ -114,7 +114,7 @@ func TestBundledGitEnvironmentConstructor(t *testing.T) {
},
})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
// We create a temporary directory where the symlinks are created, and we cannot
// predict its exact path.
@@ -144,7 +144,7 @@ func TestBundledGitEnvironmentConstructor(t *testing.T) {
execPrefix := filepath.Dir(execEnv.BinaryPath)
require.DirExists(t, execPrefix)
- execEnv.Cleanup()
+ require.NoError(t, execEnv.Cleanup())
require.NoDirExists(t, execPrefix)
})
@@ -183,7 +183,7 @@ func TestBundledGitEnvironmentConstructor(t *testing.T) {
BinDir: testhelper.TempDir(t),
})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, "git", filepath.Base(execEnv.BinaryPath))
execPrefix := filepath.Dir(execEnv.BinaryPath)
@@ -213,7 +213,7 @@ func TestBundledGitEnvironmentConstructor(t *testing.T) {
},
})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, "git", filepath.Base(execEnv.BinaryPath))
execPrefix := filepath.Dir(execEnv.BinaryPath)
@@ -248,7 +248,7 @@ func TestFallbackGitEnvironmentConstructor(t *testing.T) {
execEnv, err := constructor.Construct(config.Cfg{})
require.NoError(t, err)
- defer execEnv.Cleanup()
+ defer func() { require.NoError(t, execEnv.Cleanup()) }()
require.Equal(t, gitPath, execEnv.BinaryPath)
require.Equal(t, []string(nil), execEnv.EnvironmentVariables)
diff --git a/internal/gitaly/config/prometheus/config.go b/internal/gitaly/config/prometheus/config.go
index b6a189138..f3b8f9287 100644
--- a/internal/gitaly/config/prometheus/config.go
+++ b/internal/gitaly/config/prometheus/config.go
@@ -7,7 +7,7 @@ import (
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
- log "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/errors/cfgerror"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/duration"
)
@@ -30,12 +30,12 @@ func DefaultConfig() Config {
}
// Configure configures latency buckets for prometheus timing histograms
-func (c *Config) Configure() {
+func (c *Config) Configure(logger logrus.FieldLogger) {
if len(c.GRPCLatencyBuckets) == 0 {
return
}
- log.WithField("latencies", c.GRPCLatencyBuckets).Info("grpc prometheus histograms enabled")
+ logger.WithField("latencies", c.GRPCLatencyBuckets).Info("grpc prometheus histograms enabled")
grpcprometheus.EnableHandlingTimeHistogram(func(histogramOpts *prometheus.HistogramOpts) {
histogramOpts.Buckets = c.GRPCLatencyBuckets
diff --git a/internal/gitaly/server/auth_test.go b/internal/gitaly/server/auth_test.go
index 6333b5942..385a494d2 100644
--- a/internal/gitaly/server/auth_test.go
+++ b/internal/gitaly/server/auth_test.go
@@ -189,6 +189,7 @@ func newOperationClient(t *testing.T, token, serverSocketPath string) (gitalypb.
func runServer(t *testing.T, cfg config.Cfg) string {
t.Helper()
+ logger := testhelper.NewDiscardingLogEntry(t)
registry := backchannel.NewRegistry()
conns := client.NewPool()
t.Cleanup(func() { conns.Close() })
@@ -200,11 +201,11 @@ func runServer(t *testing.T, cfg config.Cfg) string {
))
catfileCache := catfile.NewCache(cfg)
t.Cleanup(catfileCache.Stop)
- diskCache := cache.New(cfg, locator)
+ diskCache := cache.New(cfg, locator, logger)
limitHandler := limithandler.New(cfg, limithandler.LimitConcurrencyByRepo, limithandler.WithConcurrencyLimiters)
updaterWithHooks := updateref.NewUpdaterWithHooks(cfg, locator, hookManager, gitCmdFactory, catfileCache)
- srv, err := NewGitalyServerFactory(cfg, testhelper.NewDiscardingLogEntry(t), registry, diskCache, []*limithandler.LimiterMiddleware{limitHandler}).New(false)
+ srv, err := NewGitalyServerFactory(cfg, logger, registry, diskCache, []*limithandler.LimiterMiddleware{limitHandler}).New(false)
require.NoError(t, err)
setup.RegisterAll(srv, &service.Dependencies{
@@ -243,7 +244,7 @@ func runSecureServer(t *testing.T, cfg config.Cfg) string {
cfg,
testhelper.NewDiscardingLogEntry(t),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t)),
[]*limithandler.LimiterMiddleware{limithandler.New(cfg, limithandler.LimitConcurrencyByRepo, limithandler.WithConcurrencyLimiters)},
).New(true)
require.NoError(t, err)
diff --git a/internal/gitaly/server/server.go b/internal/gitaly/server/server.go
index 2b1127d97..b30ac1c83 100644
--- a/internal/gitaly/server/server.go
+++ b/internal/gitaly/server/server.go
@@ -145,18 +145,18 @@ func (s *GitalyServerFactory) New(secure bool, opts ...Option) (*grpc.Server, er
streamServerInterceptors = append(streamServerInterceptors,
grpctracing.StreamServerTracingInterceptor(),
- cache.StreamInvalidator(s.cacheInvalidator, protoregistry.GitalyProtoPreregistered),
+ cache.StreamInvalidator(s.cacheInvalidator, protoregistry.GitalyProtoPreregistered, s.logger),
// Panic handler should remain last so that application panics will be
// converted to errors and logged
- panichandler.StreamPanicHandler,
+ panichandler.StreamPanicHandler(s.logger),
)
unaryServerInterceptors = append(unaryServerInterceptors,
grpctracing.UnaryServerTracingInterceptor(),
- cache.UnaryInvalidator(s.cacheInvalidator, protoregistry.GitalyProtoPreregistered),
+ cache.UnaryInvalidator(s.cacheInvalidator, protoregistry.GitalyProtoPreregistered, s.logger),
// Panic handler should remain last so that application panics will be
// converted to errors and logged
- panichandler.UnaryPanicHandler,
+ panichandler.UnaryPanicHandler(s.logger),
)
streamServerInterceptors = append(streamServerInterceptors, cfg.streamInterceptors...)
diff --git a/internal/gitaly/server/server_factory_test.go b/internal/gitaly/server/server_factory_test.go
index fba2a4613..fa77ff93e 100644
--- a/internal/gitaly/server/server_factory_test.go
+++ b/internal/gitaly/server/server_factory_test.go
@@ -83,7 +83,7 @@ func TestGitalyServerFactory(t *testing.T) {
cfg,
testhelper.NewDiscardingLogEntry(t),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t)),
nil,
)
t.Cleanup(sf.Stop)
@@ -103,7 +103,7 @@ func TestGitalyServerFactory(t *testing.T) {
cfg,
testhelper.NewDiscardingLogEntry(t),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t)),
nil,
)
t.Cleanup(sf.Stop)
@@ -117,7 +117,7 @@ func TestGitalyServerFactory(t *testing.T) {
cfg,
testhelper.NewDiscardingLogEntry(t),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t)),
nil,
)
t.Cleanup(sf.Stop)
@@ -147,7 +147,7 @@ func TestGitalyServerFactory(t *testing.T) {
cfg,
logger.WithContext(ctx),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), logger),
nil,
)
t.Cleanup(sf.Stop)
@@ -182,7 +182,7 @@ func TestGitalyServerFactory_closeOrder(t *testing.T) {
cfg,
testhelper.NewDiscardingLogEntry(t),
backchannel.NewRegistry(),
- cache.New(cfg, config.NewLocator(cfg)),
+ cache.New(cfg, config.NewLocator(cfg), testhelper.NewDiscardingLogEntry(t)),
nil,
)
defer sf.Stop()
diff --git a/internal/gitaly/service/smarthttp/inforefs_test.go b/internal/gitaly/service/smarthttp/inforefs_test.go
index 18caca364..b8d8d3794 100644
--- a/internal/gitaly/service/smarthttp/inforefs_test.go
+++ b/internal/gitaly/service/smarthttp/inforefs_test.go
@@ -398,7 +398,7 @@ func TestInfoRefsUploadPack_cache(t *testing.T) {
cfg := testcfg.Build(t)
locator := config.NewLocator(cfg)
- cache := cache.New(cfg, locator)
+ cache := cache.New(cfg, locator, testhelper.NewDiscardingLogEntry(t))
streamer := mockStreamer{
Streamer: cache,
diff --git a/internal/gitaly/storage/counter/counter.go b/internal/gitaly/storage/counter/counter.go
index 86accfeb6..79e36e6ea 100644
--- a/internal/gitaly/storage/counter/counter.go
+++ b/internal/gitaly/storage/counter/counter.go
@@ -10,7 +10,7 @@ import (
"time"
"github.com/prometheus/client_golang/prometheus"
- log "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
@@ -61,7 +61,7 @@ func (c *RepositoryCounter) Collect(metrics chan<- prometheus.Metric) {
func (c *RepositoryCounter) StartCountingRepositories(
ctx context.Context,
locator storage.Locator,
- logger log.FieldLogger,
+ logger logrus.FieldLogger,
) {
dontpanic.Go(func() {
c.countRepositories(ctx, locator, logger)
@@ -71,7 +71,7 @@ func (c *RepositoryCounter) StartCountingRepositories(
func (c *RepositoryCounter) countRepositories(
ctx context.Context,
locator storage.Locator,
- logger log.FieldLogger,
+ logger logrus.FieldLogger,
) {
defer func() {
c.suppressMetric.Store(false)
@@ -108,7 +108,7 @@ func (c *RepositoryCounter) countRepositories(
}
if err := walk.FindRepositories(ctx, locator, name, incrementPrefix); err != nil {
- log.WithError(err).Errorf("failed to count repositories in path %q", storPath)
+ logger.WithError(err).Errorf("failed to count repositories in path %q", storPath)
}
for prefix, ct := range paths {
diff --git a/internal/grpc/middleware/cache/cache.go b/internal/grpc/middleware/cache/cache.go
index 7ef68d66d..ad06647a7 100644
--- a/internal/grpc/middleware/cache/cache.go
+++ b/internal/grpc/middleware/cache/cache.go
@@ -13,10 +13,10 @@ import (
"google.golang.org/protobuf/proto"
)
-func methodErrLogger(method string) func(error) {
+func methodErrLogger(logger logrus.FieldLogger, method string) func(error) {
return func(err error) {
countMethodErr(method)
- logrus.WithField("full_method_name", method).Error(err)
+ logger.WithField("full_method_name", method).Error(err)
}
}
@@ -42,13 +42,13 @@ func shouldInvalidate(mi protoregistry.MethodInfo) bool {
// StreamInvalidator will invalidate any mutating RPC that targets a
// repository in a gRPC stream based RPC
-func StreamInvalidator(ci diskcache.Invalidator, reg *protoregistry.Registry) grpc.StreamServerInterceptor {
+func StreamInvalidator(ci diskcache.Invalidator, reg *protoregistry.Registry, logger logrus.FieldLogger) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if shouldIgnore(reg, info.FullMethod) {
return handler(srv, ss)
}
- errLogger := methodErrLogger(info.FullMethod)
+ errLogger := methodErrLogger(logger, info.FullMethod)
mInfo, err := reg.LookupMethod(info.FullMethod)
countRPCType(mInfo)
@@ -69,13 +69,13 @@ func StreamInvalidator(ci diskcache.Invalidator, reg *protoregistry.Registry) gr
// UnaryInvalidator will invalidate any mutating RPC that targets a
// repository in a gRPC unary RPC
-func UnaryInvalidator(ci diskcache.Invalidator, reg *protoregistry.Registry) grpc.UnaryServerInterceptor {
+func UnaryInvalidator(ci diskcache.Invalidator, reg *protoregistry.Registry, logger logrus.FieldLogger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
if shouldIgnore(reg, info.FullMethod) {
return handler(ctx, req)
}
- errLogger := methodErrLogger(info.FullMethod)
+ errLogger := methodErrLogger(logger, info.FullMethod)
mInfo, err := reg.LookupMethod(info.FullMethod)
countRPCType(mInfo)
diff --git a/internal/grpc/middleware/cache/cache_test.go b/internal/grpc/middleware/cache/cache_test.go
index 8b521b8ef..e9b924d09 100644
--- a/internal/grpc/middleware/cache/cache_test.go
+++ b/internal/grpc/middleware/cache/cache_test.go
@@ -111,8 +111,8 @@ func TestInvalidators(t *testing.T) {
mockCache := newMockCache()
server := grpc.NewServer(
- grpc.StreamInterceptor(StreamInvalidator(mockCache, protoregistry.GitalyProtoPreregistered)),
- grpc.UnaryInterceptor(UnaryInvalidator(mockCache, protoregistry.GitalyProtoPreregistered)),
+ grpc.StreamInterceptor(StreamInvalidator(mockCache, protoregistry.GitalyProtoPreregistered, testhelper.NewDiscardingLogEntry(t))),
+ grpc.UnaryInterceptor(UnaryInvalidator(mockCache, protoregistry.GitalyProtoPreregistered, testhelper.NewDiscardingLogEntry(t))),
)
service := &testService{
diff --git a/internal/grpc/middleware/panichandler/panic_handler.go b/internal/grpc/middleware/panichandler/panic_handler.go
index e91269d5f..089dfcc79 100644
--- a/internal/grpc/middleware/panichandler/panic_handler.go
+++ b/internal/grpc/middleware/panichandler/panic_handler.go
@@ -3,17 +3,12 @@ package panichandler
import (
"context"
- log "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
-var (
- _ grpc.UnaryServerInterceptor = UnaryPanicHandler
- _ grpc.StreamServerInterceptor = StreamPanicHandler
-)
-
// PanicHandler is a handler that will be called on a grpc panic
type PanicHandler func(methodName string, error interface{})
@@ -21,22 +16,26 @@ func toPanicError(grpcMethodName string, r interface{}) error {
return status.Errorf(codes.Internal, "panic: %v", r)
}
-// UnaryPanicHandler handles request-response panics
-func UnaryPanicHandler(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
- defer handleCrash(info.FullMethod, func(grpcMethodName string, r interface{}) {
- err = toPanicError(grpcMethodName, r)
- })
+// UnaryPanicHandler creates a new unary server interceptor that handles panics.
+func UnaryPanicHandler(logger logrus.FieldLogger) grpc.UnaryServerInterceptor {
+ return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
+ defer handleCrash(logger, info.FullMethod, func(grpcMethodName string, r interface{}) {
+ err = toPanicError(grpcMethodName, r)
+ })
- return handler(ctx, req)
+ return handler(ctx, req)
+ }
}
-// StreamPanicHandler handles stream panics
-func StreamPanicHandler(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
- defer handleCrash(info.FullMethod, func(grpcMethodName string, r interface{}) {
- err = toPanicError(grpcMethodName, r)
- })
+// StreamPanicHandler creates a new stream server interceptor that handles panics.
+func StreamPanicHandler(logger logrus.FieldLogger) grpc.StreamServerInterceptor {
+ return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
+ defer handleCrash(logger, info.FullMethod, func(grpcMethodName string, r interface{}) {
+ err = toPanicError(grpcMethodName, r)
+ })
- return handler(srv, stream)
+ return handler(srv, stream)
+ }
}
var additionalHandlers []PanicHandler
@@ -46,9 +45,9 @@ func InstallPanicHandler(handler PanicHandler) {
additionalHandlers = append(additionalHandlers, handler)
}
-func handleCrash(grpcMethodName string, handler PanicHandler) {
+func handleCrash(logger logrus.FieldLogger, grpcMethodName string, handler PanicHandler) {
if r := recover(); r != nil {
- log.WithFields(log.Fields{
+ logger.WithFields(logrus.Fields{
"error": r,
"method": grpcMethodName,
}).Error("grpc panic")
diff --git a/internal/log/logger.go b/internal/log/logger.go
index ee7adb7cb..ef8604b3f 100644
--- a/internal/log/logger.go
+++ b/internal/log/logger.go
@@ -1,6 +1,7 @@
package log
import (
+ "fmt"
"io"
"os"
@@ -58,18 +59,24 @@ type Config struct {
// Configure configures the default and gRPC loggers. The gRPC logger's log level will be mapped in order to decrease
// its default verbosity. Returns the configured default logger that would also be returned by `Default()`.
-func Configure(out io.Writer, format string, level string, hooks ...logrus.Hook) logrus.FieldLogger {
- configure(defaultLogger, out, format, level, hooks...)
+func Configure(out io.Writer, format string, level string, hooks ...logrus.Hook) (logrus.FieldLogger, error) {
+ if err := configure(defaultLogger, out, format, level, hooks...); err != nil {
+ return nil, err
+ }
// We replace the gRPC logger with a custom one because the default one is too chatty.
grpcLogger := logrus.New() //nolint:forbidigo
- configure(grpcLogger, out, format, mapGRPCLogLevel(level), hooks...)
+
+ if err := configure(grpcLogger, out, format, mapGRPCLogLevel(level), hooks...); err != nil {
+ return nil, err
+ }
+
grpcmwlogrus.ReplaceGrpcLogger(grpcLogger.WithField("pid", os.Getpid()))
- return Default()
+ return Default(), nil
}
-func configure(logger *logrus.Logger, out io.Writer, format, level string, hooks ...logrus.Hook) {
+func configure(logger *logrus.Logger, out io.Writer, format, level string, hooks ...logrus.Hook) error {
var formatter logrus.Formatter
switch format {
case "json":
@@ -77,7 +84,7 @@ func configure(logger *logrus.Logger, out io.Writer, format, level string, hooks
case "", "text":
formatter = UTCTextFormatter()
default:
- logrus.WithField("format", format).Fatal("invalid logger format")
+ return fmt.Errorf("invalid logger format %q", format)
}
logrusLevel, err := logrus.ParseLevel(level)
@@ -91,6 +98,8 @@ func configure(logger *logrus.Logger, out io.Writer, format, level string, hooks
for _, hook := range hooks {
logger.Hooks.Add(hook)
}
+
+ return nil
}
func mapGRPCLogLevel(level string) string {
diff --git a/internal/log/logger_test.go b/internal/log/logger_test.go
index d710eebe4..db2f0fe2e 100644
--- a/internal/log/logger_test.go
+++ b/internal/log/logger_test.go
@@ -101,7 +101,7 @@ func TestConfigure(t *testing.T) {
out.Reset()
logger := newLogger()
- configure(logger, &out, tc.format, tc.level, tc.hooks...)
+ require.NoError(t, configure(logger, &out, tc.format, tc.level, tc.hooks...))
// We cannot directly compare the loggers with each other because they contain function
// pointers, so we have to check the relevant fields one by one.
diff --git a/internal/praefect/config/log.go b/internal/praefect/config/log.go
index 429034539..4bfe26408 100644
--- a/internal/praefect/config/log.go
+++ b/internal/praefect/config/log.go
@@ -9,8 +9,6 @@ import (
// ConfigureLogger applies the settings from the configuration file to the
// logger, setting the log level and format.
-func (c Config) ConfigureLogger() *logrus.Entry {
- log.Configure(os.Stdout, c.Logging.Format, c.Logging.Level)
-
- return log.Default()
+func (c Config) ConfigureLogger() (logrus.FieldLogger, error) {
+ return log.Configure(os.Stdout, c.Logging.Format, c.Logging.Level)
}
diff --git a/internal/praefect/middleware/methodtype.go b/internal/praefect/middleware/methodtype.go
index a13278831..bd0d4b11a 100644
--- a/internal/praefect/middleware/methodtype.go
+++ b/internal/praefect/middleware/methodtype.go
@@ -10,9 +10,9 @@ import (
)
// MethodTypeUnaryInterceptor returns a Unary Interceptor that records the method type of incoming RPC requests
-func MethodTypeUnaryInterceptor(r *protoregistry.Registry) grpc.UnaryServerInterceptor {
+func MethodTypeUnaryInterceptor(r *protoregistry.Registry, logger logrus.FieldLogger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
- observeMethodType(r, info.FullMethod)
+ observeMethodType(r, logger, info.FullMethod)
res, err := handler(ctx, req)
@@ -21,9 +21,9 @@ func MethodTypeUnaryInterceptor(r *protoregistry.Registry) grpc.UnaryServerInter
}
// MethodTypeStreamInterceptor returns a Stream Interceptor that records the method type of incoming RPC requests
-func MethodTypeStreamInterceptor(r *protoregistry.Registry) grpc.StreamServerInterceptor {
+func MethodTypeStreamInterceptor(r *protoregistry.Registry, logger logrus.FieldLogger) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
- observeMethodType(r, info.FullMethod)
+ observeMethodType(r, logger, info.FullMethod)
err := handler(srv, stream)
@@ -31,14 +31,14 @@ func MethodTypeStreamInterceptor(r *protoregistry.Registry) grpc.StreamServerInt
}
}
-func observeMethodType(registry *protoregistry.Registry, fullMethod string) {
+func observeMethodType(registry *protoregistry.Registry, logger logrus.FieldLogger, fullMethod string) {
if registry.IsInterceptedMethod(fullMethod) {
return
}
mi, err := registry.LookupMethod(fullMethod)
if err != nil {
- logrus.WithField("full_method_name", fullMethod).WithError(err).Debug("error when looking up method info")
+ logger.WithField("full_method_name", fullMethod).WithError(err).Debug("error when looking up method info")
}
var opType string
diff --git a/internal/praefect/server.go b/internal/praefect/server.go
index 3084a62bd..c425a0eb1 100644
--- a/internal/praefect/server.go
+++ b/internal/praefect/server.go
@@ -82,7 +82,7 @@ func commonUnaryServerInterceptors(logger *logrus.Entry, messageProducer grpcmwl
grpctracing.UnaryServerTracingInterceptor(),
// Panic handler should remain last so that application panics will be
// converted to errors and logged
- panichandler.UnaryPanicHandler,
+ panichandler.UnaryPanicHandler(logger),
}
}
@@ -131,7 +131,7 @@ func NewGRPCServer(
unaryInterceptors := append(
commonUnaryServerInterceptors(deps.Logger, logMsgProducer),
- middleware.MethodTypeUnaryInterceptor(deps.Registry),
+ middleware.MethodTypeUnaryInterceptor(deps.Registry, deps.Logger),
auth.UnaryServerInterceptor(deps.Config.Auth),
)
unaryInterceptors = append(unaryInterceptors, serverCfg.unaryInterceptors...)
@@ -139,7 +139,7 @@ func NewGRPCServer(
streamInterceptors := []grpc.StreamServerInterceptor{
grpcmwtags.StreamServerInterceptor(ctxtagsInterceptorOption()),
grpccorrelation.StreamServerCorrelationInterceptor(), // Must be above the metadata handler
- middleware.MethodTypeStreamInterceptor(deps.Registry),
+ middleware.MethodTypeStreamInterceptor(deps.Registry, deps.Logger),
metadatahandler.StreamInterceptor,
grpcprometheus.StreamServerInterceptor,
grpcmwlogrus.StreamServerInterceptor(deps.Logger,
@@ -153,7 +153,7 @@ func NewGRPCServer(
auth.StreamServerInterceptor(deps.Config.Auth),
// Panic handler should remain last so that application panics will be
// converted to errors and logged
- panichandler.StreamPanicHandler,
+ panichandler.StreamPanicHandler(deps.Logger),
}
streamInterceptors = append(streamInterceptors, serverCfg.streamInterceptors...)
diff --git a/internal/testhelper/configure.go b/internal/testhelper/configure.go
index 399e20bfc..537485239 100644
--- a/internal/testhelper/configure.go
+++ b/internal/testhelper/configure.go
@@ -81,7 +81,10 @@ func Run(m *testing.M, opts ...RunOption) {
// configure sets up the global test configuration. On failure,
// terminates the program.
func configure() (_ func(), returnedErr error) {
- logger := log.Configure(os.Stdout, "json", "panic")
+ logger, err := log.Configure(os.Stdout, "json", "panic")
+ if err != nil {
+ return nil, fmt.Errorf("configuring logger failed: %w", err)
+ }
for key, value := range map[string]string{
// We inject the following two variables, which instruct Git to search its
diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.go
index 51d29bcdc..5cb357878 100644
--- a/internal/testhelper/testserver/gitaly.go
+++ b/internal/testhelper/testserver/gitaly.go
@@ -318,7 +318,7 @@ func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, cfg config.Cfg) *
}
if gsd.diskCache == nil {
- gsd.diskCache = cache.New(cfg, gsd.locator)
+ gsd.diskCache = cache.New(cfg, gsd.locator, gsd.logger)
}
if gsd.packObjectsCache == nil {