From d38f0e5d9ed0a6e1f7c7cf900b9544fbc13d642a Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Wed, 16 Feb 2022 09:34:32 +0100 Subject: [PATCH] Prometheus: Offer hint for metrics with labels (#45396) * Prometheus: Offer hint for counter metric with labels * Update comment * Fix hinting for buckets with labe-values --- .../datasource/prometheus/query_hints.test.ts | 30 +++++++++++++++++++ .../datasource/prometheus/query_hints.ts | 7 +++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/query_hints.test.ts b/public/app/plugins/datasource/prometheus/query_hints.test.ts index 381934e694d..66b97fa7d33 100644 --- a/public/app/plugins/datasource/prometheus/query_hints.test.ts +++ b/public/app/plugins/datasource/prometheus/query_hints.test.ts @@ -88,6 +88,21 @@ describe('getQueryHints()', () => { expect(hints).toEqual([]); }); + it('returns a rate hint with action for a counter metric with labels', () => { + const series = [ + { + datapoints: [ + [23, 1000], + [24, 1001], + ], + }, + ]; + const hints = getQueryHints('metric_total{job="grafana"}', series); + expect(hints!.length).toBe(1); + expect(hints![0].label).toContain('Selected metric looks like a counter'); + expect(hints![0].fix).toBeDefined(); + }); + it('returns a rate hint w/o action for a complex counter metric', () => { const series = [ { @@ -118,6 +133,21 @@ describe('getQueryHints()', () => { }); }); + it('returns a histogram hint with action for a bucket with labels', () => { + const series = [ + { + datapoints: [ + [23, 1000], + [24, 1001], + ], + }, + ]; + const hints = getQueryHints('metric_bucket{job="grafana"}', series); + expect(hints!.length).toBe(1); + expect(hints![0].label).toContain('Selected metric has buckets.'); + expect(hints![0].fix).toBeDefined(); + }); + it('returns a sum hint when many time series results are returned for a simple metric', () => { const seriesCount = SUM_HINT_THRESHOLD_COUNT; const series = Array.from({ length: seriesCount }, (_) => ({ diff --git a/public/app/plugins/datasource/prometheus/query_hints.ts b/public/app/plugins/datasource/prometheus/query_hints.ts index 5ab6b8c2832..50bb918b958 100644 --- a/public/app/plugins/datasource/prometheus/query_hints.ts +++ b/public/app/plugins/datasource/prometheus/query_hints.ts @@ -11,7 +11,7 @@ export function getQueryHints(query: string, series?: any[], datasource?: Promet const hints = []; // ..._bucket metric needs a histogram_quantile() - const histogramMetric = query.trim().match(/^\w+_bucket$/); + const histogramMetric = query.trim().match(/^\w+_bucket$|^\w+_bucket{.*}$/); if (histogramMetric) { const label = 'Selected metric has buckets.'; hints.push({ @@ -53,12 +53,13 @@ export function getQueryHints(query: string, series?: any[], datasource?: Promet } if (counterNameMetric) { - const simpleMetric = query.trim().match(/^\w+$/); + // FixableQuery consists of metric name and optionally label-value pairs. We are not offering fix for complex queries yet. + const fixableQuery = query.trim().match(/^\w+$|^\w+{.*}$/); const verb = certain ? 'is' : 'looks like'; let label = `Selected metric ${verb} a counter.`; let fix: QueryFix | undefined; - if (simpleMetric) { + if (fixableQuery) { fix = { label: 'Consider calculating rate of counter by adding rate().', action: {