2017-12-21 08:39:31 +01:00
|
|
|
import _ from 'lodash';
|
2017-11-22 10:11:50 +01:00
|
|
|
|
|
|
|
|
export default class PrometheusMetricFindQuery {
|
|
|
|
|
datasource: any;
|
|
|
|
|
query: any;
|
|
|
|
|
range: any;
|
|
|
|
|
|
|
|
|
|
constructor(datasource, query, timeSrv) {
|
|
|
|
|
this.datasource = datasource;
|
|
|
|
|
this.query = query;
|
|
|
|
|
this.range = timeSrv.timeRange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process() {
|
2018-09-03 11:00:46 +02:00
|
|
|
const labelValuesRegex = /^label_values\((?:(.+),\s*)?([a-zA-Z_][a-zA-Z0-9_]+)\)\s*$/;
|
|
|
|
|
const metricNamesRegex = /^metrics\((.+)\)\s*$/;
|
|
|
|
|
const queryResultRegex = /^query_result\((.+)\)\s*$/;
|
|
|
|
|
|
|
|
|
|
const labelValuesQuery = this.query.match(labelValuesRegex);
|
|
|
|
|
if (labelValuesQuery) {
|
|
|
|
|
if (labelValuesQuery[1]) {
|
|
|
|
|
return this.labelValuesQuery(labelValuesQuery[2], labelValuesQuery[1]);
|
2017-11-22 10:11:50 +01:00
|
|
|
} else {
|
2018-09-03 11:00:46 +02:00
|
|
|
return this.labelValuesQuery(labelValuesQuery[2], null);
|
2017-11-22 10:11:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 11:00:46 +02:00
|
|
|
const metricNamesQuery = this.query.match(metricNamesRegex);
|
|
|
|
|
if (metricNamesQuery) {
|
|
|
|
|
return this.metricNameQuery(metricNamesQuery[1]);
|
2017-11-22 10:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-03 11:00:46 +02:00
|
|
|
const queryResultQuery = this.query.match(queryResultRegex);
|
|
|
|
|
if (queryResultQuery) {
|
|
|
|
|
return this.queryResultQuery(queryResultQuery[1]);
|
2017-11-22 10:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if query contains full metric name, return metric name and label list
|
|
|
|
|
return this.metricNameAndLabelsQuery(this.query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labelValuesQuery(label, metric) {
|
2018-08-30 09:03:11 +02:00
|
|
|
let url;
|
2017-11-22 10:11:50 +01:00
|
|
|
|
|
|
|
|
if (!metric) {
|
|
|
|
|
// return label values globally
|
2017-12-21 08:39:31 +01:00
|
|
|
url = '/api/v1/label/' + label + '/values';
|
2017-11-22 10:11:50 +01:00
|
|
|
|
2018-04-24 12:27:37 +02:00
|
|
|
return this.datasource.metadataRequest(url).then(function(result) {
|
2017-11-22 10:11:50 +01:00
|
|
|
return _.map(result.data.data, function(value) {
|
|
|
|
|
return { text: value };
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2018-08-29 14:27:29 +02:00
|
|
|
const start = this.datasource.getPrometheusTime(this.range.from, false);
|
|
|
|
|
const end = this.datasource.getPrometheusTime(this.range.to, true);
|
2017-12-21 08:39:31 +01:00
|
|
|
url = '/api/v1/series?match[]=' + encodeURIComponent(metric) + '&start=' + start + '&end=' + end;
|
|
|
|
|
|
2018-04-24 12:27:37 +02:00
|
|
|
return this.datasource.metadataRequest(url).then(function(result) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const _labels = _.map(result.data.data, function(metric) {
|
2017-12-21 08:39:31 +01:00
|
|
|
return metric[label] || '';
|
2017-12-19 16:06:54 +01:00
|
|
|
}).filter(function(label) {
|
2017-12-21 08:39:31 +01:00
|
|
|
return label !== '';
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2017-11-22 10:11:50 +01:00
|
|
|
|
|
|
|
|
return _.uniq(_labels).map(function(metric) {
|
|
|
|
|
return {
|
|
|
|
|
text: metric,
|
2017-12-21 08:39:31 +01:00
|
|
|
expandable: true,
|
2017-11-22 10:11:50 +01:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricNameQuery(metricFilterPattern) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const url = '/api/v1/label/__name__/values';
|
2017-11-22 10:11:50 +01:00
|
|
|
|
2018-04-24 12:27:37 +02:00
|
|
|
return this.datasource.metadataRequest(url).then(function(result) {
|
2017-11-22 10:11:50 +01:00
|
|
|
return _.chain(result.data.data)
|
|
|
|
|
.filter(function(metricName) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const r = new RegExp(metricFilterPattern);
|
2017-11-22 10:11:50 +01:00
|
|
|
return r.test(metricName);
|
|
|
|
|
})
|
|
|
|
|
.map(function(matchedMetricName) {
|
|
|
|
|
return {
|
|
|
|
|
text: matchedMetricName,
|
2017-12-21 08:39:31 +01:00
|
|
|
expandable: true,
|
2017-11-22 10:11:50 +01:00
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.value();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryResultQuery(query) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const end = this.datasource.getPrometheusTime(this.range.to, true);
|
2017-12-21 08:39:31 +01:00
|
|
|
return this.datasource.performInstantQuery({ expr: query }, end).then(function(result) {
|
|
|
|
|
return _.map(result.data.data.result, function(metricData) {
|
2018-08-30 09:03:11 +02:00
|
|
|
let text = metricData.metric.__name__ || '';
|
2017-12-21 08:39:31 +01:00
|
|
|
delete metricData.metric.__name__;
|
|
|
|
|
text +=
|
|
|
|
|
'{' +
|
|
|
|
|
_.map(metricData.metric, function(v, k) {
|
|
|
|
|
return k + '="' + v + '"';
|
|
|
|
|
}).join(',') +
|
|
|
|
|
'}';
|
|
|
|
|
text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
|
2017-11-22 10:11:50 +01:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
return {
|
|
|
|
|
text: text,
|
|
|
|
|
expandable: true,
|
|
|
|
|
};
|
2017-11-22 10:11:50 +01:00
|
|
|
});
|
2017-12-21 08:39:31 +01:00
|
|
|
});
|
2017-11-22 10:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metricNameAndLabelsQuery(query) {
|
2018-08-29 14:27:29 +02:00
|
|
|
const start = this.datasource.getPrometheusTime(this.range.from, false);
|
|
|
|
|
const end = this.datasource.getPrometheusTime(this.range.to, true);
|
|
|
|
|
const url = '/api/v1/series?match[]=' + encodeURIComponent(query) + '&start=' + start + '&end=' + end;
|
2017-11-22 10:11:50 +01:00
|
|
|
|
2018-08-29 14:27:29 +02:00
|
|
|
const self = this;
|
2018-04-24 12:27:37 +02:00
|
|
|
return this.datasource.metadataRequest(url).then(function(result) {
|
2018-04-25 15:36:00 +02:00
|
|
|
return _.map(result.data.data, metric => {
|
2017-11-22 10:11:50 +01:00
|
|
|
return {
|
|
|
|
|
text: self.datasource.getOriginalMetricName(metric),
|
2017-12-21 08:39:31 +01:00
|
|
|
expandable: true,
|
2017-11-22 10:11:50 +01:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|