From 57a545626a58066c0e2737a749b79e88c8d3b34e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 21 Jun 2024 11:18:38 +0200 Subject: [PATCH] switch to github.com/containerd/log, and use structured logs Switch logging to use containerd/log, and use structured logs in some places. Signed-off-by: Sebastiaan van Stijn --- cgroup2/manager.go | 25 ++++++++++++++----------- cgroup2/utils.go | 10 +++++----- cmd/cgctl/main.go | 4 ++-- cmd/go.mod | 5 +++-- cmd/go.sum | 12 +++++++----- go.mod | 7 ++++--- go.sum | 16 ++++++++-------- 7 files changed, 43 insertions(+), 36 deletions(-) diff --git a/cgroup2/manager.go b/cgroup2/manager.go index 34bc26a1..0305471b 100644 --- a/cgroup2/manager.go +++ b/cgroup2/manager.go @@ -31,10 +31,10 @@ import ( "github.com/containerd/cgroups/v3/cgroup2/stats" + "github.com/containerd/log" systemdDbus "github.com/coreos/go-systemd/v22/dbus" "github.com/godbus/dbus/v5" "github.com/opencontainers/runtime-spec/specs-go" - "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) @@ -403,7 +403,7 @@ func (c *Manager) Kill() error { if err == nil { return nil } - logrus.Warnf("falling back to slower kill implementation: %s", err) + log.L.Warnf("falling back to slower kill implementation: %s", err) // Fallback to slow method. return c.fallbackKill() } @@ -416,13 +416,15 @@ func (c *Manager) Kill() error { // // https://github.com/opencontainers/runc/blob/8da0a0b5675764feaaaaad466f6567a9983fcd08/libcontainer/init_linux.go#L523-L529 func (c *Manager) fallbackKill() error { + logger := log.G(context.TODO()).WithFields(log.Fields{"path": c.path}) + if err := c.Freeze(); err != nil { - logrus.Warn(err) + logger.WithError(err).Warn("freezing cgroup2.manager") } pids, err := c.Procs(true) if err != nil { if err := c.Thaw(); err != nil { - logrus.Warn(err) + logger.WithError(err).Warn("thawing cgroup2.manager") } return err } @@ -430,16 +432,16 @@ func (c *Manager) fallbackKill() error { for _, pid := range pids { p, err := os.FindProcess(int(pid)) if err != nil { - logrus.Warn(err) + logger.WithFields(log.Fields{"error": err, "pid": int(pid)}).Warnf("finding process") continue } procs = append(procs, p) if err := p.Signal(unix.SIGKILL); err != nil { - logrus.Warn(err) + logger.WithFields(log.Fields{"error": err, "pid": int(pid)}).Warnf("signaling process") } } if err := c.Thaw(); err != nil { - logrus.Warn(err) + logger.WithError(err).Warn("thawing cgroup2.manager") } subreaper, err := getSubreaper() @@ -461,7 +463,7 @@ func (c *Manager) fallbackKill() error { if subreaper == 0 { if _, err := p.Wait(); err != nil { if !errors.Is(err, unix.ECHILD) { - logrus.Warnf("wait on pid %d failed: %s", p.Pid, err) + logger.WithFields(log.Fields{"error": err, "pid": p.Pid}).Warn("waiting on process") } } } @@ -957,17 +959,18 @@ func startUnit(conn *systemdDbus.Conn, group string, properties []systemdDbus.Pr return fmt.Errorf("error creating systemd unit `%s`: got `%s`", group, s) } case <-time.After(30 * time.Second): - logrus.Warnf("Timed out while waiting for StartTransientUnit(%s) completion signal from dbus. Continuing...", group) + log.G(ctx).Warnf("Timed out while waiting for StartTransientUnit(%s) completion signal from dbus. Continuing...", group) } return nil } func attemptFailedUnitReset(conn *systemdDbus.Conn, group string) { - err := conn.ResetFailedUnitContext(context.TODO(), group) + ctx := context.TODO() + err := conn.ResetFailedUnitContext(ctx, group) if err != nil { - logrus.Warnf("Unable to reset failed unit: %v", err) + log.G(ctx).Warnf("Unable to reset failed unit: %v", err) } } diff --git a/cgroup2/utils.go b/cgroup2/utils.go index 54537c3e..0974ce6a 100644 --- a/cgroup2/utils.go +++ b/cgroup2/utils.go @@ -32,9 +32,9 @@ import ( "github.com/containerd/cgroups/v3/cgroup2/stats" + "github.com/containerd/log" "github.com/godbus/dbus/v5" "github.com/opencontainers/runtime-spec/specs-go" - "github.com/sirupsen/logrus" "golang.org/x/sys/unix" ) @@ -264,7 +264,7 @@ func getStatFileContentUint64(filePath string) uint64 { res, err := parseUint(trimmed, 10, 64) if err != nil { - logrus.Errorf("unable to parse %q as a uint from Cgroup file %q", trimmed, filePath) + log.L.Errorf("unable to parse %q as a uint from Cgroup file %q", trimmed, filePath) return res } @@ -432,7 +432,7 @@ func hugePageSizes() []string { hPageSizes, err = getHugePageSizeFromFilenames(files) if err != nil { - logrus.Warnf("hugePageSizes: %s", err) + log.L.Warnf("hugePageSizes: %s", err) } }) @@ -507,7 +507,7 @@ func getStatPSIFromFile(path string) *stats.PSIStats { if pv != nil { err = parsePSIData(parts[1:], pv) if err != nil { - logrus.Errorf("failed to read file %s: %v", path, err) + log.L.WithError(err).Errorf("failed to read file %s", path) return nil } } @@ -515,7 +515,7 @@ func getStatPSIFromFile(path string) *stats.PSIStats { if err := sc.Err(); err != nil { if !errors.Is(err, unix.ENOTSUP) && !errors.Is(err, unix.EOPNOTSUPP) { - logrus.Errorf("unable to parse PSI data: %v", err) + log.L.WithError(err).Error("unable to parse PSI data") } return nil } diff --git a/cmd/cgctl/main.go b/cmd/cgctl/main.go index 6697748c..5e8f1d46 100644 --- a/cmd/cgctl/main.go +++ b/cmd/cgctl/main.go @@ -24,7 +24,7 @@ import ( "github.com/containerd/cgroups/v3" "github.com/containerd/cgroups/v3/cgroup2" - "github.com/sirupsen/logrus" + "github.com/containerd/log" "github.com/urfave/cli" ) @@ -56,7 +56,7 @@ func main() { } app.Before = func(clix *cli.Context) error { if clix.GlobalBool("debug") { - logrus.SetLevel(logrus.DebugLevel) + _ = log.SetLevel("debug") } return nil } diff --git a/cmd/go.mod b/cmd/go.mod index 8d84e33b..e75c5b73 100644 --- a/cmd/go.mod +++ b/cmd/go.mod @@ -6,7 +6,7 @@ replace github.com/containerd/cgroups/v3 => ../ require ( github.com/containerd/cgroups/v3 v3.0.0-00010101000000-000000000000 - github.com/sirupsen/logrus v1.9.0 + github.com/containerd/log v0.1.0 github.com/urfave/cli v1.22.5 ) @@ -18,7 +18,8 @@ require ( github.com/opencontainers/runtime-spec v1.0.2 // indirect github.com/russross/blackfriday/v2 v2.0.1 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect - golang.org/x/sys v0.6.0 // indirect + golang.org/x/sys v0.7.0 // indirect google.golang.org/protobuf v1.33.0 // indirect ) diff --git a/cmd/go.sum b/cmd/go.sum index 229ab67e..480b9f4d 100644 --- a/cmd/go.sum +++ b/cmd/go.sum @@ -1,6 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= @@ -23,19 +25,19 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU= github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/go.mod b/go.mod index c39ea46f..14140366 100644 --- a/go.mod +++ b/go.mod @@ -4,20 +4,21 @@ go 1.18 require ( github.com/cilium/ebpf v0.11.0 + github.com/containerd/log v0.1.0 github.com/coreos/go-systemd/v22 v22.3.2 github.com/docker/go-units v0.4.0 github.com/godbus/dbus/v5 v5.0.4 github.com/opencontainers/runtime-spec v1.0.2 - github.com/sirupsen/logrus v1.9.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.1.12 - golang.org/x/sys v0.6.0 + golang.org/x/sys v0.7.0 google.golang.org/protobuf v1.33.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 70b8b929..d3a26fc8 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -21,14 +23,12 @@ github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= @@ -51,8 +51,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=