From c0fd228badaa726e3549b5e9a5ab8351aa25cb13 Mon Sep 17 00:00:00 2001 From: TomasKohout Date: Wed, 30 Mar 2022 12:50:11 +0200 Subject: [PATCH] Add dependency on go.uber.org/automaxprocs (#10498) * add dependency on go.uber.org/automaxprocs Signed-off-by: Tomas Kohout Co-authored-by: Peter Bourgon Co-authored-by: Julien Pivotto --- cmd/prometheus/main.go | 13 ++++++++++++- docs/feature_flags.md | 9 +++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 08564eb0f..a7386b01c 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -47,6 +47,7 @@ import ( toolkit_web "github.com/prometheus/exporter-toolkit/web" toolkit_webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag" "go.uber.org/atomic" + "go.uber.org/automaxprocs/maxprocs" kingpin "gopkg.in/alecthomas/kingpin.v2" klog "k8s.io/klog" klogv2 "k8s.io/klog/v2" @@ -149,6 +150,7 @@ type flagConfig struct { enableExpandExternalLabels bool enableNewSDManager bool enablePerStepStats bool + enableAutoGOMAXPROCS bool prometheusURL string corsRegexString string @@ -186,6 +188,9 @@ func (c *flagConfig) setFeatureListOptions(logger log.Logger) error { case "promql-per-step-stats": c.enablePerStepStats = true level.Info(logger).Log("msg", "Experimental per-step statistics reporting") + case "auto-gomaxprocs": + c.enableAutoGOMAXPROCS = true + level.Info(logger).Log("msg", "Automatically set GOMAXPROCS to match Linux container CPU quota") case "": continue case "promql-at-modifier", "promql-negative-offset": @@ -382,7 +387,7 @@ func main() { serverOnlyFlag(a, "query.max-samples", "Maximum number of samples a single query can load into memory. Note that queries will fail if they try to load more samples than this into memory, so this also limits the number of samples a query can return."). Default("50000000").IntVar(&cfg.queryMaxSamples) - a.Flag("enable-feature", "Comma separated feature names to enable. Valid options: agent, exemplar-storage, expand-external-labels, memory-snapshot-on-shutdown, promql-at-modifier, promql-negative-offset, promql-per-step-stats, remote-write-receiver (DEPRECATED), extra-scrape-metrics, new-service-discovery-manager. See https://prometheus.io/docs/prometheus/latest/feature_flags/ for more details."). + a.Flag("enable-feature", "Comma separated feature names to enable. Valid options: agent, exemplar-storage, expand-external-labels, memory-snapshot-on-shutdown, promql-at-modifier, promql-negative-offset, promql-per-step-stats, remote-write-receiver (DEPRECATED), extra-scrape-metrics, new-service-discovery-manager, auto-gomaxprocs. See https://prometheus.io/docs/prometheus/latest/feature_flags/ for more details."). Default("").StringsVar(&cfg.featureList) promlogflag.AddFlags(a, &cfg.promlogConfig) @@ -564,6 +569,12 @@ func main() { ruleManager *rules.Manager ) + if cfg.enableAutoGOMAXPROCS { + if _, err := maxprocs.Set(); err != nil { + level.Warn(logger).Log("msg", "Failed to set GOMAXPROCS automatically", "err", err) + } + } + if !agentMode { opts := promql.EngineOpts{ Logger: log.With(logger, "component", "query engine"), diff --git a/docs/feature_flags.md b/docs/feature_flags.md index 17923162b..f02be1be9 100644 --- a/docs/feature_flags.md +++ b/docs/feature_flags.md @@ -88,3 +88,12 @@ statistics. Currently this is limited to totalQueryableSamples. When disabled in either the engine or the query, per-step statistics are not computed at all. + +## Auto GOMAXPROCS + +`--enable-feature=auto-gomaxprocs` + +When enabled, GOMAXPROCS variable will be automatically set to match the container CPU limit. + +This means that Go runtime will operate as if it had only amount of CPU specified in the container +CPU limit and not all CPUs on server where it's running. diff --git a/go.mod b/go.mod index c08df3077..55a119b93 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( go.opentelemetry.io/otel/sdk v1.4.1 go.opentelemetry.io/otel/trace v1.4.1 go.uber.org/atomic v1.9.0 + go.uber.org/automaxprocs v1.4.0 go.uber.org/goleak v1.1.12 golang.org/x/net v0.0.0-20220225172249-27dd8689420f golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b diff --git a/go.sum b/go.sum index da4f7b1b1..76402fe54 100644 --- a/go.sum +++ b/go.sum @@ -1368,6 +1368,8 @@ go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/automaxprocs v1.4.0 h1:CpDZl6aOlLhReez+8S3eEotD7Jx0Os++lemPlMULQP0= +go.uber.org/automaxprocs v1.4.0/go.mod h1:/mTEdr7LvHhs0v7mjdxDreTz1OG5zdZGqgOnhWiR/+Q= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=