grafana/public/app/plugins/datasource/prometheus/datasource.ts

341 lines
10 KiB
TypeScript
Raw Normal View History

import _ from 'lodash';
2017-11-12 01:38:10 -06:00
import $ from 'jquery';
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';
export function prometheusRegularEscape(value) {
return value.replace(/'/g, "\\\\'");
}
export function prometheusSpecialRegexEscape(value) {
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
}
export class PrometheusDatasource {
type: string;
editorSrc: string;
name: string;
supportMetrics: boolean;
url: string;
directUrl: string;
basicAuth: any;
withCredentials: any;
metricsNameCache: any;
interval: string;
2017-11-12 01:38:10 -06:00
httpMethod: string;
2018-03-12 09:13:05 -05:00
resultTransformer: ResultTransformer;
/** @ngInject */
constructor(instanceSettings, private $q, private backendSrv, private templateSrv, private timeSrv) {
this.type = 'prometheus';
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
this.name = instanceSettings.name;
this.supportMetrics = true;
this.url = instanceSettings.url;
this.directUrl = instanceSettings.directUrl;
this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials;
this.interval = instanceSettings.jsonData.timeInterval || '15s';
this.httpMethod = instanceSettings.jsonData.httpMethod || 'GET';
2018-03-12 09:13:05 -05:00
this.resultTransformer = new ResultTransformer(templateSrv);
}
2017-11-12 01:38:10 -06:00
_request(method, url, data?, requestId?) {
var options: any = {
url: this.url + url,
method: method,
requestId: requestId,
};
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;
}
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
if (this.basicAuth) {
options.headers = {
Authorization: this.basicAuth,
};
}
return this.backendSrv.datasourceRequest(options);
}
// Use this for tab completion features, wont publish response to other components
helperRequest(url) {
const options: any = {
url: this.url + url,
silent: true,
};
if (this.basicAuth || this.withCredentials) {
options.withCredentials = true;
}
return this.backendSrv.datasourceRequest(options);
}
interpolateQueryExpr(value, variable, defaultFormatFn) {
// if no multi or include all do not regexEscape
if (!variable.multi && !variable.includeAll) {
return prometheusRegularEscape(value);
}
if (typeof value === 'string') {
return prometheusSpecialRegexEscape(value);
}
var escapedValues = _.map(value, prometheusSpecialRegexEscape);
return escapedValues.join('|');
}
targetContainsTemplate(target) {
return this.templateSrv.variableExists(target.expr);
}
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);
var queries = [];
var activeTargets = [];
options = _.clone(options);
2017-04-25 05:57:23 -05:00
for (let target of options.targets) {
if (!target.expr || target.hide) {
continue;
}
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
}
// No valid targets, return the empty result to save a round trip.
if (_.isEmpty(queries)) {
return this.$q.when({ data: [] });
}
var allQueryPromise = _.map(queries, query => {
if (!query.instant) {
2017-09-06 11:03:02 -05:00
return this.performTimeSeriesQuery(query, start, end);
} else {
return this.performInstantQuery(query, end);
}
});
return this.$q.all(allQueryPromise).then(responseList => {
2018-02-21 06:26:45 -06:00
let result = [];
2017-04-25 05:57:23 -05:00
_.each(responseList, (response, index) => {
if (response.status === 'error') {
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);
});
return { data: result };
});
}
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
var minInterval = kbn.interval_to_seconds(
this.templateSrv.replace(target.interval, options.scopedVars) || options.interval
);
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
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, {
__interval: { text: interval + 's', value: interval + 's' },
__interval_ms: { text: interval * 1000, value: interval * 1000 },
2017-10-04 08:30:07 -05:00
});
}
query.step = interval;
2017-10-04 08:30:07 -05:00
// Only replace vars in expression after having (possibly) updated interval vars
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;
}
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);
}
return Math.max(interval * intervalFactor, minInterval, 1);
}
performTimeSeriesQuery(query, start, end) {
2016-09-28 04:54:25 -05:00
if (start > end) {
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-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
}
performSuggestQuery(query, cache = false) {
var url = '/api/v1/label/__name__/values';
if (cache && this.metricsNameCache && this.metricsNameCache.expire > Date.now()) {
return this.$q.when(
_.filter(this.metricsNameCache.data, metricName => {
return metricName.indexOf(query) !== 1;
})
);
}
return this.helperRequest(url).then(result => {
this.metricsNameCache = {
data: result.data.data,
expire: Date.now() + 60 * 1000,
};
return _.filter(result.data.data, metricName => {
return metricName.indexOf(query) !== 1;
});
});
}
metricFindQuery(query) {
if (!query) {
return this.$q.when([]);
}
let interpolated = this.templateSrv.replace(query, {}, this.interpolateQueryExpr);
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, this.timeSrv);
return metricFindQuery.process();
}
annotationQuery(options) {
2015-10-01 13:19:25 -05:00
var annotation = options.annotation;
var expr = annotation.expr || '';
var tagKeys = annotation.tagKeys || '';
var titleFormat = annotation.titleFormat || '';
var textFormat = annotation.textFormat || '';
2015-10-01 13:19:25 -05:00
if (!expr) {
return this.$q.when([]);
}
2015-10-01 13:19:25 -05:00
var interpolated = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr);
2015-10-01 13:19:25 -05:00
var step = '60s';
if (annotation.step) {
step = this.templateSrv.replace(annotation.step);
}
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,
step: this.adjustInterval(kbn.interval_to_seconds(step), 0, Math.ceil(end - start), 1) + 's',
2015-10-01 13:19:25 -05:00
};
var self = this;
return this.performTimeSeriesQuery(query, start, end).then(function(results) {
2015-10-01 13:19:25 -05:00
var eventList = [];
tagKeys = tagKeys.split(',');
2015-10-01 13:19:25 -05:00
_.each(results.data.data.result, function(series) {
var tags = _.chain(series.metric)
.filter(function(v, k) {
return _.includes(tagKeys, k);
})
.value();
2015-10-01 13:19:25 -05:00
for (let value of series.values) {
if (value[1] === '1') {
2015-10-01 13:19:25 -05:00
var event = {
annotation: annotation,
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);
}
}
2015-10-01 13:19:25 -05:00
});
return eventList;
});
}
2015-10-01 13:19:25 -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 };
}
});
}
getPrometheusTime(date, roundUp) {
if (_.isString(date)) {
date = dateMath.parse(date, roundUp);
}
return Math.ceil(date.valueOf() / 1000);
}
}