Files
grafana/public/app/plugins/datasource/cloudwatch/datasource.js

413 lines
14 KiB
JavaScript
Raw Normal View History

2015-07-30 11:37:31 +09:00
define([
'angular',
'lodash',
'moment',
2015-10-28 12:14:06 +09:00
'app/core/utils/datemath',
'app/core/utils/kbn',
'app/features/templating/variable',
2015-07-30 11:37:31 +09:00
],
2017-09-25 18:16:40 +09:00
function (angular, _, moment, dateMath, kbn, templatingVariable) {
2015-07-30 11:37:31 +09:00
'use strict';
kbn = kbn.default;
/** @ngInject */
2017-09-10 04:24:39 +09:00
function CloudWatchDatasource(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
this.type = 'cloudwatch';
this.name = instanceSettings.name;
this.supportMetrics = true;
this.proxyUrl = instanceSettings.url;
this.defaultRegion = instanceSettings.jsonData.defaultRegion;
2017-04-03 21:50:40 +09:00
this.instanceSettings = instanceSettings;
this.standardStatistics = [
'Average',
'Maximum',
'Minimum',
'Sum',
'SampleCount'
];
2015-07-30 11:37:31 +09:00
2016-01-15 21:34:34 +09:00
var self = this;
this.query = function(options) {
2015-12-07 13:40:54 +09:00
options = angular.copy(options);
options.targets = this.expandTemplateVariable(options.targets, options.scopedVars, templateSrv);
2015-07-30 11:37:31 +09:00
2017-04-03 21:50:40 +09:00
var queries = _.filter(options.targets, function (item) {
2017-06-26 16:13:30 +09:00
return item.hide !== true &&
!!item.region &&
!!item.namespace &&
!!item.metricName &&
!_.isEmpty(item.statistics);
2017-04-03 21:50:40 +09:00
}).map(function (item) {
item.region = templateSrv.replace(self.getActualRegion(item.region), options.scopedVars);
2017-04-03 21:50:40 +09:00
item.namespace = templateSrv.replace(item.namespace, options.scopedVars);
item.metricName = templateSrv.replace(item.metricName, options.scopedVars);
2017-09-26 15:06:21 +09:00
item.dimensions = self.convertDimensionFormat(item.dimensions, options.scopeVars);
item.period = String(self.getPeriod(item, options)); // use string format for period in graph query, and alerting
2017-04-03 21:50:40 +09:00
2017-09-24 12:25:52 +09:00
return _.extend({
2017-04-03 21:50:40 +09:00
refId: item.refId,
intervalMs: options.intervalMs,
maxDataPoints: options.maxDataPoints,
datasourceId: self.instanceSettings.id,
type: 'timeSeriesQuery',
2017-09-24 12:25:52 +09:00
}, item);
2017-04-03 21:50:40 +09:00
});
2015-07-30 11:37:31 +09:00
// No valid targets, return the empty result to save a round trip.
if (_.isEmpty(queries)) {
var d = $q.defer();
d.resolve({ data: [] });
return d.promise;
}
2017-04-03 21:50:40 +09:00
var request = {
2017-09-26 15:25:49 +09:00
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
2017-04-03 21:50:40 +09:00
queries: queries
};
2017-04-03 21:50:40 +09:00
return this.performTimeSeriesQuery(request);
2015-07-30 11:37:31 +09:00
};
2017-08-16 16:40:46 +09:00
this.getPeriod = function(target, options, now) {
2017-04-03 21:50:40 +09:00
var start = this.convertToCloudWatchTime(options.range.from, false);
var end = this.convertToCloudWatchTime(options.range.to, true);
2017-08-16 16:40:46 +09:00
now = Math.round((now || Date.now()) / 1000);
2017-04-03 21:50:40 +09:00
2016-10-03 10:31:07 +02:00
var period;
var range = end - start;
var hourSec = 60 * 60;
var daySec = hourSec * 24;
var periodUnit = 60;
if (!target.period) {
if (now - start <= (daySec * 15)) { // until 15 days ago
2017-04-03 21:50:40 +09:00
if (target.namespace === 'AWS/EC2') {
periodUnit = period = 300;
} else {
periodUnit = period = 60;
}
} else if (now - start <= (daySec * 63)) { // until 63 days ago
periodUnit = period = 60 * 5;
} else if (now - start <= (daySec * 455)) { // until 455 days ago
periodUnit = period = 60 * 60;
} else { // over 455 days, should return error, but try to long period
periodUnit = period = 60 * 60;
}
2016-10-03 10:31:07 +02:00
} else {
if (/^\d+$/.test(target.period)) {
period = parseInt(target.period, 10);
} else {
period = kbn.interval_to_seconds(templateSrv.replace(target.period, options.scopedVars));
}
2016-10-03 10:31:07 +02:00
}
if (period < 1) {
period = 1;
2016-10-03 10:31:07 +02:00
}
if (range / period >= 1440) {
period = Math.ceil(range / 1440 / periodUnit) * periodUnit;
2016-10-03 10:31:07 +02:00
}
return period;
};
2017-04-03 21:50:40 +09:00
this.performTimeSeriesQuery = function(request) {
2017-11-12 00:41:14 +09:00
return this.awsRequest('/api/tsdb/query', request).then(function (res) {
2017-04-03 21:50:40 +09:00
var data = [];
if (res.results) {
_.forEach(res.results, function (queryRes) {
_.forEach(queryRes.series, function (series) {
data.push({target: series.name, datapoints: series.points});
});
});
}
2017-04-03 21:50:40 +09:00
return {data: data};
});
2015-07-30 11:37:31 +09:00
};
2017-09-13 19:35:05 +09:00
function transformSuggestDataFromTable(suggestData) {
return _.map(suggestData.results['metricFindQuery'].tables[0].rows, function (v) {
return {
text: v[0],
value: v[1]
};
});
}
2017-09-20 12:36:48 +09:00
this.doMetricQueryRequest = function (subtype, parameters) {
2017-09-10 04:24:39 +09:00
var range = timeSrv.timeRange();
2017-11-12 00:41:14 +09:00
return this.awsRequest('/api/tsdb/query', {
2017-09-26 15:25:49 +09:00
from: range.from.valueOf().toString(),
to: range.to.valueOf().toString(),
2017-09-10 04:24:39 +09:00
queries: [
2017-09-24 12:25:52 +09:00
_.extend({
2017-09-10 04:24:39 +09:00
refId: 'metricFindQuery',
intervalMs: 1, // dummy
maxDataPoints: 1, // dummy
datasourceId: this.instanceSettings.id,
type: 'metricFindQuery',
2017-09-24 12:25:52 +09:00
subtype: subtype
}, parameters)
2017-09-10 04:24:39 +09:00
]
2017-09-13 19:35:05 +09:00
}).then(function (r) { return transformSuggestDataFromTable(r); });
2015-08-11 10:25:25 +09:00
};
2017-09-20 12:36:48 +09:00
this.getRegions = function () {
return this.doMetricQueryRequest('regions', null);
};
this.getNamespaces = function() {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('namespaces', null);
2015-08-11 10:25:25 +09:00
};
2017-09-13 19:35:05 +09:00
this.getMetrics = function (namespace, region) {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('metrics', {
region: templateSrv.replace(this.getActualRegion(region)),
2017-09-20 12:36:48 +09:00
namespace: templateSrv.replace(namespace)
});
2015-08-11 10:25:25 +09:00
};
2016-01-14 19:02:04 +09:00
this.getDimensionKeys = function(namespace, region) {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('dimension_keys', {
region: templateSrv.replace(this.getActualRegion(region)),
2017-09-20 12:36:48 +09:00
namespace: templateSrv.replace(namespace)
});
2015-08-11 10:25:25 +09:00
};
this.getDimensionValues = function(region, namespace, metricName, dimensionKey, filterDimensions) {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('dimension_values', {
region: templateSrv.replace(this.getActualRegion(region)),
2017-09-20 12:36:48 +09:00
namespace: templateSrv.replace(namespace),
metricName: templateSrv.replace(metricName),
dimensionKey: templateSrv.replace(dimensionKey),
dimensions: this.convertDimensionFormat(filterDimensions, {}),
});
2015-07-30 11:37:31 +09:00
};
2017-09-19 18:09:57 +09:00
this.getEbsVolumeIds = function(region, instanceId) {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('ebs_volume_ids', {
region: templateSrv.replace(this.getActualRegion(region)),
2017-09-20 12:47:36 +09:00
instanceId: templateSrv.replace(instanceId)
2017-09-20 12:36:48 +09:00
});
2017-09-19 18:09:57 +09:00
};
2017-09-19 18:55:11 +09:00
this.getEc2InstanceAttribute = function(region, attributeName, filters) {
2017-09-20 12:36:48 +09:00
return this.doMetricQueryRequest('ec2_instance_attribute', {
region: templateSrv.replace(this.getActualRegion(region)),
2017-09-20 12:47:36 +09:00
attributeName: templateSrv.replace(attributeName),
2017-09-20 12:36:48 +09:00
filters: filters
});
2017-09-19 18:55:11 +09:00
};
this.metricFindQuery = function(query) {
2015-08-11 00:09:25 +09:00
var region;
var namespace;
var metricName;
var regionQuery = query.match(/^regions\(\)/);
2015-08-11 00:09:25 +09:00
if (regionQuery) {
2017-09-13 19:35:05 +09:00
return this.getRegions();
2015-08-11 00:09:25 +09:00
}
var namespaceQuery = query.match(/^namespaces\(\)/);
2015-08-11 00:09:25 +09:00
if (namespaceQuery) {
return this.getNamespaces();
2015-08-11 00:09:25 +09:00
}
2016-01-14 19:02:04 +09:00
var metricNameQuery = query.match(/^metrics\(([^\)]+?)(,\s?([^,]+?))?\)/);
2015-08-11 00:09:25 +09:00
if (metricNameQuery) {
2017-09-20 12:47:36 +09:00
namespace = metricNameQuery[1];
region = metricNameQuery[3];
return this.getMetrics(namespace, region);
2015-08-11 00:09:25 +09:00
}
2016-01-14 19:02:04 +09:00
var dimensionKeysQuery = query.match(/^dimension_keys\(([^\)]+?)(,\s?([^,]+?))?\)/);
2015-08-11 00:09:25 +09:00
if (dimensionKeysQuery) {
2017-09-20 12:47:36 +09:00
namespace = dimensionKeysQuery[1];
region = dimensionKeysQuery[3];
return this.getDimensionKeys(namespace, region);
2015-08-11 00:09:25 +09:00
}
2015-12-04 01:02:25 +09:00
var dimensionValuesQuery = query.match(/^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/);
2015-08-11 00:09:25 +09:00
if (dimensionValuesQuery) {
2017-09-20 12:47:36 +09:00
region = dimensionValuesQuery[1];
namespace = dimensionValuesQuery[2];
metricName = dimensionValuesQuery[3];
var dimensionKey = dimensionValuesQuery[4];
2015-12-04 01:02:25 +09:00
return this.getDimensionValues(region, namespace, metricName, dimensionKey, {});
2015-08-11 00:09:25 +09:00
}
2015-09-17 18:07:40 +09:00
var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
if (ebsVolumeIdsQuery) {
2017-09-20 12:47:36 +09:00
region = ebsVolumeIdsQuery[1];
var instanceId = ebsVolumeIdsQuery[2];
2017-09-19 18:09:57 +09:00
return this.getEbsVolumeIds(region, instanceId);
2015-09-17 18:07:40 +09:00
}
var ec2InstanceAttributeQuery = query.match(/^ec2_instance_attribute\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
if (ec2InstanceAttributeQuery) {
2017-09-20 12:47:36 +09:00
region = ec2InstanceAttributeQuery[1];
var targetAttributeName = ec2InstanceAttributeQuery[2];
2017-09-19 18:55:11 +09:00
var filterJson = JSON.parse(templateSrv.replace(ec2InstanceAttributeQuery[3]));
return this.getEc2InstanceAttribute(region, targetAttributeName, filterJson);
}
2015-08-11 00:09:25 +09:00
return $q.when([]);
};
2017-09-25 18:16:40 +09:00
this.annotationQuery = function (options) {
var annotation = options.annotation;
2017-09-26 15:45:52 +09:00
var statistics = _.map(annotation.statistics, function (s) { return templateSrv.replace(s); });
2017-09-25 18:16:40 +09:00
var defaultPeriod = annotation.prefixMatching ? '' : '300';
var period = annotation.period || defaultPeriod;
period = parseInt(period, 10);
var parameters = {
prefixMatching: annotation.prefixMatching,
region: templateSrv.replace(this.getActualRegion(annotation.region)),
2017-09-25 18:16:40 +09:00
namespace: templateSrv.replace(annotation.namespace),
metricName: templateSrv.replace(annotation.metricName),
2017-09-26 15:06:21 +09:00
dimensions: this.convertDimensionFormat(annotation.dimensions, {}),
statistics: statistics,
2017-09-25 18:16:40 +09:00
period: period,
actionPrefix: annotation.actionPrefix || '',
alarmNamePrefix: annotation.alarmNamePrefix || ''
};
2015-10-26 15:47:34 +09:00
2017-11-12 00:41:14 +09:00
return this.awsRequest('/api/tsdb/query', {
2017-09-26 15:25:49 +09:00
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
2017-09-25 18:16:40 +09:00
queries: [
_.extend({
refId: 'annotationQuery',
intervalMs: 1, // dummy
maxDataPoints: 1, // dummy
datasourceId: this.instanceSettings.id,
type: 'annotationQuery'
}, parameters)
]
}).then(function (r) {
return _.map(r.results['annotationQuery'].tables[0].rows, function (v) {
return {
annotation: annotation,
time: Date.parse(v[0]),
title: v[1],
tags: [v[2]],
text: v[3]
};
});
2015-10-26 15:47:34 +09:00
});
};
2017-09-29 13:24:18 +09:00
this.targetContainsTemplate = function(target) {
return templateSrv.variableExists(target.region) ||
templateSrv.variableExists(target.namespace) ||
templateSrv.variableExists(target.metricName) ||
_.find(target.dimensions, function(v, k) {
return templateSrv.variableExists(k) || templateSrv.variableExists(v);
});
};
this.testDatasource = function() {
2015-08-11 10:25:25 +09:00
/* use billing metrics for test */
var region = this.defaultRegion;
2015-08-11 10:25:25 +09:00
var namespace = 'AWS/Billing';
var metricName = 'EstimatedCharges';
var dimensions = {};
2015-11-17 22:49:46 +09:00
return this.getDimensionValues(region, namespace, metricName, 'ServiceName', dimensions).then(function () {
return { status: 'success', message: 'Data source is working' };
}, function (err) {
return { status: 'error', message: err.message };
2015-07-30 11:37:31 +09:00
});
};
2017-11-12 00:41:14 +09:00
this.awsRequest = function(url, data) {
var options = {
method: 'POST',
2017-11-12 00:41:14 +09:00
url: url,
data: data
};
2015-08-13 21:20:47 +09:00
return backendSrv.datasourceRequest(options).then(function(result) {
return result.data;
});
2015-08-10 23:15:25 +09:00
};
this.getDefaultRegion = function() {
2015-08-10 23:15:25 +09:00
return this.defaultRegion;
};
this.getActualRegion = function(region) {
if (region === 'default' || _.isEmpty(region)) {
return this.getDefaultRegion();
}
return region;
};
this.getExpandedVariables = function(target, dimensionKey, variable, templateSrv) {
/* if the all checkbox is marked we should add all values to the targets */
var allSelected = _.find(variable.options, {'selected': true, 'text': 'All'});
2016-09-06 01:22:39 +09:00
return _.chain(variable.options)
.filter(function(v) {
if (allSelected) {
return v.text !== 'All';
} else {
return v.selected;
}
2016-09-06 01:22:39 +09:00
})
.map(function(v) {
var t = angular.copy(target);
var scopedVar = {};
scopedVar[variable.name] = v;
2017-10-17 18:11:21 +09:00
t.refId = target.refId + '_' + v.value;
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
2016-09-06 01:22:39 +09:00
return t;
}).value();
};
this.expandTemplateVariable = function(targets, scopedVars, templateSrv) {
2016-09-06 01:22:39 +09:00
var self = this;
return _.chain(targets)
.map(function(target) {
2016-09-06 01:51:10 +09:00
var dimensionKey = _.findKey(target.dimensions, function(v) {
return templateSrv.variableExists(v) && !_.has(scopedVars, templateSrv.getVariableName(v));
});
2016-09-06 01:51:10 +09:00
if (dimensionKey) {
var multiVariable = _.find(templateSrv.variables, function(variable) {
return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name) && variable.multi;
});
var variable = _.find(templateSrv.variables, function(variable) {
return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name);
});
return self.getExpandedVariables(target, dimensionKey, multiVariable || variable, templateSrv);
} else {
return [target];
}
}).flatten().value();
};
2016-01-15 21:34:34 +09:00
this.convertToCloudWatchTime = function(date, roundUp) {
2015-10-28 12:14:06 +09:00
if (_.isString(date)) {
date = dateMath.parse(date, roundUp);
}
2015-09-26 02:15:27 +09:00
return Math.round(date.valueOf() / 1000);
2016-01-15 21:34:34 +09:00
};
2015-07-30 11:37:31 +09:00
2016-01-15 21:34:34 +09:00
this.convertDimensionFormat = function(dimensions, scopedVars) {
2017-09-26 15:06:21 +09:00
var convertedDimensions = {};
_.each(dimensions, function (value, key) {
convertedDimensions[templateSrv.replace(key, scopedVars)] = templateSrv.replace(value, scopedVars);
2015-08-25 15:45:09 +09:00
});
2017-09-26 15:06:21 +09:00
return convertedDimensions;
2016-01-15 21:34:34 +09:00
};
2015-08-25 15:45:09 +09:00
}
2015-07-30 11:37:31 +09:00
return CloudWatchDatasource;
2015-07-30 11:37:31 +09:00
});