mirror of https://github.com/grafana/grafana
Closes #35, grafana now supports multiple graphite backends. Check config.sample for how to setup. In the general tab of the graphite panel you can change datasource
parent
39bbd5604d
commit
36f56f3695
@ -0,0 +1,34 @@ |
||||
define([ |
||||
'angular', |
||||
'underscore', |
||||
'config', |
||||
'./graphite/graphiteDatasource' |
||||
], |
||||
function (angular, _, config, GraphiteDatasource) { |
||||
'use strict'; |
||||
|
||||
var module = angular.module('kibana.services'); |
||||
|
||||
module.service('datasourceSrv', function($q, filterSrv, $http) { |
||||
var defaultDatasource = _.findWhere(_.values(config.datasources), { default: true } ); |
||||
|
||||
this.default = new GraphiteDatasource(defaultDatasource, $q, filterSrv, $http); |
||||
|
||||
this.get = function(name) { |
||||
if (!name) { |
||||
return this.default; |
||||
} |
||||
|
||||
return new GraphiteDatasource(config.datasources[name], $q, filterSrv, $http); |
||||
}; |
||||
|
||||
this.listOptions = function() { |
||||
return _.map(config.datasources, function(value, key) { |
||||
return { |
||||
name: value.default ? key + ' (default)' : key, |
||||
value: value.default ? null : key |
||||
}; |
||||
}); |
||||
}; |
||||
}); |
||||
}); |
@ -0,0 +1,150 @@ |
||||
define([ |
||||
'angular', |
||||
'underscore', |
||||
'jquery', |
||||
'config', |
||||
'kbn', |
||||
'moment' |
||||
], |
||||
function (angular, _, $, config, kbn, moment) { |
||||
'use strict'; |
||||
|
||||
function GraphiteDatasource(datasource, $q, filterSrv, $http) { |
||||
this.url = datasource.url; |
||||
this.type = 'graphite'; |
||||
this.basicAuth = datasource.basicAuth; |
||||
this.$q = $q; |
||||
this.filterSrv = filterSrv; |
||||
this.$http = $http; |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.query = function(options) { |
||||
try { |
||||
var graphOptions = { |
||||
from: this.translateTime(options.range.from), |
||||
until: this.translateTime(options.range.to), |
||||
targets: options.targets, |
||||
format: options.format, |
||||
maxDataPoints: options.maxDataPoints, |
||||
}; |
||||
|
||||
var params = this.buildGraphiteParams(graphOptions); |
||||
|
||||
if (options.format === 'png') { |
||||
return $q.when(graphiteRenderUrl + '?' + params.join('&')); |
||||
} |
||||
|
||||
return this.doGraphiteRequest({ |
||||
method: 'POST', |
||||
url: '/render', |
||||
datasource: options.datasource, |
||||
data: params.join('&'), |
||||
headers: { |
||||
'Content-Type': 'application/x-www-form-urlencoded', |
||||
} |
||||
}); |
||||
} |
||||
catch(err) { |
||||
return this.$q.reject(err); |
||||
} |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.translateTime = function(date) { |
||||
if (_.isString(date)) { |
||||
if (date === 'now') { |
||||
return 'now'; |
||||
} |
||||
else if (date.indexOf('now') >= 0) { |
||||
date = date.substring(3); |
||||
date = date.replace('m', 'min'); |
||||
date = date.replace('M', 'mon'); |
||||
return date; |
||||
} |
||||
|
||||
date = kbn.parseDate(date); |
||||
} |
||||
|
||||
date = moment.utc(date).local(); |
||||
|
||||
if (config.timezoneOffset) { |
||||
date = date.zone(config.timezoneOffset); |
||||
} |
||||
|
||||
return date.format('HH:mm_YYYYMMDD'); |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.metricFindQuery = function(query) { |
||||
var interpolated; |
||||
try { |
||||
interpolated = this.filterSrv.applyFilterToTarget(query); |
||||
} |
||||
catch(err) { |
||||
return $q.reject(err); |
||||
} |
||||
|
||||
return this.doGraphiteRequest({method: 'GET', url: '/metrics/find/?query=' + interpolated }) |
||||
.then(function(results) { |
||||
return _.map(results.data, function(metric) { |
||||
return { |
||||
text: metric.text, |
||||
expandable: metric.expandable ? true : false |
||||
}; |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.listDashboards = function(query) { |
||||
return this.doGraphiteRequest({ method: 'GET', url: '/dashboard/find/', params: {query: query || ''} }) |
||||
.then(function(results) { |
||||
return results.data.dashboards; |
||||
}); |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.loadDashboard = function(dashName) { |
||||
return this.doGraphiteRequest({method: 'GET', url: '/dashboard/load/' + encodeURIComponent(dashName) }); |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.doGraphiteRequest = function(options) { |
||||
if (this.basicAuth) { |
||||
options.withCredentials = true; |
||||
options.headers = options.headers || {}; |
||||
options.headers.Authorization = 'Basic ' + config.graphiteBasicAuth; |
||||
} |
||||
|
||||
options.url = this.url + options.url; |
||||
|
||||
return this.$http(options); |
||||
}; |
||||
|
||||
GraphiteDatasource.prototype.buildGraphiteParams = function(options) { |
||||
var clean_options = []; |
||||
var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints']; |
||||
|
||||
if (options.format !== 'png') { |
||||
options['format'] = 'json'; |
||||
} |
||||
|
||||
_.each(options, function (value, key) { |
||||
if ($.inArray(key, graphite_options) === -1) { |
||||
return; |
||||
} |
||||
|
||||
if (key === "targets") { |
||||
_.each(value, function (value) { |
||||
if (!value.hide) { |
||||
var targetValue = this.filterSrv.applyFilterToTarget(value.target); |
||||
clean_options.push("target=" + encodeURIComponent(targetValue)); |
||||
} |
||||
}, this); |
||||
} |
||||
else if (value !== null) { |
||||
clean_options.push(key + "=" + encodeURIComponent(value)); |
||||
} |
||||
}, this); |
||||
return clean_options; |
||||
}; |
||||
|
||||
|
||||
return GraphiteDatasource; |
||||
|
||||
}); |
@ -1,161 +0,0 @@ |
||||
define([ |
||||
'angular', |
||||
'underscore', |
||||
'jquery', |
||||
'config', |
||||
'kbn', |
||||
'moment' |
||||
], |
||||
function (angular, _, $, config, kbn, moment) { |
||||
'use strict'; |
||||
|
||||
var module = angular.module('kibana.services'); |
||||
|
||||
module.service('graphiteSrv', function($http, $q, filterSrv) { |
||||
var graphiteRenderUrl = config.graphiteUrl + "/render"; |
||||
|
||||
this.query = function(options) { |
||||
try { |
||||
var graphOptions = { |
||||
from: this.translateTime(options.range.from), |
||||
until: this.translateTime(options.range.to), |
||||
targets: options.targets, |
||||
format: options.format, |
||||
maxDataPoints: options.maxDataPoints |
||||
}; |
||||
|
||||
var params = buildGraphiteParams(graphOptions); |
||||
|
||||
if (options.format === 'png') { |
||||
return $q.when(graphiteRenderUrl + '?' + params.join('&')); |
||||
} |
||||
|
||||
return doGraphiteRequest({ |
||||
method: 'POST', |
||||
url: graphiteRenderUrl, |
||||
data: params.join('&'), |
||||
headers: { |
||||
'Content-Type': 'application/x-www-form-urlencoded', |
||||
} |
||||
}); |
||||
} |
||||
catch(err) { |
||||
return $q.reject(err); |
||||
} |
||||
}; |
||||
|
||||
this.translateTime = function(date) { |
||||
if (_.isString(date)) { |
||||
if (date === 'now') { |
||||
return 'now'; |
||||
} |
||||
else if (date.indexOf('now') >= 0) { |
||||
date = date.substring(3); |
||||
date = date.replace('m', 'min'); |
||||
date = date.replace('M', 'mon'); |
||||
return date; |
||||
} |
||||
|
||||
date = kbn.parseDate(date); |
||||
} |
||||
|
||||
date = moment.utc(date).local(); |
||||
|
||||
if (config.timezoneOffset) { |
||||
date = date.zone(config.timezoneOffset); |
||||
} |
||||
|
||||
return date.format('HH:mm_YYYYMMDD'); |
||||
}; |
||||
|
||||
this.match = function(targets, graphiteTargetStr) { |
||||
var found = targets[0]; |
||||
|
||||
for (var i = 0; i < targets.length; i++) { |
||||
if (targets[i].target === graphiteTargetStr) { |
||||
found = targets[i]; |
||||
break; |
||||
} |
||||
if(targets[i].target.match("'" + graphiteTargetStr + "'")) { |
||||
found = targets[i]; |
||||
} |
||||
} |
||||
|
||||
return found; |
||||
}; |
||||
|
||||
this.metricFindQuery = function(query) { |
||||
var interpolated; |
||||
try { |
||||
interpolated = filterSrv.applyFilterToTarget(query); |
||||
} |
||||
catch(err) { |
||||
return $q.reject(err); |
||||
} |
||||
|
||||
var url = config.graphiteUrl + '/metrics/find/?query=' + interpolated; |
||||
return doGraphiteRequest({method: 'GET', url: url}) |
||||
.then(function(results) { |
||||
return _.map(results.data, function(metric) { |
||||
return { |
||||
text: metric.text, |
||||
expandable: metric.expandable ? true : false |
||||
}; |
||||
}); |
||||
}); |
||||
}; |
||||
|
||||
this.listDashboards = function(query) { |
||||
var url = config.graphiteUrl + '/dashboard/find/'; |
||||
return doGraphiteRequest({ method: 'GET', url: url, params: {query: query || ''} }) |
||||
.then(function(results) { |
||||
return results.data.dashboards; |
||||
}); |
||||
}; |
||||
|
||||
this.loadDashboard = function(dashName) { |
||||
var url = config.graphiteUrl + '/dashboard/load/' + encodeURIComponent(dashName); |
||||
return doGraphiteRequest({method: 'GET', url: url}); |
||||
}; |
||||
|
||||
function doGraphiteRequest(options) { |
||||
if (config.graphiteBasicAuth) { |
||||
options.withCredentials = true; |
||||
options.headers = options.headers || {}; |
||||
options.headers.Authorization = 'Basic ' + config.graphiteBasicAuth; |
||||
} |
||||
|
||||
return $http(options); |
||||
} |
||||
|
||||
function buildGraphiteParams(options) { |
||||
var clean_options = []; |
||||
var graphite_options = ['target', 'targets', 'from', 'until', 'rawData', 'format', 'maxDataPoints']; |
||||
|
||||
if (options.format !== 'png') { |
||||
options['format'] = 'json'; |
||||
} |
||||
|
||||
$.each(options, function (key, value) { |
||||
if ($.inArray(key, graphite_options) === -1) { |
||||
return; |
||||
} |
||||
|
||||
if (key === "targets") { |
||||
$.each(value, function (index, value) { |
||||
if (!value.hide) { |
||||
var targetValue = filterSrv.applyFilterToTarget(value.target); |
||||
clean_options.push("target=" + encodeURIComponent(targetValue)); |
||||
} |
||||
}); |
||||
} |
||||
else if (value !== null) { |
||||
clean_options.push(key + "=" + encodeURIComponent(value)); |
||||
} |
||||
}); |
||||
return clean_options; |
||||
} |
||||
|
||||
}); |
||||
|
||||
}); |
Loading…
Reference in new issue