2017-12-21 01:39:31 -06:00
|
|
|
import _ from 'lodash';
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-11-12 01:38:10 -06:00
|
|
|
import $ from 'jquery';
|
2017-12-21 01:39:31 -06:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
import * as dateMath from 'app/core/utils/datemath';
|
|
|
|
import PrometheusMetricFindQuery from './metric_find_query';
|
2018-03-12 09:13:05 -05:00
|
|
|
import { ResultTransformer } from './result_transformer';
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2018-03-27 09:16:00 -05:00
|
|
|
export function prometheusRegularEscape(value) {
|
|
|
|
return value.replace(/'/g, "\\\\'");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prometheusSpecialRegexEscape(value) {
|
|
|
|
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PrometheusDatasource {
|
|
|
|
type: string;
|
|
|
|
editorSrc: string;
|
|
|
|
name: string;
|
|
|
|
supportMetrics: boolean;
|
|
|
|
url: string;
|
|
|
|
directUrl: string;
|
|
|
|
basicAuth: any;
|
|
|
|
withCredentials: any;
|
2017-09-07 03:09:59 -05:00
|
|
|
metricsNameCache: any;
|
2017-11-15 04:22:00 -06:00
|
|
|
interval: string;
|
2017-11-12 01:38:10 -06:00
|
|
|
httpMethod: string;
|
2018-03-12 09:13:05 -05:00
|
|
|
resultTransformer: ResultTransformer;
|
2017-08-10 02:33:17 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-12-21 01:39:31 -06:00
|
|
|
constructor(instanceSettings, private $q, private backendSrv, private templateSrv, private timeSrv) {
|
|
|
|
this.type = 'prometheus';
|
|
|
|
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
|
2017-08-10 02:33:17 -05:00
|
|
|
this.name = instanceSettings.name;
|
|
|
|
this.supportMetrics = true;
|
|
|
|
this.url = instanceSettings.url;
|
|
|
|
this.directUrl = instanceSettings.directUrl;
|
|
|
|
this.basicAuth = instanceSettings.basicAuth;
|
|
|
|
this.withCredentials = instanceSettings.withCredentials;
|
2017-12-21 01:39:31 -06:00
|
|
|
this.interval = instanceSettings.jsonData.timeInterval || '15s';
|
2018-03-12 11:41:07 -05:00
|
|
|
this.httpMethod = instanceSettings.jsonData.httpMethod || 'GET';
|
2018-03-12 09:13:05 -05:00
|
|
|
this.resultTransformer = new ResultTransformer(templateSrv);
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:38:10 -06:00
|
|
|
_request(method, url, data?, requestId?) {
|
2016-02-01 16:24:08 -06:00
|
|
|
var options: any = {
|
|
|
|
url: this.url + url,
|
2016-06-08 11:05:46 -05:00
|
|
|
method: method,
|
2017-12-21 01:39:31 -06:00
|
|
|
requestId: requestId,
|
2016-02-01 16:24:08 -06:00
|
|
|
};
|
2017-11-12 01:38:10 -06:00
|
|
|
if (method === 'GET') {
|
|
|
|
if (!_.isEmpty(data)) {
|
|
|
|
options.url =
|
|
|
|
options.url +
|
|
|
|
'?' +
|
|
|
|
_.map(data, (v, k) => {
|
|
|
|
return encodeURIComponent(k) + '=' + encodeURIComponent(v);
|
|
|
|
}).join('&');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
options.headers = {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
};
|
|
|
|
options.transformRequest = data => {
|
|
|
|
return $.param(data);
|
|
|
|
};
|
|
|
|
options.data = data;
|
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
|
|
|
if (this.basicAuth || this.withCredentials) {
|
|
|
|
options.withCredentials = true;
|
|
|
|
}
|
2017-08-10 02:33:17 -05:00
|
|
|
|
2016-02-01 16:24:08 -06:00
|
|
|
if (this.basicAuth) {
|
|
|
|
options.headers = {
|
2017-12-21 01:39:31 -06:00
|
|
|
Authorization: this.basicAuth,
|
2016-02-01 16:24:08 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
return this.backendSrv.datasourceRequest(options);
|
2016-03-03 03:49:30 -06:00
|
|
|
}
|
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
interpolateQueryExpr(value, variable, defaultFormatFn) {
|
2016-03-01 14:35:55 -06:00
|
|
|
// if no multi or include all do not regexEscape
|
|
|
|
if (!variable.multi && !variable.includeAll) {
|
2018-03-27 09:16:00 -05:00
|
|
|
return prometheusRegularEscape(value);
|
2016-03-01 14:35:55 -06:00
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
if (typeof value === 'string') {
|
2016-09-16 09:50:30 -05:00
|
|
|
return prometheusSpecialRegexEscape(value);
|
2016-03-03 03:49:30 -06:00
|
|
|
}
|
|
|
|
|
2016-09-16 09:50:30 -05:00
|
|
|
var escapedValues = _.map(value, prometheusSpecialRegexEscape);
|
2017-12-21 01:39:31 -06:00
|
|
|
return escapedValues.join('|');
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-03-01 14:35:55 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
targetContainsTemplate(target) {
|
|
|
|
return this.templateSrv.variableExists(target.expr);
|
|
|
|
}
|
2016-10-11 09:00:59 -05:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
query(options) {
|
2016-08-20 08:17:18 -05:00
|
|
|
var start = this.getPrometheusTime(options.range.from, false);
|
|
|
|
var end = this.getPrometheusTime(options.range.to, true);
|
2017-10-04 08:30:07 -05:00
|
|
|
var range = Math.ceil(end - start);
|
2016-02-01 16:24:08 -06:00
|
|
|
|
|
|
|
var queries = [];
|
2016-04-30 09:40:41 -05:00
|
|
|
var activeTargets = [];
|
|
|
|
|
2016-02-01 16:24:08 -06:00
|
|
|
options = _.clone(options);
|
2016-09-14 02:17:10 -05:00
|
|
|
|
2017-04-25 05:57:23 -05:00
|
|
|
for (let target of options.targets) {
|
2016-02-01 16:24:08 -06:00
|
|
|
if (!target.expr || target.hide) {
|
2017-05-19 03:24:41 -05:00
|
|
|
continue;
|
2016-02-01 16:24:08 -06:00
|
|
|
}
|
2016-09-14 02:17:10 -05:00
|
|
|
|
2016-04-30 09:40:41 -05:00
|
|
|
activeTargets.push(target);
|
2017-10-04 08:30:07 -05:00
|
|
|
queries.push(this.createQuery(target, options, range));
|
2017-04-25 05:57:23 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
|
|
|
// No valid targets, return the empty result to save a round trip.
|
|
|
|
if (_.isEmpty(queries)) {
|
2017-08-10 02:33:17 -05:00
|
|
|
return this.$q.when({ data: [] });
|
2016-02-01 16:24:08 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 02:17:10 -05:00
|
|
|
var allQueryPromise = _.map(queries, query => {
|
2017-09-07 04:34:46 -05:00
|
|
|
if (!query.instant) {
|
2017-09-06 11:03:02 -05:00
|
|
|
return this.performTimeSeriesQuery(query, start, end);
|
|
|
|
} else {
|
|
|
|
return this.performInstantQuery(query, end);
|
|
|
|
}
|
2016-09-14 02:17:10 -05:00
|
|
|
});
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
return this.$q.all(allQueryPromise).then(responseList => {
|
2018-02-21 06:26:45 -06:00
|
|
|
let result = [];
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-04-25 05:57:23 -05:00
|
|
|
_.each(responseList, (response, index) => {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (response.status === 'error') {
|
2016-02-01 16:24:08 -06:00
|
|
|
throw response.error;
|
|
|
|
}
|
2017-04-25 05:57:23 -05:00
|
|
|
|
2018-03-12 09:13:05 -05:00
|
|
|
let transformerOptions = {
|
|
|
|
format: activeTargets[index].format,
|
|
|
|
step: queries[index].step,
|
|
|
|
legendFormat: activeTargets[index].legendFormat,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
responseListLength: responseList.length,
|
|
|
|
responseIndex: index,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.resultTransformer.transform(result, response, transformerOptions);
|
2016-02-01 16:24:08 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
return { data: result };
|
|
|
|
});
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-10-04 08:30:07 -05:00
|
|
|
createQuery(target, options, range) {
|
|
|
|
var query: any = {};
|
|
|
|
query.instant = target.instant;
|
|
|
|
|
|
|
|
var interval = kbn.interval_to_seconds(options.interval);
|
|
|
|
// Minimum interval ("Min step"), if specified for the query. or same as interval otherwise
|
2017-12-19 09:06:54 -06:00
|
|
|
var minInterval = kbn.interval_to_seconds(
|
2017-12-21 01:39:31 -06:00
|
|
|
this.templateSrv.replace(target.interval, options.scopedVars) || options.interval
|
2017-12-19 09:06:54 -06:00
|
|
|
);
|
2017-10-04 08:30:07 -05:00
|
|
|
var intervalFactor = target.intervalFactor || 1;
|
|
|
|
// Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits
|
2017-12-21 01:39:31 -06:00
|
|
|
var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor);
|
2017-10-04 08:30:07 -05:00
|
|
|
var scopedVars = options.scopedVars;
|
|
|
|
// If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars
|
|
|
|
if (interval !== adjustedInterval) {
|
|
|
|
interval = adjustedInterval;
|
|
|
|
scopedVars = Object.assign({}, options.scopedVars, {
|
2017-12-21 01:39:31 -06:00
|
|
|
__interval: { text: interval + 's', value: interval + 's' },
|
|
|
|
__interval_ms: { text: interval * 1000, value: interval * 1000 },
|
2017-10-04 08:30:07 -05:00
|
|
|
});
|
|
|
|
}
|
2017-10-26 04:46:33 -05:00
|
|
|
query.step = interval;
|
2017-10-04 08:30:07 -05:00
|
|
|
|
|
|
|
// Only replace vars in expression after having (possibly) updated interval vars
|
2017-12-21 01:39:31 -06:00
|
|
|
query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr);
|
2017-10-04 08:30:07 -05:00
|
|
|
query.requestId = options.panelId + target.refId;
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2017-09-12 09:36:27 -05:00
|
|
|
adjustInterval(interval, minInterval, range, intervalFactor) {
|
|
|
|
// Prometheus will drop queries that might return more than 11000 data points.
|
|
|
|
// Calibrate interval if it is too small.
|
|
|
|
if (interval !== 0 && range / intervalFactor / interval > 11000) {
|
|
|
|
interval = Math.ceil(range / intervalFactor / 11000);
|
2017-03-08 23:53:50 -06:00
|
|
|
}
|
2017-10-18 06:27:55 -05:00
|
|
|
return Math.max(interval * intervalFactor, minInterval, 1);
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2017-03-08 23:53:50 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
performTimeSeriesQuery(query, start, end) {
|
2016-09-28 04:54:25 -05:00
|
|
|
if (start > end) {
|
2017-12-21 01:39:31 -06:00
|
|
|
throw { message: 'Invalid time range' };
|
2016-09-28 04:54:25 -05:00
|
|
|
}
|
|
|
|
|
2017-11-12 01:38:10 -06:00
|
|
|
var url = '/api/v1/query_range';
|
|
|
|
var data = {
|
|
|
|
query: query.expr,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
step: query.step,
|
|
|
|
};
|
|
|
|
return this._request(this.httpMethod, url, data, query.requestId);
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-09-06 11:03:02 -05:00
|
|
|
performInstantQuery(query, time) {
|
2017-11-12 01:38:10 -06:00
|
|
|
var url = '/api/v1/query';
|
|
|
|
var data = {
|
|
|
|
query: query.expr,
|
|
|
|
time: time,
|
|
|
|
};
|
|
|
|
return this._request(this.httpMethod, url, data, query.requestId);
|
2017-09-06 11:03:02 -05:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:59 -05:00
|
|
|
performSuggestQuery(query, cache = false) {
|
2017-12-21 01:39:31 -06:00
|
|
|
var url = '/api/v1/label/__name__/values';
|
2017-12-19 09:06:54 -06:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
if (cache && this.metricsNameCache && this.metricsNameCache.expire > Date.now()) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.$q.when(
|
|
|
|
_.filter(this.metricsNameCache.data, metricName => {
|
|
|
|
return metricName.indexOf(query) !== 1;
|
|
|
|
})
|
|
|
|
);
|
2017-09-07 03:09:59 -05:00
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
return this._request('GET', url).then(result => {
|
2017-09-07 03:09:59 -05:00
|
|
|
this.metricsNameCache = {
|
|
|
|
data: result.data.data,
|
2017-12-21 01:39:31 -06:00
|
|
|
expire: Date.now() + 60 * 1000,
|
2017-09-07 03:09:59 -05:00
|
|
|
};
|
|
|
|
return _.filter(result.data.data, metricName => {
|
2016-02-01 16:24:08 -06:00
|
|
|
return metricName.indexOf(query) !== 1;
|
|
|
|
});
|
|
|
|
});
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
metricFindQuery(query) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!query) {
|
|
|
|
return this.$q.when([]);
|
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
let interpolated = this.templateSrv.replace(query, {}, this.interpolateQueryExpr);
|
|
|
|
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, this.timeSrv);
|
2015-12-08 20:22:00 -06:00
|
|
|
return metricFindQuery.process();
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
annotationQuery(options) {
|
2015-10-01 13:19:25 -05:00
|
|
|
var annotation = options.annotation;
|
2017-12-21 01:39:31 -06:00
|
|
|
var expr = annotation.expr || '';
|
|
|
|
var tagKeys = annotation.tagKeys || '';
|
|
|
|
var titleFormat = annotation.titleFormat || '';
|
|
|
|
var textFormat = annotation.textFormat || '';
|
2015-10-01 13:19:25 -05:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!expr) {
|
|
|
|
return this.$q.when([]);
|
|
|
|
}
|
2015-10-01 13:19:25 -05:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
var interpolated = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr);
|
2015-10-01 13:19:25 -05:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
var step = '60s';
|
2017-03-08 23:53:50 -06:00
|
|
|
if (annotation.step) {
|
2017-08-10 02:33:17 -05:00
|
|
|
step = this.templateSrv.replace(annotation.step);
|
2017-03-08 23:53:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var start = this.getPrometheusTime(options.range.from, false);
|
|
|
|
var end = this.getPrometheusTime(options.range.to, true);
|
2015-10-01 13:19:25 -05:00
|
|
|
var query = {
|
|
|
|
expr: interpolated,
|
2017-12-21 01:39:31 -06:00
|
|
|
step: this.adjustInterval(kbn.interval_to_seconds(step), 0, Math.ceil(end - start), 1) + 's',
|
2015-10-01 13:19:25 -05:00
|
|
|
};
|
2016-06-16 03:48:26 -05:00
|
|
|
|
2015-12-08 20:22:00 -06:00
|
|
|
var self = this;
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.performTimeSeriesQuery(query, start, end).then(function(results) {
|
2015-10-01 13:19:25 -05:00
|
|
|
var eventList = [];
|
2017-12-21 01:39:31 -06:00
|
|
|
tagKeys = tagKeys.split(',');
|
2015-10-01 13:19:25 -05:00
|
|
|
|
|
|
|
_.each(results.data.data.result, function(series) {
|
|
|
|
var tags = _.chain(series.metric)
|
2017-12-19 09:06:54 -06:00
|
|
|
.filter(function(v, k) {
|
|
|
|
return _.includes(tagKeys, k);
|
|
|
|
})
|
|
|
|
.value();
|
2015-10-01 13:19:25 -05:00
|
|
|
|
2017-07-18 12:50:44 -05:00
|
|
|
for (let value of series.values) {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (value[1] === '1') {
|
2015-10-01 13:19:25 -05:00
|
|
|
var event = {
|
|
|
|
annotation: annotation,
|
2017-07-18 12:50:44 -05:00
|
|
|
time: Math.floor(parseFloat(value[0])) * 1000,
|
2018-03-12 09:13:05 -05:00
|
|
|
title: self.resultTransformer.renderTemplate(titleFormat, series.metric),
|
2015-10-01 13:19:25 -05:00
|
|
|
tags: tags,
|
2018-03-12 09:13:05 -05:00
|
|
|
text: self.resultTransformer.renderTemplate(textFormat, series.metric),
|
2015-10-01 13:19:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
eventList.push(event);
|
|
|
|
}
|
2017-07-18 12:50:44 -05:00
|
|
|
}
|
2015-10-01 13:19:25 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return eventList;
|
|
|
|
});
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2015-10-01 13:19:25 -05:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
testDatasource() {
|
2017-11-12 01:38:10 -06:00
|
|
|
let now = new Date().getTime();
|
|
|
|
return this.performInstantQuery({ expr: '1+1' }, now / 1000).then(response => {
|
|
|
|
if (response.data.status === 'success') {
|
|
|
|
return { status: 'success', message: 'Data source is working' };
|
|
|
|
} else {
|
|
|
|
return { status: 'error', message: response.error };
|
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
});
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
|
2017-08-10 02:33:17 -05:00
|
|
|
getPrometheusTime(date, roundUp) {
|
2016-02-01 16:24:08 -06:00
|
|
|
if (_.isString(date)) {
|
|
|
|
date = dateMath.parse(date, roundUp);
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
return Math.ceil(date.valueOf() / 1000);
|
2017-08-10 02:33:17 -05:00
|
|
|
}
|
2016-02-01 16:24:08 -06:00
|
|
|
}
|