Skip to content

Commit 34e02e2

Browse files
authored
client: Add Option to provide limit query param for APIs that support it (#1544)
* client: Add Option to provide limit query param for APIs that support it Signed-off-by: Ivan Ryabov <[email protected]> * Renamed formatOptions -> addOptionalURLParams and comment as per review feedback Signed-off-by: Ivan Ryabov <[email protected]> --------- Signed-off-by: Ivan Ryabov <[email protected]>
1 parent 3631776 commit 34e02e2

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

api/prometheus/v1/api.go

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ type API interface {
475475
// Flags returns the flag values that Prometheus was launched with.
476476
Flags(ctx context.Context) (FlagsResult, error)
477477
// LabelNames returns the unique label names present in the block in sorted order by given time range and matchers.
478-
LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time) ([]string, Warnings, error)
478+
LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]string, Warnings, error)
479479
// LabelValues performs a query for the values of the given label, time range and matchers.
480-
LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time) (model.LabelValues, Warnings, error)
480+
LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time, opts ...Option) (model.LabelValues, Warnings, error)
481481
// Query performs a query for the given time.
482482
Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error)
483483
// QueryRange performs a query for the given range.
@@ -489,7 +489,7 @@ type API interface {
489489
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
490490
Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error)
491491
// Series finds series by label matchers.
492-
Series(ctx context.Context, matches []string, startTime, endTime time.Time) ([]model.LabelSet, Warnings, error)
492+
Series(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]model.LabelSet, Warnings, error)
493493
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
494494
// under the TSDB's data directory and returns the directory as response.
495495
Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
@@ -502,7 +502,7 @@ type API interface {
502502
// Metadata returns metadata about metrics currently scraped by the metric name.
503503
Metadata(ctx context.Context, metric, limit string) (map[string][]Metadata, error)
504504
// TSDB returns the cardinality statistics.
505-
TSDB(ctx context.Context) (TSDBResult, error)
505+
TSDB(ctx context.Context, opts ...Option) (TSDBResult, error)
506506
// WalReplay returns the current replay status of the wal.
507507
WalReplay(ctx context.Context) (WalReplayStatus, error)
508508
}
@@ -1024,9 +1024,10 @@ func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
10241024
return res, err
10251025
}
10261026

1027-
func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time) ([]string, Warnings, error) {
1027+
func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]string, Warnings, error) {
10281028
u := h.client.URL(epLabels, nil)
1029-
q := u.Query()
1029+
q := addOptionalURLParams(u.Query(), opts)
1030+
10301031
if !startTime.IsZero() {
10311032
q.Set("start", formatTime(startTime))
10321033
}
@@ -1046,9 +1047,10 @@ func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime, e
10461047
return labelNames, w, err
10471048
}
10481049

1049-
func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time) (model.LabelValues, Warnings, error) {
1050+
func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []string, startTime, endTime time.Time, opts ...Option) (model.LabelValues, Warnings, error) {
10501051
u := h.client.URL(epLabelValues, map[string]string{"name": label})
1051-
q := u.Query()
1052+
q := addOptionalURLParams(u.Query(), opts)
1053+
10521054
if !startTime.IsZero() {
10531055
q.Set("start", formatTime(startTime))
10541056
}
@@ -1076,6 +1078,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin
10761078

10771079
type apiOptions struct {
10781080
timeout time.Duration
1081+
limit uint64
10791082
}
10801083

10811084
type Option func(c *apiOptions)
@@ -1088,20 +1091,35 @@ func WithTimeout(timeout time.Duration) Option {
10881091
}
10891092
}
10901093

