From 0ee977b827c38df00af3582dcfb016e573c5630d Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Fri, 4 Aug 2023 14:26:23 +0200 Subject: [PATCH] Docs: document options and trade-offs to query label names and values (#5582) * Docs: document options and trade-offs to query label names and values Signed-off-by: Marco Pracucci * Moved under new query section Signed-off-by: Marco Pracucci * Addressed review comments Signed-off-by: Marco Pracucci * Address Ursula feedback Signed-off-by: Marco Pracucci * Fix correctness Signed-off-by: Marco Pracucci * Final comments from Ursula Signed-off-by: Marco Pracucci --------- Signed-off-by: Marco Pracucci --- CHANGELOG.md | 1 + docs/sources/mimir/query/_index.md | 12 ++ .../mimir/query/query-metric-labels.md | 117 ++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 docs/sources/mimir/query/_index.md create mode 100644 docs/sources/mimir/query/query-metric-labels.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 44477167c04..23b820409f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,6 +127,7 @@ * [CHANGE] Fix link to external OTLP/HTTP documentation. * [ENHANCEMENT] Improved `MimirRulerTooManyFailedQueries` runbook. #5586 * [ENHANCEMENT] Improved "Recover accidentally deleted blocks" runbook. #5620 +* [ENHANCEMENT] Documented options and trade-offs to query label names and values. #5582 ### Tools diff --git a/docs/sources/mimir/query/_index.md b/docs/sources/mimir/query/_index.md new file mode 100644 index 00000000000..dbc9798c140 --- /dev/null +++ b/docs/sources/mimir/query/_index.md @@ -0,0 +1,12 @@ +--- +description: Learn how to query metric data from Grafana Mimir. +title: Query metric data from Grafana Mimir +menuTitle: Query +weight: 44 +keywords: + - query +--- + +# Query metric data from Grafana Mimir + +{{< section menuTitle="true" >}} diff --git a/docs/sources/mimir/query/query-metric-labels.md b/docs/sources/mimir/query/query-metric-labels.md new file mode 100644 index 00000000000..95fc7ab5c3b --- /dev/null +++ b/docs/sources/mimir/query/query-metric-labels.md @@ -0,0 +1,117 @@ +--- +description: Learn how to query metric labels from Grafana Mimir. +title: Query metric labels +menuTitle: Query metric labels +weight: 2 +keywords: + - labels +--- + +# Query metric labels + +Grafana Mimir supports multiple [HTTP API]({{< relref "../references/http-api/index.md" >}}) endpoints to query metric label names and values. +There are differences and trade-off between these API endpoints. + +## Querying label names + +The following API endpoints find the list of label names of all (or a subset of) series stored in Mimir: + +- **[Get label names]({{< relref "../references/http-api/index.md#get-label-names" >}})**
+ `GET,POST /api/v1/labels` +- **[Label names cardinality]({{< relref "../references/http-api/index.md#label-names-cardinality" >}})**
+ `GET,POST /api/v1/cardinality/label_names` + +{{% admonition type="note" %}} +`` is the Prometheus HTTP prefix as documented in the [HTTP API]({{< relref "../references/http-api/index.md#path-prefixes" >}}) reference. +The default prefix is `/prometheus`. +{{% /admonition %}} + +### Recommendations and performance trade-off + +- Prefer _get label names_ over _label names cardinality_ API, unless you need to know the number of unique label values for each label name. +- The _get label names_ API `start` and `end` parameters are optional. When omitted, Mimir will query label names for the whole retention period or up to the configured `-store.max-labels-query-length`. We recommend to always specify `start` and `end` parameters, and preferably set them to the shortest period that is feasible for your use case. +- For both API endpoints, executing a request specifying the series selector is computationally more expensive than the same request without the series selector. However, the results set might be significantly smaller when the series selector reduces the series set. + +### Features comparison + +The different API endpoints have different features, and the main differences follow. + +#### Can specify the time range in the request? + +- **Get label names**
+ Yes. However, the actual time range granularity is restricted to TSDB block range periods, which are `2h` for the most recent metrics, and up to `24h` for historical metrics when running Mimir with the default configuration. + In practice, the input `start` and `end` parameters are rounded to the TSDB block boundaries: + - The `start` parameter is rounded to the start of the nearest block containing samples with timestamps that are greater than or equal to the input timestamp. + - The `end` parameter is rounded to the end of the nearest block containing samples with timestamps that are less than or equal to the input timestamp. +- **Label names cardinality**
+ No. The API queries only the in-memory series in the ingesters that hold series for the tenant. + +#### Can specify the series selector in the request? + +- **Get label names API**
+ Yes. The optional `match[]` parameter makes it possible to select the series from which to read the label names. The `match[]` parameter can be specified multiple times. When omitted, label names are extracted from all series. +- **Label names cardinality API**
+ Yes. The optional `selector` parameter makes it possible to select the series from which to read the label names. The `selector` parameter can be specified only once. When omitted, label names are extracted from all series. + +## Querying label values + +The following API endpoints find the list of label values for a given label name: + +- **[Get label values]({{< relref "../references/http-api/index.md#get-label-values" >}})**
+ `GET /api/v1/label/{name}/values` +- **[Label values cardinality]({{< relref "../references/http-api/index.md#label-values-cardinality" >}})**
+ `GET,POST /api/v1/cardinality/label_values` + +{{% admonition type="note" %}} +`` is the Prometheus HTTP prefix as documented in the [HTTP API]({{< relref "../references/http-api/index.md#path-prefixes" >}}) reference. +The default prefix is `/prometheus`. +{{% /admonition %}} + +### Recommendations and performance trade-off + +- Prefer _get label values_ over _label values cardinality_ API, unless you need to know the number of series for each label name and value. +- The _get label values_ API makes it possible to specify a single label name, and the _label values cardinality_ API makes it possible to specify multiple label names. If you need to get the label values for multiple label names, we recommend issuing multiple _get label values_ requests instead of a single _label values cardinality_ API call with multiple label names. +- The _get label values_ API `start` and `end` parameters are optional. When omitted, Mimir will query label values for the whole retention period or up to the configured `-store.max-labels-query-length`. We recommend to always specify `start` and `end` parameters, and preferably set them to the shortest period that is feasible for your use case. The longer the time range, the more computationally expensive the request is. +- For both API endpoints, executing a request specifying the series selector is computationally more expensive than the same request without the series selector. However, the results set might be significantly smaller when the series selector reduces the series set. + +### Features comparison + +The different API endpoints have different features, and the main differences follow. + +#### Can specify the time range in the request? + +- **Get label values API**
+ Yes. However, the actual time range granularity is restricted to TSDB block range periods, which are `2h` for the most recent metrics, and up to `24h` for historical metrics when running Mimir with the default configuration. + In practice, the input `start` and `end` parameters are rounded to the TSDB block boundaries: + - The `start` parameter is rounded to the start of the nearest block containing samples with timestamps that are greater than or equal to the input timestamp. + - The `end` parameter is rounded to the end of the nearest block containing samples with timestamps that are less than or equal to the input timestamp. +- **Label values cardinality API**
+ No. The API queries only the in-memory series in the ingesters that hold series for the tenant. + +#### Can specify the series selector in the request? + +- **Get label values API**
+ Yes. The optional `match[]` parameter makes it possible to select the series from which to read the label values. The `match[]` parameter can be specified multiple times. When omitted, label values are extracted from all series. +- **Label values cardinality API**
+ Yes. The optional `selector` parameter makes it possible to select the series from which to read the label names and values. The `selector` parameter can be specified only once. When omitted, label names and values are extracted from all series. + +## Alternatives + +The following API endpoints have not been explicitly designed to query metric label names and values, but label names and/or values can be extracted from the response: + +- **[Get series by label matchers]({{< relref "../references/http-api/index.md#get-series-by-label-matchers" >}})**
+ `GET,POST /api/v1/series` +- **[Instant query]({{< relref "../references/http-api/index.md#instant-query" >}})**
+ `GET,POST /api/v1/query` + +{{% admonition type="note" %}} +`` is the Prometheus HTTP prefix as documented in the [HTTP API]({{< relref "../references/http-api/index.md#path-prefixes" >}}) reference. +The default prefix is `/prometheus`. +{{% /admonition %}} + +### Recommendations and performance trade-off + +- Prefer _get label names_ over other APIs to get the list of label names. The _get label names_ is expected to always perform better than other APIs. +- Prefer _get label values_ over other APIs to get the list of label values. + - If you're experiencing a slow response time when querying _get label values_ API, the request includes a series selector and the series selector is expected to match a small set of series (order of few thousands), then consider using _get series by label matchers_ instead of the _get label values_ API. + - If you can't use _get series by label matchers_ API (e.g. the API is not available in the client you're using), then fallback to the _instant query_ API.