mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
support /api/v1/labels
This commit is contained in:
parent
6caae9167e
commit
d46bf75293
@ -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.
|
||||
|
@ -379,6 +379,24 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
|
||||
});
|
||||
}
|
||||
|
||||
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 => {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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)',
|
||||
|
Loading…
Reference in New Issue
Block a user