Skip to content

Commit

Permalink
Set GOMAXPROCS automatically
Browse files Browse the repository at this point in the history
This is required in containerised environments to properly set
GOMAXPROCS to actual container limits set and not the whole underlying node's CPUs

See:
https://github.com/uber-go/automaxprocs
  • Loading branch information
amuraru committed Dec 16, 2022
1 parent 430ff5e commit 205d739
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
go.uber.org/automaxprocs v1.5.1 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cloudhut/kminion/v2/prometheus"
promclient "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
)

Expand All @@ -43,14 +44,16 @@ func main() {
if err != nil {
startupLogger.Fatal("failed to parse config", zap.Error(err))
}

logger := logging.NewLogger(cfg.Logger, cfg.Exporter.Namespace).Named("main")
if err != nil {
startupLogger.Fatal("failed to create new logger", zap.Error(err))
}

logger.Info("started kminion", zap.String("version", version), zap.String("built_at", builtAt))

// set GOMAXPROCS automatically
l := func(format string, a ...interface{}) {
logger.Info(fmt.Sprintf(format, a...))
}
if _, err = maxprocs.Set(maxprocs.Logger(l)); err != nil {
logger.Fatal("failed to set GOMAXPROCS automatically", zap.Error(err))
}
// Setup context that stops when the application receives an interrupt signal
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
Expand Down

0 comments on commit 205d739

Please sign in to comment.