Skip to content

Add support for undocumented query options for API #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,9 +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
limit uint64
timeout time.Duration
lookbackDelta time.Duration
stats StatsValue
limit uint64
}

type Option func(c *apiOptions)
Expand All @@ -1091,6 +1101,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
}
}

// 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
func WithStats(stats StatsValue) Option {
return func(o *apiOptions) {
o.stats = stats
}
}

// 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 {
Expand All @@ -1109,6 +1137,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.stats != "" {
q.Set("stats", string(opt.stats))
}

if opt.limit > 0 {
q.Set("limit", strconv.FormatUint(opt.limit, 10))
}
Expand Down
Loading