Skip to content

Commit d46b13d

Browse files
committed
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 <[email protected]>
1 parent b0ace3d commit d46b13d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ type apiOptions struct {
10891089
lookbackDelta time.Duration
10901090
stats StatsValue
10911091
limit uint64
1092+
customOptions map[string]string
10921093
}
10931094

10941095
type Option func(c *apiOptions)
@@ -1127,6 +1128,16 @@ func WithLimit(limit uint64) Option {
11271128
}
11281129
}
11291130

1131+
// WithCustomOption sets an arbitrary key/value pair on the API request.
1132+
func WithCustomOption(key, value string) Option {
1133+
return func(c *apiOptions) {
1134+
if c.customOptions == nil {
1135+
c.customOptions = make(map[string]string)
1136+
}
1137+
c.customOptions[key] = value
1138+
}
1139+
}
1140+
11301141
func addOptionalURLParams(q url.Values, opts []Option) url.Values {
11311142
opt := &apiOptions{}
11321143
for _, o := range opts {
@@ -1149,6 +1160,15 @@ func addOptionalURLParams(q url.Values, opts []Option) url.Values {
11491160
q.Set("limit", strconv.FormatUint(opt.limit, 10))
11501161
}
11511162

1163+
if opt.customOptions != nil {
1164+
for k, v := range opt.customOptions {
1165+
if k == "" || v == "" {
1166+
continue
1167+
}
1168+
q.Set(k, v)
1169+
}
1170+
}
1171+
11521172
return q
11531173
}
11541174

0 commit comments

Comments
 (0)