Skip to content

Commit

Permalink
feat(memlimit): use log/slog for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
KimMachineGun committed Dec 10, 2023
1 parent c47ceb5 commit df87add
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions examples/gosigar/go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
33 changes: 22 additions & 11 deletions memlimit/memlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package memlimit
import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"math"
"os"
"runtime/debug"
Expand All @@ -29,7 +28,7 @@ var (
)

type config struct {
logger *log.Logger
logger *slog.Logger
ratio float64
env bool
provider Provider
Expand Down Expand Up @@ -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:
Expand All @@ -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),
)
}
}()

Expand All @@ -95,22 +103,25 @@ 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)
}
}()

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)
Expand All @@ -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
}
Expand Down

0 comments on commit df87add

Please sign in to comment.