1091-
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {
1092-
u := h.client.URL(epQuery, nil)
1093-
q := u.Query()
1094+
// WithLimit provides an optional maximum number of returned entries for APIs that support limit parameter
1095+
// e.g. https://prometheus.io/docs/prometheus/latest/querying/api/#instant-querie:~:text=%3A%20End%20timestamp.-,limit%3D%3Cnumber%3E,-%3A%20Maximum%20number%20of
1096+
func WithLimit(limit uint64) Option {
1097+
return func(o *apiOptions) {
1098+
o.limit = limit
1099+
}
1100+
}
10941101

1102+
func addOptionalURLParams(q url.Values, opts []Option) url.Values {
10951103
opt := &apiOptions{}
10961104
for _, o := range opts {
10971105
o(opt)
10981106
}
10991107

1100-
d := opt.timeout
1101-
if d > 0 {
1102-
q.Set("timeout", d.String())
1108+
if opt.timeout > 0 {
1109+
q.Set("timeout", opt.timeout.String())
11031110
}
11041111

1112+
if opt.limit > 0 {
1113+
q.Set("limit", strconv.FormatUint(opt.limit, 10))
1114+
}
1115+
1116+
return q
1117+
}
1118+
1119+
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {
1120+
u := h.client.URL(epQuery, nil)
1121+
q := addOptionalURLParams(u.Query(), opts)
1122+
11051123
q.Set("query", query)
11061124
if !ts.IsZero() {
11071125
q.Set("time", formatTime(ts))
@@ -1118,36 +1136,25 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ..
11181136

11191137
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error) {
11201138
u := h.client.URL(epQueryRange, nil)
1121-
q := u.Query()
1139+
q := addOptionalURLParams(u.Query(), opts)
11221140

11231141
q.Set("query", query)
11241142
q.Set("start", formatTime(r.Start))
11251143
q.Set("end", formatTime(r.End))
11261144
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
11271145

1128-
opt := &apiOptions{}
1129-
for _, o := range opts {
1130-
o(opt)
1131-
}
1132-
1133-
d := opt.timeout
1134-
if d > 0 {
1135-
q.Set("timeout", d.String())
1136-
}
1137-
11381146
_, body, warnings, err := h.client.DoGetFallback(ctx, u, q)
11391147
if err != nil {
11401148
return nil, warnings, err
11411149
}
11421150

11431151
var qres queryResult
1144-
11451152
return qres.v, warnings, json.Unmarshal(body, &qres)
11461153
}
11471154

1148-
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime, endTime time.Time) ([]model.LabelSet, Warnings, error) {
1155+
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime, endTime time.Time, opts ...Option) ([]model.LabelSet, Warnings, error) {
11491156
u := h.client.URL(epSeries, nil)
1150-
q := u.Query()
1157+
q := addOptionalURLParams(u.Query(), opts)
11511158

11521159
for _, m := range matches {
11531160
q.Add("match[]", m)
@@ -1166,8 +1173,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime, endTi
11661173
}
11671174

11681175
var mset []model.LabelSet
1169-
err = json.Unmarshal(body, &mset)
1170-
return mset, warnings, err
1176+
return mset, warnings, json.Unmarshal(body, &mset)
11711177
}
11721178

11731179
func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error) {
@@ -1278,8 +1284,10 @@ func (h *httpAPI) Metadata(ctx context.Context, metric, limit string) (map[strin
12781284
return res, err
12791285
}
12801286

1281-
func (h *httpAPI) TSDB(ctx context.Context) (TSDBResult, error) {
1287+
func (h *httpAPI) TSDB(ctx context.Context, opts ...Option) (TSDBResult, error) {
12821288
u := h.client.URL(epTSDB, nil)
1289+
q := addOptionalURLParams(u.Query(), opts)
1290+
u.RawQuery = q.Encode()
12831291

12841292
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
12851293
if err != nil {

api/prometheus/v1/api_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ func TestAPIs(t *testing.T) {
154154
}
155155
}
156156

157-
doLabelNames := func(matches []string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
157+
doLabelNames := func(matches []string, startTime, endTime time.Time, opts ...Option) func() (interface{}, Warnings, error) {
158158
return func() (interface{}, Warnings, error) {
159-
return promAPI.LabelNames(context.Background(), matches, startTime, endTime)
159+
return promAPI.LabelNames(context.Background(), matches, startTime, endTime, opts...)
160160
}
161161
}
162162

163-
doLabelValues := func(matches []string, label string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
163+
doLabelValues := func(matches []string, label string, startTime, endTime time.Time, opts ...Option) func() (interface{}, Warnings, error) {
164164
return func() (interface{}, Warnings, error) {
165-
return promAPI.LabelValues(context.Background(), label, matches, startTime, endTime)
165+
return promAPI.LabelValues(context.Background(), label, matches, startTime, endTime, opts...)
166166
}
167167
}
168168

@@ -178,9 +178,9 @@ func TestAPIs(t *testing.T) {
178178
}
179179
}
180180

181-
doSeries := func(matcher string, startTime, endTime time.Time) func() (interface{}, Warnings, error) {
181+
doSeries := func(matcher string, startTime, endTime time.Time, opts ...Option) func() (interface{}, Warnings, error) {
182182
return func() (interface{}, Warnings, error) {
183-
return promAPI.Series(context.Background(), []string{matcher}, startTime, endTime)
183+
return promAPI.Series(context.Background(), []string{matcher}, startTime, endTime, opts...)
184184
}
185185
}
186186

@@ -219,9 +219,9 @@ func TestAPIs(t *testing.T) {
219219
}
220220
}
221221

222-
doTSDB := func() func() (interface{}, Warnings, error) {
222+
doTSDB := func(opts ...Option) func() (interface{}, Warnings, error) {
223223
return func() (interface{}, Warnings, error) {
224-
v, err := promAPI.TSDB(context.Background())
224+
v, err := promAPI.TSDB(context.Background(), opts...)
225225
return v, nil, err
226226
}
227227
}

0 commit comments

Comments
 (0)