From d46b13dcf09cb67c4689d7306ca10aaad7e71ad7 Mon Sep 17 00:00:00 2001 From: Alec Merdler Date: Mon, 30 Jun 2025 12:31:39 -0400 Subject: [PATCH] feat: support arbitrary query params in request Adds 'WithCustomOption()' func which allows setting arbitrary key/value pairs to the URL query parameters of the request. The primary use case for this is to work with https://github.com/prometheus-community/prom-label-proxy. Signed-off-by: Alec Merdler --- api/prometheus/v1/api.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 8a72f9bfc..718ba95a8 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1089,6 +1089,7 @@ type apiOptions struct { lookbackDelta time.Duration stats StatsValue limit uint64 + customOptions map[string]string } type Option func(c *apiOptions) @@ -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 { @@ -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 }