Skip to content

Commit 6b820eb

Browse files
Add support for undocumented query options for API (#1743)
* refactor: Add support for undocumented query options for API * `WithLookbackDelta` and `WithPerStepStats` options have been added to API client. Signed-off-by: Mahendra Paipuri <[email protected]> * Update api/prometheus/v1/api.go Co-authored-by: Bartlomiej Plotka <[email protected]> Signed-off-by: Mahendra Paipuri <[email protected]> * refactor: Address PR comments and feedback * Use a custom `StatsValue` type to provide as query parameter for `stats` option. Signed-off-by: Mahendra Paipuri <[email protected]> --------- Signed-off-by: Mahendra Paipuri <[email protected]> Signed-off-by: Mahendra Paipuri <[email protected]> Co-authored-by: Bartlomiej Plotka <[email protected]>
1 parent 97546ff commit 6b820eb

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

api/prometheus/v1/api.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -1076,9 +1076,19 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin
10761076
return labelValues, w, err
10771077
}
10781078

1079+
// StatsValue is a type for `stats` query parameter.
1080+
type StatsValue string
1081+
1082+
// AllStatsValue is the query parameter value to return all the query statistics.
1083+
const (
1084+
AllStatsValue StatsValue = "all"
1085+
)
1086+
10791087
type apiOptions struct {
1080-
timeout time.Duration
1081-
limit uint64
1088+
timeout time.Duration
1089+
lookbackDelta time.Duration
1090+
stats StatsValue
1091+
limit uint64
10821092
}
10831093

10841094
type Option func(c *apiOptions)
@@ -1091,6 +1101,24 @@ func WithTimeout(timeout time.Duration) Option {
10911101
}
10921102
}
10931103

1104+
// WithLookbackDelta can be used to provide an optional query lookback delta for Query and QueryRange.
1105+
// This URL variable is not documented on Prometheus HTTP API.
1106+
// https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167
1107+
func WithLookbackDelta(lookbackDelta time.Duration) Option {
1108+
return func(o *apiOptions) {
1109+
o.lookbackDelta = lookbackDelta
1110+
}
1111+
}
1112+
1113+
// WithStats can be used to provide an optional per step stats for Query and QueryRange.
1114+
// This URL variable is not documented on Prometheus HTTP API.
1115+
// https://github.com/prometheus/prometheus/blob/e04913aea2792a5c8bc7b3130c389ca1b027dd9b/promql/engine.go#L162-L167
1116+
func WithStats(stats StatsValue) Option {
1117+
return func(o *apiOptions) {
1118+
o.stats = stats
1119+
}
1120+
}
1121+
10941122
// WithLimit provides an optional maximum number of returned entries for APIs that support limit parameter
10951123
// e.g. https://prometheus.io/docs/prometheus/latest/querying/api/#instant-querie:~:text=%3A%20End%20timestamp.-,limit%3D%3Cnumber%3E,-%3A%20Maximum%20number%20of
10961124
func WithLimit(limit uint64) Option {
@@ -1109,6 +1137,14 @@ func addOptionalURLParams(q url.Values, opts []Option) url.Values {
11091137
q.Set("timeout", opt.timeout.String())
11101138
}
11111139

1140+
if opt.lookbackDelta > 0 {
1141+
q.Set("lookback_delta", opt.lookbackDelta.String())
1142+
}
1143+
1144+
if opt.stats != "" {
1145+
q.Set("stats", string(opt.stats))
1146+
}
1147+
11121148
if opt.limit > 0 {
11131149
q.Set("limit", strconv.FormatUint(opt.limit, 10))
11141150
}

0 commit comments

Comments
 (0)