From d46bf752939ede21d4dc9bfb0e239a1c229145ae Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Thu, 7 Feb 2019 13:54:25 +0900 Subject: [PATCH] support /api/v1/labels --- .../sources/features/datasources/prometheus.md | 1 + .../datasource/prometheus/datasource.ts | 18 ++++++++++++++++++ .../datasource/prometheus/metric_find_query.ts | 15 +++++++++++++++ .../prometheus/specs/metric_find_query.test.ts | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/docs/sources/features/datasources/prometheus.md b/docs/sources/features/datasources/prometheus.md index 611a3b4d9e2..f3fbcbcd5d9 100644 --- a/docs/sources/features/datasources/prometheus.md +++ b/docs/sources/features/datasources/prometheus.md @@ -68,6 +68,7 @@ provides the following functions you can use in the `Query` input field. Name | Description ---- | -------- +*label_names()* | Returns a list of label names. *label_values(label)* | Returns a list of label values for the `label` in every metric. *label_values(metric, label)* | Returns a list of label values for the `label` in the specified metric. *metrics(metric)* | Returns a list of metrics matching the specified `metric` regex. diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index be62bd3b9f0..1fa8f6eb661 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -379,6 +379,24 @@ export class PrometheusDatasource implements DataSourceApi { }); } + getTagKeys(options) { + const url = '/api/v1/labels'; + return this.metadataRequest(url).then(result => { + return _.map(result.data.data, value => { + return { text: value }; + }); + }); + } + + getTagValues(options) { + const url = '/api/v1/label/' + options.key + '/values'; + return this.metadataRequest(url).then(result => { + return _.map(result.data.data, value => { + return { text: value }; + }); + }); + } + testDatasource() { const now = new Date().getTime(); return this.performInstantQuery({ expr: '1+1' }, now / 1000).then(response => { diff --git a/public/app/plugins/datasource/prometheus/metric_find_query.ts b/public/app/plugins/datasource/prometheus/metric_find_query.ts index 680f7a8fb98..5992efbf61c 100644 --- a/public/app/plugins/datasource/prometheus/metric_find_query.ts +++ b/public/app/plugins/datasource/prometheus/metric_find_query.ts @@ -12,10 +12,16 @@ export default class PrometheusMetricFindQuery { } process() { + const labelNamesRegex = /^label_names\(\)\s*$/; const labelValuesRegex = /^label_values\((?:(.+),\s*)?([a-zA-Z_][a-zA-Z0-9_]*)\)\s*$/; const metricNamesRegex = /^metrics\((.+)\)\s*$/; const queryResultRegex = /^query_result\((.+)\)\s*$/; + const labelNamesQuery = this.query.match(labelNamesRegex); + if (labelNamesQuery) { + return this.labelNamesQuery(); + } + const labelValuesQuery = this.query.match(labelValuesRegex); if (labelValuesQuery) { if (labelValuesQuery[1]) { @@ -39,6 +45,15 @@ export default class PrometheusMetricFindQuery { return this.metricNameAndLabelsQuery(this.query); } + labelNamesQuery() { + const url = '/api/v1/labels'; + return this.datasource.metadataRequest(url).then(result => { + return _.map(result.data.data, value => { + return { text: value }; + }); + }); + } + labelValuesQuery(label, metric) { let url; diff --git a/public/app/plugins/datasource/prometheus/specs/metric_find_query.test.ts b/public/app/plugins/datasource/prometheus/specs/metric_find_query.test.ts index 1466bd8ac96..5e45f56303a 100644 --- a/public/app/plugins/datasource/prometheus/specs/metric_find_query.test.ts +++ b/public/app/plugins/datasource/prometheus/specs/metric_find_query.test.ts @@ -42,6 +42,24 @@ describe('PrometheusMetricFindQuery', () => { }); describe('When performing metricFindQuery', () => { + it('label_names() should generate label name search query', async () => { + const query = ctx.setupMetricFindQuery({ + query: 'label_names()', + response: { + data: ['name1', 'name2', 'name3'], + }, + }); + const results = await query.process(); + + expect(results).toHaveLength(3); + expect(ctx.backendSrvMock.datasourceRequest).toHaveBeenCalledTimes(1); + expect(ctx.backendSrvMock.datasourceRequest).toHaveBeenCalledWith({ + method: 'GET', + url: 'proxied/api/v1/labels', + silent: true, + }); + }); + it('label_values(resource) should generate label search query', async () => { const query = ctx.setupMetricFindQuery({ query: 'label_values(resource)',