diff --git a/CHANGELOG.md b/CHANGELOG.md index a2e10303280..c56f029808c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Changed - [#7123](https://github.com/thanos-io/thanos/pull/7123) Rule: Change default Alertmanager API version to v2. +- [##7222](https://github.com/thanos-io/thanos/pull/7123) Automatic detection of memory limits and configure GOMEMLIMIT to match. ### Removed diff --git a/cmd/thanos/config.go b/cmd/thanos/config.go index 5c5318212e7..67cdf981d50 100644 --- a/cmd/thanos/config.go +++ b/cmd/thanos/config.go @@ -13,6 +13,7 @@ import ( "strings" "time" + "github.com/KimMachineGun/automemlimit/memlimit" extflag "github.com/efficientgo/tools/extkingpin" "github.com/pkg/errors" @@ -283,3 +284,42 @@ func parseFlagLabels(s []string) (labels.Labels, error) { sort.Sort(lset) return lset, nil } + +type goMemLimitConfig struct { + enableAutoGoMemlimit bool + memlimitRatio float64 +} + +func (gml *goMemLimitConfig) registerFlag(cmd extkingpin.FlagClause) *goMemLimitConfig { + cmd.Flag("enable-auto-gomemlimit", + "Enable go runtime to automatically limit memory consumption by compact component. This is an experimental feature."). + Default("false").BoolVar(&gml.enableAutoGoMemlimit) + + cmd.Flag("auto-gomemlimit.ratio", + "The ratio of reserved GOMEMLIMIT memory to the detected maximum container or system memory."). + Default("0.9").FloatVar(&gml.memlimitRatio) + + return gml +} + +func configureGoAutoMemLimit(common goMemLimitConfig) error { + if common.memlimitRatio <= 0.0 || common.memlimitRatio > 1.0 { + return errors.New("--auto-gomemlimit.ratio must be greater than 0 and less than or equal to 1.") + } + + if common.enableAutoGoMemlimit { + if _, err := memlimit.SetGoMemLimitWithOpts( + memlimit.WithRatio(common.memlimitRatio), + memlimit.WithProvider( + memlimit.ApplyFallback( + memlimit.FromCgroup, + memlimit.FromSystem, + ), + ), + ); err != nil { + return errors.Wrap(err, "Failed to set GOMEMLIMIT automatically") + } + } + + return nil +} diff --git a/cmd/thanos/main.go b/cmd/thanos/main.go index d6eafde695a..d8d69a9e7ce 100644 --- a/cmd/thanos/main.go +++ b/cmd/thanos/main.go @@ -49,6 +49,10 @@ func main() { Default(logging.LogFormatLogfmt).Enum(logging.LogFormatLogfmt, logging.LogFormatJSON) tracingConfig := extkingpin.RegisterCommonTracingFlags(app) + goMemLimitConf := goMemLimitConfig{} + + goMemLimitConf.registerFlag(app) + registerSidecar(app) registerStore(app) registerQuery(app) @@ -61,6 +65,11 @@ func main() { cmd, setup := app.Parse() logger := logging.NewLogger(*logLevel, *logFormat, *debugName) + if err := configureGoAutoMemLimit(goMemLimitConf); err != nil { + level.Error(logger).Log("msg", "failed to configure Go runtime memory limits", "err", err) + os.Exit(1) + } + // Running in container with limits but with empty/wrong value of GOMAXPROCS env var could lead to throttling by cpu // maxprocs will automate adjustment by using cgroups info about cpu limit if it set as value for runtime.GOMAXPROCS. undo, err := maxprocs.Set(maxprocs.Logger(func(template string, args ...interface{}) { diff --git a/cmd/thanos/rule.go b/cmd/thanos/rule.go index 1be8a8b406a..999f418ba15 100644 --- a/cmd/thanos/rule.go +++ b/cmd/thanos/rule.go @@ -951,7 +951,12 @@ func queryFuncCreator( queryAPIClients := grpcEndpointSet.GetQueryAPIClients() for _, i := range rand.Perm(len(queryAPIClients)) { e := query.NewRemoteEngine(logger, queryAPIClients[i], query.Opts{}) - q, err := e.NewInstantQuery(ctx, nil, qs, t) + expr, err := parser.ParseExpr(qs) + if err != nil { + level.Error(logger).Log("err", err, "query", qs) + continue + } + q, err := e.NewInstantQuery(ctx, nil, expr, t) if err != nil { level.Error(logger).Log("err", err, "query", qs) continue diff --git a/docs/components/compact.md b/docs/components/compact.md index 91e6fd04c64..95193e80156 100644 --- a/docs/components/compact.md +++ b/docs/components/compact.md @@ -279,6 +279,9 @@ usage: thanos compact [] Continuously compacts blocks in an object store bucket. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. --block-discovery-strategy="concurrent" One of concurrent, recursive. When set to concurrent, stores will concurrently issue @@ -375,6 +378,9 @@ Flags: non-downsampled data is not efficient and useful e.g it is not possible to render all samples for a human eye anyway + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --hash-func= Specify which hash function to use when calculating the hashes of produced files. If no function has been specified, it does not diff --git a/docs/components/query-frontend.md b/docs/components/query-frontend.md index 5df99529f27..41ecbd56a40 100644 --- a/docs/components/query-frontend.md +++ b/docs/components/query-frontend.md @@ -199,10 +199,16 @@ Query frontend command implements a service deployed in front of queriers to improve query parallelization and caching. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. --cache-compression-type="" Use compression in results cache. Supported values are: 'snappy' and ” (disable compression). + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. -h, --help Show context-sensitive help (also try --help-long and --help-man). --http-address="0.0.0.0:10902" diff --git a/docs/components/query.md b/docs/components/query.md index 59aa9ed783a..43d41e22b3f 100644 --- a/docs/components/query.md +++ b/docs/components/query.md @@ -294,6 +294,12 @@ Flags: --alert.query-url=ALERT.QUERY-URL The external Thanos Query URL that would be set in all alerts 'Source' field. + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --endpoint= ... Addresses of statically configured Thanos API servers (repeatable). The scheme may be prefixed with 'dns+' or 'dnssrv+' to detect diff --git a/docs/components/receive.md b/docs/components/receive.md index 8b59a87533e..5114b38a73b 100644 --- a/docs/components/receive.md +++ b/docs/components/receive.md @@ -297,6 +297,12 @@ usage: thanos receive [] Accept Prometheus remote write API requests and write to local tsdb. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --grpc-address="0.0.0.0:10901" Listen ip:port address for gRPC endpoints (StoreAPI). Make sure this address is routable diff --git a/docs/components/rule.md b/docs/components/rule.md index a60b32ca83f..98fc562a72d 100644 --- a/docs/components/rule.md +++ b/docs/components/rule.md @@ -316,7 +316,13 @@ Flags: lookups. The port defaults to 9093 or the SRV record's value. The URL path is used as a prefix for the regular Alertmanager API path. + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. --data-dir="data/" data directory + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --eval-interval=1m The default evaluation interval to use. --for-grace-period=10m Minimum duration between alert and restored "for" state. This is maintained only for alerts diff --git a/docs/components/sidecar.md b/docs/components/sidecar.md index 8a31801b13e..2b0c310af9a 100644 --- a/docs/components/sidecar.md +++ b/docs/components/sidecar.md @@ -76,6 +76,12 @@ usage: thanos sidecar [] Sidecar for Prometheus server. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --grpc-address="0.0.0.0:10901" Listen ip:port address for gRPC endpoints (StoreAPI). Make sure this address is routable diff --git a/docs/components/store.md b/docs/components/store.md index 0f5f55d0223..7e229b24413 100644 --- a/docs/components/store.md +++ b/docs/components/store.md @@ -29,6 +29,9 @@ Store node giving access to blocks in a bucket provider. Now supported GCS, S3, Azure, Swift, Tencent COS and Aliyun OSS. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. --block-discovery-strategy="concurrent" One of concurrent, recursive. When set to concurrent, stores will concurrently issue @@ -69,6 +72,9 @@ Flags: cause the store to read them. For such use cases use Prometheus + sidecar. Ignored if --no-cache-index-header option is specified. + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. --grpc-address="0.0.0.0:10901" Listen ip:port address for gRPC endpoints (StoreAPI). Make sure this address is routable @@ -375,6 +381,8 @@ While the remaining settings are **optional**: The `redis` index cache allows to use [Redis](https://redis.io) as cache backend. This cache type is configured using `--index-cache.config-file` to reference the configuration file or `--index-cache.config` to put yaml config directly: ```yaml mdox-exec="go run scripts/cfggen/main.go --name=cacheutil.RedisClientConfig" +# command-line-arguments +ld: warning: ignoring duplicate libraries: '-lproc' type: REDIS config: addr: "" diff --git a/docs/components/tools.md b/docs/components/tools.md index 6f0879d8852..82ccea24a98 100644 --- a/docs/components/tools.md +++ b/docs/components/tools.md @@ -216,9 +216,15 @@ usage: thanos tools bucket web [] Web interface for remote storage bucket. Flags: + --auto-gomemlimit.ratio=0.9 + The ratio of reserved GOMEMLIMIT memory to the + detected maximum container or system memory. --disable-admin-operations Disable UI/API admin operations like marking blocks for deletion and no compaction. + --enable-auto-gomemlimit Enable go runtime to automatically limit memory + consumption by compact component. This is an + experimental feature. -h, --help Show context-sensitive help (also try --help-long and --help-man). --http-address="0.0.0.0:10902" diff --git a/go.mod b/go.mod index 58f4171a4b0..151eebabcbe 100644 --- a/go.mod +++ b/go.mod @@ -65,7 +65,7 @@ require ( github.com/sony/gobreaker v0.5.0 github.com/stretchr/testify v1.8.4 github.com/thanos-io/objstore v0.0.0-20231112185854-37752ee64d98 - github.com/thanos-io/promql-engine v0.0.0-20240125175542-4a8e9731acba + github.com/thanos-io/promql-engine v0.0.0-20240318110350-23714ea2522d github.com/uber/jaeger-client-go v2.30.0+incompatible github.com/uber/jaeger-lib v2.4.1+incompatible // indirect github.com/vimeo/galaxycache v0.0.0-20210323154928-b7e5d71c067a @@ -126,7 +126,11 @@ require ( require ( github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect + github.com/cilium/ebpf v0.11.0 // indirect + github.com/containerd/cgroups/v3 v3.0.3 // indirect + github.com/docker/go-units v0.5.0 // indirect github.com/go-openapi/runtime v0.27.1 // indirect + github.com/godbus/dbus/v5 v5.0.4 // indirect github.com/golang-jwt/jwt/v5 v5.2.0 // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/hashicorp/go-version v1.6.0 // indirect @@ -134,6 +138,8 @@ require ( github.com/metalmatze/signal v0.0.0-20210307161603-1c9aa721a97a // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/onsi/ginkgo v1.16.5 // indirect + github.com/opencontainers/runtime-spec v1.0.2 // indirect + github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect github.com/sercand/kuberesolver/v4 v4.0.0 // indirect github.com/zhangyunhao116/umap v0.0.0-20221211160557-cb7705fafa39 // indirect go.opentelemetry.io/collector/featuregate v1.0.1 // indirect @@ -153,6 +159,7 @@ require ( require ( cloud.google.com/go/compute/metadata v0.2.3 // indirect github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.32.3 // indirect + github.com/KimMachineGun/automemlimit v0.5.0 github.com/OneOfOne/xxhash v1.2.6 // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect diff --git a/go.sum b/go.sum index 11eb3def355..43a47acb981 100644 --- a/go.sum +++ b/go.sum @@ -632,6 +632,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapp github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk= +github.com/KimMachineGun/automemlimit v0.5.0 h1:BeOe+BbJc8L5chL3OwzVYjVzyvPALdd5wxVVOWuUZmQ= +github.com/KimMachineGun/automemlimit v0.5.0/go.mod h1:di3GCKiu9Y+1fs92erCbUvKzPkNyViN3mA0vti/ykEQ= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= @@ -749,6 +751,8 @@ github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moA github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/cilium/ebpf v0.11.0 h1:V8gS/bTCCjX9uUnkUFUpPsksM8n1lXBAvHcpiFk1X2Y= +github.com/cilium/ebpf v0.11.0/go.mod h1:WE7CZAnqOL2RouJ4f1uyNhqr2P4CCvXFIqdRDUgWsVs= github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE= @@ -764,6 +768,8 @@ github.com/cncf/xds/go v0.0.0-20231109132714-523115ebc101 h1:7To3pQ+pZo0i3dsWEbi github.com/cncf/xds/go v0.0.0-20231109132714-523115ebc101/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= +github.com/containerd/cgroups/v3 v3.0.3 h1:S5ByHZ/h9PMe5IOQoN7E+nMc2UcLEM/V48DGDJ9kip0= +github.com/containerd/cgroups/v3 v3.0.3/go.mod h1:8HBe7V3aWGLFPd/k03swSIsGjZhHI2WzJmticMgVuz0= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd/v22 v22.4.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= @@ -850,6 +856,8 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8 github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= +github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= +github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= @@ -927,6 +935,7 @@ github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6Wezm github.com/gobwas/ws v1.2.1 h1:F2aeBZrm2NDsc7vbovKrWSogd4wvfAxg0FQ89/iqOTk= github.com/gobwas/ws v1.2.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= @@ -1345,6 +1354,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +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/opentracing-contrib/go-grpc v0.0.0-20180928155321-4b5a12d3ff02/go.mod h1:JNdpVEzCpXBgIiv4ds+TzhN1hrtxq6ClLrTlT9OQRSc= github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e h1:4cPxUYdgaGzZIT5/j0IfqOrrXmq6bG8AwvwisMXpdrg= github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e/go.mod h1:DYR5Eij8rJl8h7gblRrOZ8g0kW1umSpKqYIBTgeDtLo= @@ -1370,6 +1381,8 @@ github.com/ovh/go-ovh v1.4.3 h1:Gs3V823zwTFpzgGLZNI6ILS4rmxZgJwJCz54Er9LwD0= github.com/ovh/go-ovh v1.4.3/go.mod h1:AkPXVtgwB6xlKblMjRKJJmjRp+ogrE7fz2lVgcQY8SY= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= +github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= @@ -1536,8 +1549,8 @@ github.com/thanos-community/galaxycache v0.0.0-20211122094458-3a32041a1f1e h1:f1 github.com/thanos-community/galaxycache v0.0.0-20211122094458-3a32041a1f1e/go.mod h1:jXcofnrSln/cLI6/dhlBxPQZEEQHVPCcFaH75M+nSzM= github.com/thanos-io/objstore v0.0.0-20231112185854-37752ee64d98 h1:gx2MTto1UQRumGoJzY3aFPQ31Ov3nOV7NaD7j6q288k= github.com/thanos-io/objstore v0.0.0-20231112185854-37752ee64d98/go.mod h1:JauBAcJ61tRSv9widgISVmA6akQXDeUMXBrVmWW4xog= -github.com/thanos-io/promql-engine v0.0.0-20240125175542-4a8e9731acba h1:BFohBPqCWBpbqNO3F3lC2uZ0egSfPGQoSDloTRraPHU= -github.com/thanos-io/promql-engine v0.0.0-20240125175542-4a8e9731acba/go.mod h1:YGk7VqhYDfhUyZjWK7ZU1JmBQKSvr5mT5Txut8oK1MA= +github.com/thanos-io/promql-engine v0.0.0-20240318110350-23714ea2522d h1:/6Gy8ul/6iKHaAg3OhaoPmph2TRAlansv4z+VAbTOKk= +github.com/thanos-io/promql-engine v0.0.0-20240318110350-23714ea2522d/go.mod h1:YGk7VqhYDfhUyZjWK7ZU1JmBQKSvr5mT5Txut8oK1MA= github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw77ZpO1/o86g+JV3VKlk3d48jopJxzTjU= github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab/go.mod h1:eheTFp954zcWZXCU8d0AT76ftsQOTo4DTqkN/h3k1MY= github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyVklgXycLB6ek= diff --git a/pkg/query/remote_engine.go b/pkg/query/remote_engine.go index 1896d7b6412..c48768b2337 100644 --- a/pkg/query/remote_engine.go +++ b/pkg/query/remote_engine.go @@ -181,26 +181,26 @@ func (r *remoteEngine) infosWithoutReplicaLabels() infopb.TSDBInfos { return infos } -func (r *remoteEngine) NewRangeQuery(_ context.Context, opts promql.QueryOpts, qs string, start, end time.Time, interval time.Duration) (promql.Query, error) { +func (r *remoteEngine) NewRangeQuery(_ context.Context, _ promql.QueryOpts, plan parser.Expr, start, end time.Time, interval time.Duration) (promql.Query, error) { return &remoteQuery{ logger: r.logger, client: r.client, opts: r.opts, - qs: qs, + plan: plan, start: start, end: end, interval: interval, }, nil } -func (r *remoteEngine) NewInstantQuery(_ context.Context, _ promql.QueryOpts, qs string, ts time.Time) (promql.Query, error) { +func (r *remoteEngine) NewInstantQuery(_ context.Context, _ promql.QueryOpts, plan parser.Expr, ts time.Time) (promql.Query, error) { return &remoteQuery{ logger: r.logger, client: r.client, opts: r.opts, - qs: qs, + plan: plan, start: ts, end: ts, interval: 0, @@ -212,7 +212,7 @@ type remoteQuery struct { client Client opts Opts - qs string + plan parser.Expr start time.Time end time.Time interval time.Duration @@ -235,7 +235,7 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result { // Instant query. if r.start == r.end { request := &querypb.QueryRequest{ - Query: r.qs, + Query: r.plan.String(), TimeSeconds: r.start.Unix(), TimeoutSeconds: int64(r.opts.Timeout.Seconds()), EnablePartialResponse: r.opts.EnablePartialResponse, @@ -286,7 +286,7 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result { } request := &querypb.QueryRangeRequest{ - Query: r.qs, + Query: r.plan.String(), StartTimeSeconds: r.start.Unix(), EndTimeSeconds: r.end.Unix(), IntervalSeconds: int64(r.interval.Seconds()), @@ -349,7 +349,7 @@ func (r *remoteQuery) Exec(ctx context.Context) *promql.Result { } result = append(result, series) } - level.Debug(r.logger).Log("msg", "Executed query", "query", r.qs, "time", time.Since(start)) + level.Debug(r.logger).Log("msg", "Executed query", "query", r.plan, "time", time.Since(start)) return &promql.Result{Value: result, Warnings: warnings} } @@ -360,7 +360,7 @@ func (r *remoteQuery) Statement() parser.Statement { return nil } func (r *remoteQuery) Stats() *stats.Statistics { return nil } -func (r *remoteQuery) String() string { return r.qs } +func (r *remoteQuery) String() string { return r.plan.String() } func (r *remoteQuery) Cancel() { if r.cancel != nil { diff --git a/pkg/query/remote_engine_test.go b/pkg/query/remote_engine_test.go index 224acc60395..46c1159d1c9 100644 --- a/pkg/query/remote_engine_test.go +++ b/pkg/query/remote_engine_test.go @@ -14,6 +14,7 @@ import ( "github.com/go-kit/log" "github.com/pkg/errors" "github.com/prometheus/prometheus/model/labels" + "github.com/prometheus/prometheus/promql/parser" "google.golang.org/grpc" "github.com/thanos-io/thanos/pkg/api/query/querypb" @@ -27,11 +28,13 @@ func TestRemoteEngine_Warnings(t *testing.T) { Timeout: 1 * time.Second, }) var ( - query = "up" start = time.Unix(0, 0) end = time.Unix(120, 0) step = 30 * time.Second ) + query, err := parser.ParseExpr("up") + testutil.Ok(t, err) + qry, err := engine.NewRangeQuery(context.Background(), nil, query, start, end, step) testutil.Ok(t, err) res := qry.Exec(context.Background())