@@ -475,9 +475,9 @@ type API interface {
475
475
// Flags returns the flag values that Prometheus was launched with.
476
476
Flags (ctx context.Context ) (FlagsResult , error )
477
477
// 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 )
479
479
// 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 )
481
481
// Query performs a query for the given time.
482
482
Query (ctx context.Context , query string , ts time.Time , opts ... Option ) (model.Value , Warnings , error )
483
483
// QueryRange performs a query for the given range.
@@ -489,7 +489,7 @@ type API interface {
489
489
// Runtimeinfo returns the various runtime information properties about the Prometheus server.
490
490
Runtimeinfo (ctx context.Context ) (RuntimeinfoResult , error )
491
491
// 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 )
493
493
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
494
494
// under the TSDB's data directory and returns the directory as response.
495
495
Snapshot (ctx context.Context , skipHead bool ) (SnapshotResult , error )
@@ -502,7 +502,7 @@ type API interface {
502
502
// Metadata returns metadata about metrics currently scraped by the metric name.
503
503
Metadata (ctx context.Context , metric , limit string ) (map [string ][]Metadata , error )
504
504
// TSDB returns the cardinality statistics.
505
- TSDB (ctx context.Context ) (TSDBResult , error )
505
+ TSDB (ctx context.Context , opts ... Option ) (TSDBResult , error )
506
506
// WalReplay returns the current replay status of the wal.
507
507
WalReplay (ctx context.Context ) (WalReplayStatus , error )
508
508
}
@@ -1024,9 +1024,10 @@ func (h *httpAPI) Runtimeinfo(ctx context.Context) (RuntimeinfoResult, error) {
1024
1024
return res , err
1025
1025
}
1026
1026
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 ) {
1028
1028
u := h .client .URL (epLabels , nil )
1029
- q := u .Query ()
1029
+ q := addOptionalURLParams (u .Query (), opts )
1030
+
1030
1031
if ! startTime .IsZero () {
1031
1032
q .Set ("start" , formatTime (startTime ))
1032
1033
}
@@ -1046,9 +1047,10 @@ func (h *httpAPI) LabelNames(ctx context.Context, matches []string, startTime, e
1046
1047
return labelNames , w , err
1047
1048
}
1048
1049
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 ) {
1050
1051
u := h .client .URL (epLabelValues , map [string ]string {"name" : label })
1051
- q := u .Query ()
1052
+ q := addOptionalURLParams (u .Query (), opts )
1053
+
1052
1054
if ! startTime .IsZero () {
1053
1055
q .Set ("start" , formatTime (startTime ))
1054
1056
}
@@ -1076,6 +1078,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin
1076
1078
1077
1079
type apiOptions struct {
1078
1080
timeout time.Duration
1081
+ limit uint64
1079
1082
}
1080
1083
1081
1084
type Option func (c * apiOptions )
@@ -1088,20 +1091,35 @@ func WithTimeout(timeout time.Duration) Option {
1088
1091
}
1089
1092
}
1090
1093
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
+ }
1094
1101
1102
+ func addOptionalURLParams (q url.Values , opts []Option ) url.Values {
1095
1103
opt := & apiOptions {}
1096
1104
for _ , o := range opts {
1097
1105
o (opt )
1098
1106
}
1099
1107
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 ())
1103
1110
}
1104
1111
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
+
1105
1123
q .Set ("query" , query )
1106
1124
if ! ts .IsZero () {
1107
1125
q .Set ("time" , formatTime (ts ))
@@ -1118,36 +1136,25 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ..
1118
1136
1119
1137
func (h * httpAPI ) QueryRange (ctx context.Context , query string , r Range , opts ... Option ) (model.Value , Warnings , error ) {
1120
1138
u := h .client .URL (epQueryRange , nil )
1121
- q := u .Query ()
1139
+ q := addOptionalURLParams ( u .Query (), opts )
1122
1140
1123
1141
q .Set ("query" , query )
1124
1142
q .Set ("start" , formatTime (r .Start ))
1125
1143
q .Set ("end" , formatTime (r .End ))
1126
1144
q .Set ("step" , strconv .FormatFloat (r .Step .Seconds (), 'f' , - 1 , 64 ))
1127
1145
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
-
1138
1146
_ , body , warnings , err := h .client .DoGetFallback (ctx , u , q )
1139
1147
if err != nil {
1140
1148
return nil , warnings , err
1141
1149
}
1142
1150
1143
1151
var qres queryResult
1144
-
1145
1152
return qres .v , warnings , json .Unmarshal (body , & qres )
1146
1153
}
1147
1154
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 ) {
1149
1156
u := h .client .URL (epSeries , nil )
1150
- q := u .Query ()
1157
+ q := addOptionalURLParams ( u .Query (), opts )
1151
1158
1152
1159
for _ , m := range matches {
1153
1160
q .Add ("match[]" , m )
@@ -1166,8 +1173,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime, endTi
1166
1173
}
1167
1174
1168
1175
var mset []model.LabelSet
1169
- err = json .Unmarshal (body , & mset )
1170
- return mset , warnings , err
1176
+ return mset , warnings , json .Unmarshal (body , & mset )
1171
1177
}
1172
1178
1173
1179
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
1278
1284
return res , err
1279
1285
}
1280
1286
1281
- func (h * httpAPI ) TSDB (ctx context.Context ) (TSDBResult , error ) {
1287
+ func (h * httpAPI ) TSDB (ctx context.Context , opts ... Option ) (TSDBResult , error ) {
1282
1288
u := h .client .URL (epTSDB , nil )
1289
+ q := addOptionalURLParams (u .Query (), opts )
1290
+ u .RawQuery = q .Encode ()
1283
1291
1284
1292
req , err := http .NewRequest (http .MethodGet , u .String (), nil )
1285
1293
if err != nil {
0 commit comments