Skip to content

feat: support arbitrary query params in request #1825

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ type apiOptions struct {
lookbackDelta time.Duration
stats StatsValue
limit uint64
customOptions map[string]string
}

type Option func(c *apiOptions)
Expand Down Expand Up @@ -1127,6 +1128,16 @@ func WithLimit(limit uint64) Option {
}
}

// WithCustomOption sets an arbitrary key/value pair on the API request.
func WithCustomOption(key, value string) Option {
return func(c *apiOptions) {
if c.customOptions == nil {
c.customOptions = make(map[string]string)
}
c.customOptions[key] = value
}
}

func addOptionalURLParams(q url.Values, opts []Option) url.Values {
opt := &apiOptions{}
for _, o := range opts {
Expand All @@ -1149,6 +1160,15 @@ func addOptionalURLParams(q url.Values, opts []Option) url.Values {
q.Set("limit", strconv.FormatUint(opt.limit, 10))
}

if opt.customOptions != nil {
for k, v := range opt.customOptions {
if k == "" || v == "" {
continue
}
q.Set(k, v)
}
}

return q
}

Expand Down
Loading