Skip to content

Commit 205d739

Browse files
committed
Set GOMAXPROCS automatically
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
1 parent 430ff5e commit 205d739

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/prometheus/client_model v0.3.0 // indirect
4343
github.com/prometheus/common v0.37.0 // indirect
4444
github.com/prometheus/procfs v0.8.0 // indirect
45+
go.uber.org/automaxprocs v1.5.1 // indirect
4546
go.uber.org/multierr v1.8.0 // indirect
4647
golang.org/x/crypto v0.3.0 // indirect
4748
golang.org/x/net v0.2.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
383383
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
384384
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
385385
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
386+
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
387+
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
386388
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
387389
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
388390
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=

main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cloudhut/kminion/v2/prometheus"
1818
promclient "github.com/prometheus/client_golang/prometheus"
1919
"github.com/prometheus/client_golang/prometheus/promhttp"
20+
"go.uber.org/automaxprocs/maxprocs"
2021
"go.uber.org/zap"
2122
)
2223

@@ -43,14 +44,16 @@ func main() {
4344
if err != nil {
4445
startupLogger.Fatal("failed to parse config", zap.Error(err))
4546
}
46-
4747
logger := logging.NewLogger(cfg.Logger, cfg.Exporter.Namespace).Named("main")
48-
if err != nil {
49-
startupLogger.Fatal("failed to create new logger", zap.Error(err))
50-
}
51-
5248
logger.Info("started kminion", zap.String("version", version), zap.String("built_at", builtAt))
5349

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

0 commit comments

Comments
 (0)