From be194e08d3a45b3282d292cc7e6e88899e1a737e Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Mon, 17 Feb 2025 22:30:03 +0100 Subject: [PATCH 1/3] refactor: Add support for undocumented query options for API * `WithLookbackDelta` and `WithPerStepStats` options have been added to API client. Signed-off-by: Mahendra Paipuri --- api/prometheus/v1/api.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index cddf027fd..7391120f0 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1077,8 +1077,10 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin } type apiOptions struct { - timeout time.Duration - limit uint64 + timeout time.Duration + lookbackDelta time.Duration + enablePerStepStats bool + limit uint64 } type Option func(c *apiOptions) @@ -1091,6 +1093,24 @@ func WithTimeout(timeout time.Duration) Option { } } +// WithLookbackDelta can be used to provide an optional query lookback delta for Query and QueryRange. +// This URL variable is not documented on Prometheus HTTP API. +// https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167 +func WithLookbackDelta(lookbackDelta time.Duration) Option { + return func(o *apiOptions) { + o.lookbackDelta = lookbackDelta + } +} + +// WithPerStepStats can be used to provide an optional per step stats for Query and QueryRange. +// This URL variable is not documented on Prometheus HTTP API. +// https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167 +func WithPerStepStats(enablePerStepStats bool) Option { + return func(o *apiOptions) { + o.enablePerStepStats = enablePerStepStats + } +} + // WithLimit provides an optional maximum number of returned entries for APIs that support limit parameter // e.g. https://prometheus.io/docs/prometheus/latest/querying/api/#instant-querie:~:text=%3A%20End%20timestamp.-,limit%3D%3Cnumber%3E,-%3A%20Maximum%20number%20of func WithLimit(limit uint64) Option { @@ -1109,6 +1129,14 @@ func addOptionalURLParams(q url.Values, opts []Option) url.Values { q.Set("timeout", opt.timeout.String()) } + if opt.lookbackDelta > 0 { + q.Set("lookback_delta", opt.lookbackDelta.String()) + } + + if opt.enablePerStepStats { + q.Set("stats", "all") + } + if opt.limit > 0 { q.Set("limit", strconv.FormatUint(opt.limit, 10)) } From df12d40b02fc8f420ecf010d6ba73c4d3556c234 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri <44365948+mahendrapaipuri@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:07:45 +0100 Subject: [PATCH 2/3] Update api/prometheus/v1/api.go Co-authored-by: Bartlomiej Plotka Signed-off-by: Mahendra Paipuri <44365948+mahendrapaipuri@users.noreply.github.com> --- api/prometheus/v1/api.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 7391120f0..d5e441fba 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1105,9 +1105,14 @@ func WithLookbackDelta(lookbackDelta time.Duration) Option { // WithPerStepStats can be used to provide an optional per step stats for Query and QueryRange. // This URL variable is not documented on Prometheus HTTP API. // https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167 -func WithPerStepStats(enablePerStepStats bool) Option { +type StatsValue string + +const ( + AllStatsValue StatsValue = "all" +) +func WithStats(stats StatsValue) Option { return func(o *apiOptions) { - o.enablePerStepStats = enablePerStepStats + o.stats = stats } } From c7183d6b0653b1badf71ee76a27a011eee09cd64 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Thu, 20 Feb 2025 14:14:49 +0100 Subject: [PATCH 3/3] refactor: Address PR comments and feedback * Use a custom `StatsValue` type to provide as query parameter for `stats` option. Signed-off-by: Mahendra Paipuri --- api/prometheus/v1/api.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index d5e441fba..8a72f9bfc 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1076,11 +1076,19 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin return labelValues, w, err } +// StatsValue is a type for `stats` query parameter. +type StatsValue string + +// AllStatsValue is the query parameter value to return all the query statistics. +const ( + AllStatsValue StatsValue = "all" +) + type apiOptions struct { - timeout time.Duration - lookbackDelta time.Duration - enablePerStepStats bool - limit uint64 + timeout time.Duration + lookbackDelta time.Duration + stats StatsValue + limit uint64 } type Option func(c *apiOptions) @@ -1102,14 +1110,9 @@ func WithLookbackDelta(lookbackDelta time.Duration) Option { } } -// WithPerStepStats can be used to provide an optional per step stats for Query and QueryRange. +// WithStats can be used to provide an optional per step stats for Query and QueryRange. // This URL variable is not documented on Prometheus HTTP API. // https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167 -type StatsValue string - -const ( - AllStatsValue StatsValue = "all" -) func WithStats(stats StatsValue) Option { return func(o *apiOptions) { o.stats = stats @@ -1138,8 +1141,8 @@ func addOptionalURLParams(q url.Values, opts []Option) url.Values { q.Set("lookback_delta", opt.lookbackDelta.String()) } - if opt.enablePerStepStats { - q.Set("stats", "all") + if opt.stats != "" { + q.Set("stats", string(opt.stats)) } if opt.limit > 0 {