From 3efe33b917b82447b5a8defb09cf62334049ffcd Mon Sep 17 00:00:00 2001 From: Dmytro Kozlov Date: Mon, 11 Jul 2022 20:18:30 +0300 Subject: [PATCH] vmselect/prometeus: Add limit param to api/v1/series api endpoint (#2851) * issue-2841: Add limit param to api/v1/series api endpoint * issue-2841: add change log * issue-2841: update logic * issue-2841: simplify logic * issue-2841: simplify logic, add information to documentation --- README.md | 1 + app/vmselect/prometheus/prometheus.go | 7 +++++++ docs/CHANGELOG.md | 3 ++- docs/README.md | 1 + docs/Single-server-VictoriaMetrics.md | 1 + docs/url-examples.md | 9 +++++---- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 49a8d93f3..7241c3ccd 100644 --- a/README.md +++ b/README.md @@ -636,6 +636,7 @@ VictoriaMetrics accepts `round_digits` query arg for `/api/v1/query` and `/api/v VictoriaMetrics accepts `limit` query arg for `/api/v1/labels` and `/api/v1/label//values` handlers for limiting the number of returned entries. For example, the query to `/api/v1/labels?limit=5` returns a sample of up to 5 unique labels, while ignoring the rest of labels. If the provided `limit` value exceeds the corresponding `-search.maxTagKeys` / `-search.maxTagValues` command-line flag values, then limits specified in the command-line flags are used. By default, VictoriaMetrics returns time series for the last 5 minutes from `/api/v1/series`, while the Prometheus API defaults to all time. Use `start` and `end` to select a different time range. +VictoriaMetrics accepts `limit` query arg for `/api/v1/series` handlers for limiting the number of returned entries. For example, the query to `/api/v1/series?limit=5` returns a sample of up to 5 series, while ignoring the rest. If the provided `limit` value exceeds the corresponding `-search.maxSeries` command-line flag values, then limits specified in the command-line flags are used. Additionally, VictoriaMetrics provides the following handlers: diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index 8784242fc..2ce9cfd6e 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -609,6 +609,10 @@ func SeriesHandler(qt *querytracer.Tracer, startTime time.Time, w http.ResponseW if cp.start == 0 { cp.start = cp.end - defaultStep } + limit, err := searchutils.GetInt(r, "limit") + if err != nil { + return err + } sq := storage.NewSearchQuery(cp.start, cp.end, cp.filterss, *maxSeriesLimit) metricNames, err := netstorage.SearchMetricNames(qt, sq, cp.deadline) if err != nil { @@ -620,6 +624,9 @@ func SeriesHandler(qt *querytracer.Tracer, startTime time.Time, w http.ResponseW qtDone := func() { qt.Donef("start=%d, end=%d", cp.start, cp.end) } + if limit > 0 && limit < len(metricNames) { + metricNames = metricNames[:limit] + } WriteSeriesResponse(bw, metricNames, qt, qtDone) if err := bw.Flush(); err != nil { return err diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 77561e1d1..3a4e6c6d0 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -25,7 +25,8 @@ The following tip changes can be tested by building VictoriaMetrics components f * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `-remoteWrite.headers` command-line option for specifying optional HTTP headers to send to the configured `-remoteWrite.url`. For example, `-remoteWrite.headers='Foo:Bar^^Baz:x'` would send `Foo: Bar` and `Baz: x` HTTP headers with every request to `-remoteWrite.url`. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2805). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): push per-target `scrape_samples_limit` metric to the cofigured `-remoteWrite.url` if `sample_limit` option is set for this target in [scrape_config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config). See [this feature request](https://github.com/VictoriaMetrics/operator/issues/497). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): attach node-level labels to [kubernetes_sd_config](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config) targets if `attach_metadata: {"node": true}` is set for `role: endpoints` and `role: endpointslice`. This is a feature backport from Prometheus 2.37 - see [this pull request](https://github.com/prometheus/prometheus/pull/10759). -* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add ability to specify additional HTTP headers to send to scrape targets via `headers` section in `scrape_configs`. This can be used when the scrape target requires custom authorization and authentication like in [this stackoverflow question](https://stackoverflow.com/questions/66032498/prometheus-scrape-metric-with-custom-header). For example, the following config instructs sending `My-Auth: top-secret` and `TenantID: FooBar` headers with each request to `http://host123:8080/metrics`: +* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add ability to specify additional HTTP headers to send to scrape targets via `headers` section in `scrape_configs`. This can be used when the scrape target requires custom authorization and authentication like in [this stackoverflow question](https://stackoverflow.com/questions/66032498/prometheus-scrape-metric-with-custom-header). For example, the following config instructs sending `My-Auth: top-secret` and `TenantID: FooBar` headers with each request to `http://host123:8080/metrics`. +* FEATURE: add ability to specify additional request param `limit` to `api/v1/series` endpoint. This can be used if you want to limit the number of series from this api and flag `-search.maxSeries` returns more data than needed. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2841). ```yaml scrape_configs: diff --git a/docs/README.md b/docs/README.md index 49a8d93f3..7241c3ccd 100644 --- a/docs/README.md +++ b/docs/README.md @@ -636,6 +636,7 @@ VictoriaMetrics accepts `round_digits` query arg for `/api/v1/query` and `/api/v VictoriaMetrics accepts `limit` query arg for `/api/v1/labels` and `/api/v1/label//values` handlers for limiting the number of returned entries. For example, the query to `/api/v1/labels?limit=5` returns a sample of up to 5 unique labels, while ignoring the rest of labels. If the provided `limit` value exceeds the corresponding `-search.maxTagKeys` / `-search.maxTagValues` command-line flag values, then limits specified in the command-line flags are used. By default, VictoriaMetrics returns time series for the last 5 minutes from `/api/v1/series`, while the Prometheus API defaults to all time. Use `start` and `end` to select a different time range. +VictoriaMetrics accepts `limit` query arg for `/api/v1/series` handlers for limiting the number of returned entries. For example, the query to `/api/v1/series?limit=5` returns a sample of up to 5 series, while ignoring the rest. If the provided `limit` value exceeds the corresponding `-search.maxSeries` command-line flag values, then limits specified in the command-line flags are used. Additionally, VictoriaMetrics provides the following handlers: diff --git a/docs/Single-server-VictoriaMetrics.md b/docs/Single-server-VictoriaMetrics.md index f0039ff40..2550decec 100644 --- a/docs/Single-server-VictoriaMetrics.md +++ b/docs/Single-server-VictoriaMetrics.md @@ -640,6 +640,7 @@ VictoriaMetrics accepts `round_digits` query arg for `/api/v1/query` and `/api/v VictoriaMetrics accepts `limit` query arg for `/api/v1/labels` and `/api/v1/label//values` handlers for limiting the number of returned entries. For example, the query to `/api/v1/labels?limit=5` returns a sample of up to 5 unique labels, while ignoring the rest of labels. If the provided `limit` value exceeds the corresponding `-search.maxTagKeys` / `-search.maxTagValues` command-line flag values, then limits specified in the command-line flags are used. By default, VictoriaMetrics returns time series for the last 5 minutes from `/api/v1/series`, while the Prometheus API defaults to all time. Use `start` and `end` to select a different time range. +VictoriaMetrics accepts `limit` query arg for `/api/v1/series` handlers for limiting the number of returned entries. For example, the query to `/api/v1/series?limit=5` returns a sample of up to 5 series, while ignoring the rest. If the provided `limit` value exceeds the corresponding `-search.maxSeries` command-line flag values, then limits specified in the command-line flags are used. Additionally, VictoriaMetrics provides the following handlers: diff --git a/docs/url-examples.md b/docs/url-examples.md index 12378f9c1..9700b7326 100644 --- a/docs/url-examples.md +++ b/docs/url-examples.md @@ -28,12 +28,12 @@ The expected output should return [HTTP Status 204](https://datatracker.ietf.org > Host: 127.0.0.1:8428 > User-Agent: curl/7.81.0 > Accept: */* -> +> * Mark bundle as not supporting multiuse < HTTP/1.1 204 No Content < X-Server-Hostname: eba075fb0e1a < Date: Tue, 21 Jun 2022 07:33:35 GMT -< +< * Connection #0 to host 127.0.0.1 left intact ``` @@ -59,12 +59,12 @@ The expected output should return [HTTP Status 204](https://datatracker.ietf.org > Host: 127.0.0.1:8481 > User-Agent: curl/7.81.0 > Accept: */* -> +> * Mark bundle as not supporting multiuse < HTTP/1.1 204 No Content < X-Server-Hostname: 101ed7a45c94 < Date: Tue, 21 Jun 2022 07:21:36 GMT -< +< * Connection #0 to host 127.0.0.1 left intact ``` @@ -401,6 +401,7 @@ Additional information: * [Prometheus querying API usage](https://docs.victoriametrics.com/#prometheus-querying-api-usage) * [Finding series by label matchers](https://prometheus.io/docs/prometheus/latest/querying/api/#finding-series-by-label-matchers) * [URL format for VictoriaMetrics cluster](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#url-format) +VictoriaMetrics accepts `limit` query arg for `/api/v1/series` handlers for limiting the number of returned entries. For example, the query to `/api/v1/series?limit=5` returns a sample of up to 5 series, while ignoring the rest. If the provided `limit` value exceeds the corresponding `-search.maxSeries` command-line flag values, then limits specified in the command-line flags are used. ## /api/v1/status/tsdb