2016-02-02 05:52:43 -06:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
import _ from "lodash";
|
|
|
|
import * as dateMath from "app/core/utils/datemath";
|
|
|
|
import { isVersionGtOrEq, SemVersion } from "app/core/utils/version";
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
/** @ngInject */
|
2017-12-19 09:06:54 -06:00
|
|
|
export function GraphiteDatasource(
|
|
|
|
instanceSettings,
|
|
|
|
$q,
|
|
|
|
backendSrv,
|
|
|
|
templateSrv
|
|
|
|
) {
|
2016-02-02 05:52:43 -06:00
|
|
|
this.basicAuth = instanceSettings.basicAuth;
|
|
|
|
this.url = instanceSettings.url;
|
|
|
|
this.name = instanceSettings.name;
|
2017-12-19 09:06:54 -06:00
|
|
|
this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || "0.9";
|
2017-10-20 03:23:14 -05:00
|
|
|
this.supportsTags = supportsTags(this.graphiteVersion);
|
2016-02-02 05:52:43 -06:00
|
|
|
this.cacheTimeout = instanceSettings.cacheTimeout;
|
|
|
|
this.withCredentials = instanceSettings.withCredentials;
|
2017-12-19 09:06:54 -06:00
|
|
|
this.render_method = instanceSettings.render_method || "POST";
|
2016-02-02 05:52:43 -06:00
|
|
|
|
2017-08-31 07:05:52 -05:00
|
|
|
this.getQueryOptionsInfo = function() {
|
|
|
|
return {
|
2017-12-19 09:06:54 -06:00
|
|
|
maxDataPoints: true,
|
|
|
|
cacheTimeout: true,
|
|
|
|
links: [
|
2017-08-31 07:05:52 -05:00
|
|
|
{
|
|
|
|
text: "Help",
|
2017-12-19 09:06:54 -06:00
|
|
|
url:
|
|
|
|
"http://docs.grafana.org/features/datasources/graphite/#using-graphite-in-grafana"
|
2017-08-31 07:05:52 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-02-02 05:52:43 -06:00
|
|
|
this.query = function(options) {
|
2016-03-22 15:27:53 -05:00
|
|
|
var graphOptions = {
|
|
|
|
from: this.translateTime(options.rangeRaw.from, false),
|
|
|
|
until: this.translateTime(options.rangeRaw.to, true),
|
|
|
|
targets: options.targets,
|
|
|
|
format: options.format,
|
|
|
|
cacheTimeout: options.cacheTimeout || this.cacheTimeout,
|
2017-12-19 09:06:54 -06:00
|
|
|
maxDataPoints: options.maxDataPoints
|
2016-03-22 15:27:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var params = this.buildGraphiteParams(graphOptions, options.scopedVars);
|
|
|
|
if (params.length === 0) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return $q.when({ data: [] });
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
2016-03-22 15:27:53 -05:00
|
|
|
|
2016-06-16 03:48:26 -05:00
|
|
|
var httpOptions: any = {
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "POST",
|
|
|
|
url: "/render",
|
|
|
|
data: params.join("&"),
|
2016-06-16 03:48:26 -05:00
|
|
|
headers: {
|
2017-12-19 09:06:54 -06:00
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
}
|
2016-06-16 03:48:26 -05:00
|
|
|
};
|
2016-03-22 15:27:53 -05:00
|
|
|
|
2016-06-16 03:48:26 -05:00
|
|
|
if (options.panelId) {
|
2017-12-19 09:06:54 -06:00
|
|
|
httpOptions.requestId = this.name + ".panelId." + options.panelId;
|
2016-03-22 15:27:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.doGraphiteRequest(httpOptions).then(this.convertDataPointsToMs);
|
2016-02-02 05:52:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
this.convertDataPointsToMs = function(result) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!result || !result.data) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
for (var i = 0; i < result.data.length; i++) {
|
|
|
|
var series = result.data[i];
|
|
|
|
for (var y = 0; y < series.datapoints.length; y++) {
|
|
|
|
series.datapoints[y][1] *= 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
this.parseTags = function(tagString) {
|
|
|
|
let tags = [];
|
2017-12-19 09:06:54 -06:00
|
|
|
tags = tagString.split(",");
|
2017-10-07 03:31:39 -05:00
|
|
|
if (tags.length === 1) {
|
2017-12-19 09:06:54 -06:00
|
|
|
tags = tagString.split(" ");
|
|
|
|
if (tags[0] === "") {
|
2017-10-07 03:31:39 -05:00
|
|
|
tags = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
};
|
|
|
|
|
2016-02-02 05:52:43 -06:00
|
|
|
this.annotationQuery = function(options) {
|
|
|
|
// Graphite metric as annotation
|
|
|
|
if (options.annotation.target) {
|
2017-12-19 09:06:54 -06:00
|
|
|
var target = templateSrv.replace(options.annotation.target, {}, "glob");
|
2016-02-02 05:52:43 -06:00
|
|
|
var graphiteQuery = {
|
|
|
|
rangeRaw: options.rangeRaw,
|
|
|
|
targets: [{ target: target }],
|
2017-12-19 09:06:54 -06:00
|
|
|
format: "json",
|
2016-02-02 05:52:43 -06:00
|
|
|
maxDataPoints: 100
|
|
|
|
};
|
|
|
|
|
2016-03-04 05:13:40 -06:00
|
|
|
return this.query(graphiteQuery).then(function(result) {
|
2016-02-02 05:52:43 -06:00
|
|
|
var list = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < result.data.length; i++) {
|
|
|
|
var target = result.data[i];
|
|
|
|
|
|
|
|
for (var y = 0; y < target.datapoints.length; y++) {
|
|
|
|
var datapoint = target.datapoints[y];
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!datapoint[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
list.push({
|
|
|
|
annotation: options.annotation,
|
|
|
|
time: datapoint[1],
|
|
|
|
title: target.target
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Graphite event as annotation
|
|
|
|
var tags = templateSrv.replace(options.annotation.tags);
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.events({ range: options.rangeRaw, tags: tags }).then(
|
|
|
|
results => {
|
|
|
|
var list = [];
|
|
|
|
for (var i = 0; i < results.data.length; i++) {
|
|
|
|
var e = results.data[i];
|
|
|
|
|
|
|
|
var tags = e.tags;
|
|
|
|
if (_.isString(e.tags)) {
|
|
|
|
tags = this.parseTags(e.tags);
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
list.push({
|
|
|
|
annotation: options.annotation,
|
|
|
|
time: e.when * 1000,
|
|
|
|
title: e.what,
|
|
|
|
tags: tags,
|
|
|
|
text: e.data
|
|
|
|
});
|
2017-10-07 03:31:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
return list;
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
);
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.events = function(options) {
|
|
|
|
try {
|
2017-12-19 09:06:54 -06:00
|
|
|
var tags = "";
|
2016-02-02 05:52:43 -06:00
|
|
|
if (options.tags) {
|
2017-12-19 09:06:54 -06:00
|
|
|
tags = "&tags=" + options.tags;
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
return this.doGraphiteRequest({
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "GET",
|
|
|
|
url:
|
|
|
|
"/events/get_data?from=" +
|
|
|
|
this.translateTime(options.range.from, false) +
|
|
|
|
"&until=" +
|
|
|
|
this.translateTime(options.range.to, true) +
|
|
|
|
tags
|
2016-02-02 05:52:43 -06:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return $q.reject(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-11 09:00:59 -05:00
|
|
|
this.targetContainsTemplate = function(target) {
|
|
|
|
return templateSrv.variableExists(target.target);
|
|
|
|
};
|
|
|
|
|
2016-02-02 05:52:43 -06:00
|
|
|
this.translateTime = function(date, roundUp) {
|
|
|
|
if (_.isString(date)) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (date === "now") {
|
|
|
|
return "now";
|
|
|
|
} else if (date.indexOf("now-") >= 0 && date.indexOf("/") === -1) {
|
2016-02-02 05:52:43 -06:00
|
|
|
date = date.substring(3);
|
2017-12-19 09:06:54 -06:00
|
|
|
date = date.replace("m", "min");
|
|
|
|
date = date.replace("M", "mon");
|
2016-02-02 05:52:43 -06:00
|
|
|
return date;
|
|
|
|
}
|
|
|
|
date = dateMath.parse(date, roundUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// graphite' s from filter is exclusive
|
|
|
|
// here we step back one minute in order
|
|
|
|
// to guarantee that we get all the data that
|
|
|
|
// exists for the specified range
|
|
|
|
if (roundUp) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (date.get("s")) {
|
|
|
|
date.add(1, "m");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
} else if (roundUp === false) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (date.get("s")) {
|
|
|
|
date.subtract(1, "m");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.unix();
|
|
|
|
};
|
|
|
|
|
2017-07-31 15:39:44 -05:00
|
|
|
this.metricFindQuery = function(query, optionalOptions) {
|
|
|
|
let options = optionalOptions || {};
|
2017-07-31 10:21:15 -05:00
|
|
|
let interpolatedQuery = templateSrv.replace(query);
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
let httpOptions: any = {
|
|
|
|
method: "GET",
|
|
|
|
url: "/metrics/find",
|
2017-07-31 10:21:15 -05:00
|
|
|
params: {
|
|
|
|
query: interpolatedQuery
|
|
|
|
},
|
|
|
|
// for cancellations
|
2017-12-19 09:06:54 -06:00
|
|
|
requestId: options.requestId
|
2017-07-31 10:21:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options && options.range) {
|
2017-08-08 06:51:54 -05:00
|
|
|
httpOptions.params.from = this.translateTime(options.range.from, false);
|
|
|
|
httpOptions.params.until = this.translateTime(options.range.to, true);
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
2017-07-31 10:21:15 -05:00
|
|
|
return this.doGraphiteRequest(httpOptions).then(results => {
|
|
|
|
return _.map(results.data, metric => {
|
2016-02-02 05:52:43 -06:00
|
|
|
return {
|
|
|
|
text: metric.text,
|
|
|
|
expandable: metric.expandable ? true : false
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-04 11:46:18 -05:00
|
|
|
this.getTags = function(optionalOptions) {
|
|
|
|
let options = optionalOptions || {};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
let httpOptions: any = {
|
|
|
|
method: "GET",
|
|
|
|
url: "/tags",
|
2017-10-04 11:46:18 -05:00
|
|
|
// for cancellations
|
2017-12-19 09:06:54 -06:00
|
|
|
requestId: options.requestId
|
2017-10-04 11:46:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options && options.range) {
|
|
|
|
httpOptions.params.from = this.translateTime(options.range.from, false);
|
|
|
|
httpOptions.params.until = this.translateTime(options.range.to, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.doGraphiteRequest(httpOptions).then(results => {
|
|
|
|
return _.map(results.data, tag => {
|
|
|
|
return {
|
|
|
|
text: tag.tag,
|
|
|
|
id: tag.id
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getTagValues = function(tag, optionalOptions) {
|
|
|
|
let options = optionalOptions || {};
|
|
|
|
|
2017-10-27 05:15:54 -05:00
|
|
|
let httpOptions: any = {
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "GET",
|
|
|
|
url: "/tags/" + tag,
|
2017-10-04 11:46:18 -05:00
|
|
|
// for cancellations
|
2017-12-19 09:06:54 -06:00
|
|
|
requestId: options.requestId
|
2017-10-04 11:46:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options && options.range) {
|
|
|
|
httpOptions.params.from = this.translateTime(options.range.from, false);
|
|
|
|
httpOptions.params.until = this.translateTime(options.range.to, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.doGraphiteRequest(httpOptions).then(results => {
|
|
|
|
if (results.data && results.data.values) {
|
|
|
|
return _.map(results.data.values, value => {
|
|
|
|
return {
|
|
|
|
text: value.value,
|
|
|
|
id: value.id
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-27 05:15:54 -05:00
|
|
|
this.getTagsAutoComplete = (expression, tagPrefix) => {
|
|
|
|
let httpOptions: any = {
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "GET",
|
|
|
|
url: "/tags/autoComplete/tags",
|
2017-10-27 05:15:54 -05:00
|
|
|
params: {
|
|
|
|
expr: expression
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (tagPrefix) {
|
|
|
|
httpOptions.params.tagPrefix = tagPrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.doGraphiteRequest(httpOptions).then(results => {
|
|
|
|
if (results.data) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return _.map(results.data, tag => {
|
2017-10-27 05:15:54 -05:00
|
|
|
return { text: tag };
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getTagValuesAutoComplete = (expression, tag, valuePrefix) => {
|
|
|
|
let httpOptions: any = {
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "GET",
|
|
|
|
url: "/tags/autoComplete/values",
|
2017-10-27 05:15:54 -05:00
|
|
|
params: {
|
2017-10-31 02:20:01 -05:00
|
|
|
expr: expression,
|
|
|
|
tag: tag
|
2017-10-27 05:15:54 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (valuePrefix) {
|
|
|
|
httpOptions.params.valuePrefix = valuePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.doGraphiteRequest(httpOptions).then(results => {
|
|
|
|
if (results.data) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return _.map(results.data, value => {
|
2017-10-27 05:15:54 -05:00
|
|
|
return { text: value };
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-20 08:25:19 -05:00
|
|
|
this.getVersion = function() {
|
|
|
|
let httpOptions = {
|
2017-12-19 09:06:54 -06:00
|
|
|
method: "GET",
|
|
|
|
url: "/version/_" // Prevent last / trimming
|
2017-10-20 08:25:19 -05:00
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.doGraphiteRequest(httpOptions)
|
|
|
|
.then(results => {
|
|
|
|
if (results.data) {
|
|
|
|
let semver = new SemVersion(results.data);
|
|
|
|
return semver.isValid() ? results.data : "";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return "";
|
|
|
|
});
|
2017-10-20 08:25:19 -05:00
|
|
|
};
|
|
|
|
|
2016-02-02 05:52:43 -06:00
|
|
|
this.testDatasource = function() {
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.metricFindQuery("*").then(function() {
|
|
|
|
return { status: "success", message: "Data source is working" };
|
2016-02-02 05:52:43 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.doGraphiteRequest = function(options) {
|
|
|
|
if (this.basicAuth || this.withCredentials) {
|
|
|
|
options.withCredentials = true;
|
|
|
|
}
|
|
|
|
if (this.basicAuth) {
|
|
|
|
options.headers = options.headers || {};
|
|
|
|
options.headers.Authorization = this.basicAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.url = this.url + options.url;
|
2017-12-19 09:06:54 -06:00
|
|
|
options.inspect = { type: "graphite" };
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
return backendSrv.datasourceRequest(options);
|
|
|
|
};
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
this._seriesRefLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
this.buildGraphiteParams = function(options, scopedVars) {
|
2017-12-19 09:06:54 -06:00
|
|
|
var graphite_options = [
|
|
|
|
"from",
|
|
|
|
"until",
|
|
|
|
"rawData",
|
|
|
|
"format",
|
|
|
|
"maxDataPoints",
|
|
|
|
"cacheTimeout"
|
|
|
|
];
|
|
|
|
var clean_options = [],
|
|
|
|
targets = {};
|
2016-02-02 05:52:43 -06:00
|
|
|
var target, targetValue, i;
|
|
|
|
var regex = /\#([A-Z])/g;
|
|
|
|
var intervalFormatFixRegex = /'(\d+)m'/gi;
|
|
|
|
var hasTargets = false;
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
options["format"] = "json";
|
2016-02-02 05:52:43 -06:00
|
|
|
|
|
|
|
function fixIntervalFormat(match) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return match.replace("m", "min").replace("M", "mon");
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < options.targets.length; i++) {
|
|
|
|
target = options.targets[i];
|
|
|
|
if (!target.target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target.refId) {
|
|
|
|
target.refId = this._seriesRefLetters[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
targetValue = templateSrv.replace(target.target, scopedVars);
|
2017-12-19 09:06:54 -06:00
|
|
|
targetValue = targetValue.replace(
|
|
|
|
intervalFormatFixRegex,
|
|
|
|
fixIntervalFormat
|
|
|
|
);
|
2016-02-02 05:52:43 -06:00
|
|
|
targets[target.refId] = targetValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nestedSeriesRegexReplacer(match, g1) {
|
2016-07-14 03:29:22 -05:00
|
|
|
return targets[g1] || match;
|
2016-02-02 05:52:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < options.targets.length; i++) {
|
|
|
|
target = options.targets[i];
|
|
|
|
if (!target.target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
targetValue = targets[target.refId];
|
|
|
|
targetValue = targetValue.replace(regex, nestedSeriesRegexReplacer);
|
|
|
|
targets[target.refId] = targetValue;
|
|
|
|
|
|
|
|
if (!target.hide) {
|
|
|
|
hasTargets = true;
|
|
|
|
clean_options.push("target=" + encodeURIComponent(targetValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
_.each(options, function(value, key) {
|
|
|
|
if (_.indexOf(graphite_options, key) === -1) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-02 05:52:43 -06:00
|
|
|
if (value) {
|
|
|
|
clean_options.push(key + "=" + encodeURIComponent(value));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!hasTargets) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return clean_options;
|
|
|
|
};
|
|
|
|
}
|
2017-10-20 03:23:14 -05:00
|
|
|
|
|
|
|
function supportsTags(version: string): boolean {
|
2017-12-19 09:06:54 -06:00
|
|
|
return isVersionGtOrEq(version, "1.1");
|
2017-10-20 03:23:14 -05:00
|
|
|
}
|