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
pull/45478/head
Ivana Huckova 3 years ago committed by GitHub
parent 39797e33ae
commit d38f0e5d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      public/app/plugins/datasource/prometheus/query_hints.test.ts
  2. 7
      public/app/plugins/datasource/prometheus/query_hints.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 }, (_) => ({

@ -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: {

Loading…
Cancel
Save