From ddf5b65c51a36168e803d471f65c9f9e81abbf57 Mon Sep 17 00:00:00 2001 From: Olof Bourghardt Date: Wed, 28 Jul 2021 08:26:09 +0200 Subject: [PATCH] Prometheus: add functionality to specify desired step interval in dashboards panels (#36422) * Add select component for choosing step option * Add onStepChange * Add functionality for max step * Rename minInterval to stepInterval to describe min, max and exact step interval * Change select option from standard to exact * Add new type StepType for better type safety * Add tests for adjustInterval * Add functionality and tests for exact step option * Prometheus: Spell out min and max in select component * Prometheus: Change width of step select component and add placeholder * Prometheus: Adjust for the factor in exact step * Prometheus: Update tooltip of step lable to include max and exact options and add padding to select component to give it some breathing room from other components * Update snapshot for step tooltip * Prometheus: make tooltip more informative * Prometheus: add tooltip to interval input element * Prometheus: extract default step option * Prometheus: update snapshot for PromQueryEditor * Prometheus: change step labels to uppercase * Prometheus: define a default step option * Prometheus: use default step option in both ui component and logic * Prometheus: update snapshot for PromQueryEditor * Prometheus: refactor datasource.ts for better readability * Prometheus: change tool tip for step * Prometheus: update snapshots * Prometheus: add correct styling * Prometheus: update snapshots * Prometheus change variable name to something less superfluous * Prometheus: refactor * Prometheus: add new test for adjustInterval * Docs: Update docummentation on the step parameter for prometheus * Prometheus: make step input field smaller and change placeholder text to 15s * Prometheus: update snapshots * Prometheus: Make stepMode uniform in all places in the code * Prometheus: update documentation and tooltip for step * Prometheus: update snapshots --- docs/sources/datasources/prometheus.md | 6 +- .../prometheus/components/PromQueryEditor.tsx | 57 +++++++++++++++---- .../PromQueryEditor.test.tsx.snap | 42 +++++++++++--- .../datasource/prometheus/datasource.test.ts | 56 +++++++++++++++++- .../datasource/prometheus/datasource.ts | 32 +++++++++-- .../plugins/datasource/prometheus/types.ts | 3 + 6 files changed, 169 insertions(+), 27 deletions(-) diff --git a/docs/sources/datasources/prometheus.md b/docs/sources/datasources/prometheus.md index 7d7f09422b8..687c361b8a0 100644 --- a/docs/sources/datasources/prometheus.md +++ b/docs/sources/datasources/prometheus.md @@ -46,8 +46,10 @@ Open a graph in edit mode by clicking the title > Edit (or by pressing `e` key w | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `Query expression` | Prometheus query expression, check out the [Prometheus documentation](http://prometheus.io/docs/querying/basics/). | | `Legend format` | Controls the name of the time series, using name or pattern. For example `{{hostname}}` is replaced with the label value for the label `hostname`. | -| `Min step` | An additional lower limit for the [`step` parameter of Prometheus range queries](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries) and for the `$__interval` and `$__rate_interval` variables. The limit is absolute, it cannot modified by the _Resolution_ setting. | -| `Resolution` | `1/1` sets both the `$__interval` variable and the [`step` parameter of Prometheus range queries](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries) such that each pixel corresponds to one data point. For better performance, lower resolutions can be picked. `1/2` only retrieves a data point for every other pixel, and `1/10` retrieves one data point per 10 pixels. Note that both _Min time interval_ and _Min step_ limit the final value of `$__interval` and `step`. | +| `Step` | Use 'Minimum' or 'Maximum' step mode to set the lower or upper bounds respectively on the interval between data points. For example, set "minimum 1h" to hint that measurements were not taken more frequently. Use the 'Exact' step mode to set an exact interval between data points. `$__interval` and `$__rate_interval` are supported. | + +| `Resolution` | `1/1` sets both the `$__interval` variable and the [`step` parameter of Prometheus range queries](https://prometheus.io/docs/prometheus/latest/querying/api/#range-queries) such that each pixel corresponds to one data point. For better performance, lower resolutions can be picked. `1/2` only retrieves a data point for every other pixel, and `1/10` retrieves one data point per 10 pixels. Note that both _Min time interval_ and _Step_ limit the final value of `$__interval` and `step`. | + | `Metric lookup` | Search for metric names in this input field. | | `Format as` | Switch between `Table`, `Time series`, or `Heatmap`. `Table` will only work in the Table panel. `Heatmap` is suitable for displaying metrics of the Histogram type on a Heatmap panel. Under the hood, it converts cumulative histograms to regular ones and sorts series by the bucket bound. | | `Instant` | Perform an "instant" query, to return only the latest value that Prometheus has scraped for the requested time series. Instant queries return results much faster than normal range queries. Use them to look up label sets. | diff --git a/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx b/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx index ca655fa8ffb..ed72724567a 100644 --- a/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromQueryEditor.tsx @@ -4,7 +4,7 @@ import React, { PureComponent } from 'react'; // Types import { InlineFormLabel, LegacyForms, Select } from '@grafana/ui'; import { SelectableValue } from '@grafana/data'; -import { PromQuery } from '../types'; +import { PromQuery, StepMode } from '../types'; import PromQueryField from './PromQueryField'; import PromLink from './PromLink'; @@ -24,11 +24,29 @@ const INTERVAL_FACTOR_OPTIONS: Array> = map([1, 2, 3, 4, label: '1/' + value, })); +export const DEFAULT_STEP_MODE: SelectableValue = { + value: 'min', + label: 'Minimum', +}; + +const STEP_MODES: Array> = [ + DEFAULT_STEP_MODE, + { + value: 'max', + label: 'Maximum', + }, + { + value: 'exact', + label: 'Exact', + }, +]; + interface State { legendFormat?: string; formatOption: SelectableValue; interval?: string; intervalFactorOption: SelectableValue; + stepMode: SelectableValue; instant: boolean; exemplar: boolean; } @@ -52,6 +70,8 @@ export class PromQueryEditor extends PureComponent formatOption: FORMAT_OPTIONS.find((option) => option.value === query.format) || FORMAT_OPTIONS[0], intervalFactorOption: INTERVAL_FACTOR_OPTIONS.find((option) => option.value === query.intervalFactor) || INTERVAL_FACTOR_OPTIONS[0], + // Step mode + stepMode: STEP_MODES.find((option) => option.value === query.stepMode) || DEFAULT_STEP_MODE, // Switch options instant: Boolean(query.instant), exemplar: Boolean(query.exemplar), @@ -84,6 +104,11 @@ export class PromQueryEditor extends PureComponent this.setState({ intervalFactorOption: option }, this.onRunQuery); }; + onStepChange = (option: SelectableValue) => { + this.query.stepMode = option.value; + this.setState({ stepMode: option }, this.onRunQuery); + }; + onLegendChange = (e: React.SyntheticEvent) => { const legendFormat = e.currentTarget.value; this.query.legendFormat = legendFormat; @@ -105,7 +130,7 @@ export class PromQueryEditor extends PureComponent render() { const { datasource, query, range, data } = this.props; - const { formatOption, instant, interval, intervalFactorOption, legendFormat, exemplar } = this.state; + const { formatOption, instant, interval, intervalFactorOption, stepMode, legendFormat, exemplar } = this.state; return (
- An additional lower limit for the step parameter of the Prometheus query and for the{' '} - $__interval and $__rate_interval variables. The limit is absolute and not - modified by the "Resolution" setting. + Use 'Minimum' or 'Maximum' step mode to set the lower or upper bounds + respectively on the interval between data points. For example, set "minimum 1h" to hint + that measurements were not taken more frequently. Use the 'Exact' step mode to set an + exact interval between data points. $__interval and $__rate_interval are + supported. } > - Min step + Step +
-
Resolution
/>
- } diff --git a/public/app/plugins/datasource/prometheus/components/__snapshots__/PromQueryEditor.test.tsx.snap b/public/app/plugins/datasource/prometheus/components/__snapshots__/PromQueryEditor.test.tsx.snap index 96f1460ba5f..1d8aa57be7e 100644 --- a/public/app/plugins/datasource/prometheus/components/__snapshots__/PromQueryEditor.test.tsx.snap +++ b/public/app/plugins/datasource/prometheus/components/__snapshots__/PromQueryEditor.test.tsx.snap @@ -31,8 +31,7 @@ exports[`Render PromQueryEditor with basic options should render 1`] = ` - An additional lower limit for the step parameter of the Prometheus query and for the - + Use 'Minimum' or 'Maximum' step mode to set the lower or upper bounds respectively on the interval between data points. For example, set "minimum 1h" to hint that measurements were not taken more frequently. Use the 'Exact' step mode to set an exact interval between data points. $__interval @@ -40,18 +39,46 @@ exports[`Render PromQueryEditor with basic options should render 1`] = ` $__rate_interval - variables. The limit is absolute and not modified by the "Resolution" setting. + are supported. } - width={7} + width={5} > - Min step + Step + @@ -112,6 +139,7 @@ exports[`Render PromQueryEditor with basic options should render 1`] = ` Format