From df87addbbfca2ec39715220e329052d8f7f5026f Mon Sep 17 00:00:00 2001 From: Geon Kim Date: Sun, 10 Dec 2023 14:18:25 +0900 Subject: [PATCH] feat(memlimit): use log/slog for logging --- .github/workflows/test.yml | 12 ++++++------ examples/gosigar/go.mod | 4 +--- go.mod | 2 +- go.sum | 8 ++++++++ memlimit/memlimit.go | 33 ++++++++++++++++++++++----------- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b573fcd..a51fc17 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,15 +15,15 @@ jobs: - name: Pull golang image run: | - docker pull golang:1.19 + docker pull golang:1.21 - name: Run tests in Go container (1000m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.19 go test -v ./... -expected=1048576000 + docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.21 go test -v ./... -expected=1048576000 - name: Run tests in Go container (4321m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 + docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.21 go test -v ./... -expected=4530896896 test-ubuntu-22_04: runs-on: ubuntu-22.04 @@ -37,12 +37,12 @@ jobs: - name: Pull golang image run: | - docker pull golang:1.19 + docker pull golang:1.21 - name: Run tests in Go container (1000m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.19 go test -v ./... -expected=1048576000 + docker run --rm -v=$(pwd):/app -w=/app -m=1000m golang:1.21 go test -v ./... -expected=1048576000 - name: Run tests in Go container (4321m) run: | - docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.19 go test -v ./... -expected=4530896896 + docker run --rm -v=$(pwd):/app -w=/app -m=4321m golang:1.21 go test -v ./... -expected=4530896896 diff --git a/examples/gosigar/go.mod b/examples/gosigar/go.mod index a5660f1..265ed5e 100644 --- a/examples/gosigar/go.mod +++ b/examples/gosigar/go.mod @@ -1,8 +1,6 @@ module github.com/KimMachineGun/automemlimit/examples/gosigar -go 1.21 - -toolchain go1.21.0 +go 1.21.0 require ( github.com/KimMachineGun/automemlimit v0.0.0 diff --git a/go.mod b/go.mod index ff8c023..6c45b1b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/KimMachineGun/automemlimit -go 1.19 +go 1.21.0 require github.com/containerd/cgroups/v3 v3.0.1 diff --git a/go.sum b/go.sum index 680e0c6..4d2ac07 100644 --- a/go.sum +++ b/go.sum @@ -9,23 +9,30 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= +github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0= github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= 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.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 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= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -36,3 +43,4 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/memlimit/memlimit.go b/memlimit/memlimit.go index dbb4d1c..1e7bf98 100644 --- a/memlimit/memlimit.go +++ b/memlimit/memlimit.go @@ -3,8 +3,7 @@ package memlimit import ( "errors" "fmt" - "io" - "log" + "log/slog" "math" "os" "runtime/debug" @@ -29,7 +28,7 @@ var ( ) type config struct { - logger *log.Logger + logger *slog.Logger ratio float64 env bool provider Provider @@ -65,6 +64,12 @@ func WithProvider(provider Provider) Option { } } +func WithLogger(logger *slog.Logger) Option { + return func(cfg *config) { + cfg.logger = logger + } +} + // SetGoMemLimitWithOpts sets GOMEMLIMIT with options. // // Options: @@ -73,20 +78,23 @@ func WithProvider(provider Provider) Option { // - WithProvider func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { cfg := &config{ - logger: log.New(io.Discard, "", log.LstdFlags), + logger: slog.Default(), ratio: defaultAUTOMEMLIMIT, env: false, provider: FromCgroup, } - if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" { - cfg.logger = log.Default() + if _, ok := os.LookupEnv(envAUTOMEMLIMIT_DEBUG); ok { + cfg.logger.Warn("AUTOMEMLIMIT_DEBUG is deprecated, use slog.Level instead") } for _, opt := range opts { opt(cfg) } defer func() { if _err != nil { - cfg.logger.Println(_err) + cfg.logger.Error( + "failed to set GOMEMLIMIT", + slog.Any("err", _err), + ) } }() @@ -95,7 +103,10 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { err := recover() if err != nil { if _err != nil { - cfg.logger.Println(_err) + cfg.logger.Error( + "failed to set GOMEMLIMIT", + slog.Any("error", _err), + ) } _err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous value %d: %v", snapshot, err) debug.SetMemoryLimit(snapshot) @@ -103,14 +114,14 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { }() if val, ok := os.LookupEnv(envGOMEMLIMIT); ok { - cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val) + cfg.logger.Info("GOMEMLIMIT is set already, skipping", slog.String("GOMEMLIMIT", val)) return 0, nil } ratio := cfg.ratio if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok { if val == "off" { - cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n") + cfg.logger.Debug("AUTOMEMLIMIT is set to off, skipping") return 0, nil } _ratio, err := strconv.ParseFloat(val, 64) @@ -125,7 +136,7 @@ func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) { return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err) } - cfg.logger.Printf("GOMEMLIMIT=%d\n", limit) + cfg.logger.Info("GOMEMLIMIT is set", slog.Int64("GOMEMLIMIT", limit)) return limit, nil }