mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(cloudwatch): lots of code refactoring and cleanup, #684, dimension values lookup works but seems to return all dimension values no matter what dimension key you select, removed strange formating of template dimension values query, should not return key value pair but only the dimension value
This commit is contained in:
parent
91285baea5
commit
04f4454974
@ -31,13 +31,13 @@ func ProxyCloudWatchDataSourceRequest(c *middleware.Context) {
|
|||||||
case "GetMetricStatistics":
|
case "GetMetricStatistics":
|
||||||
reqParam := &struct {
|
reqParam := &struct {
|
||||||
Parameters struct {
|
Parameters struct {
|
||||||
Namespace string `json:"Namespace"`
|
Namespace string `json:"namespace"`
|
||||||
MetricName string `json:"MetricName"`
|
MetricName string `json:"metricName"`
|
||||||
Dimensions []*cloudwatch.Dimension `json:"Dimensions"`
|
Dimensions []*cloudwatch.Dimension `json:"dimensions"`
|
||||||
Statistics []*string `json:"Statistics"`
|
Statistics []*string `json:"statistics"`
|
||||||
StartTime int64 `json:"StartTime"`
|
StartTime int64 `json:"startTime"`
|
||||||
EndTime int64 `json:"EndTime"`
|
EndTime int64 `json:"endTime"`
|
||||||
Period int64 `json:"Period"`
|
Period int64 `json:"period"`
|
||||||
} `json:"parameters"`
|
} `json:"parameters"`
|
||||||
}{}
|
}{}
|
||||||
json.Unmarshal([]byte(body), reqParam)
|
json.Unmarshal([]byte(body), reqParam)
|
||||||
@ -63,9 +63,9 @@ func ProxyCloudWatchDataSourceRequest(c *middleware.Context) {
|
|||||||
case "ListMetrics":
|
case "ListMetrics":
|
||||||
reqParam := &struct {
|
reqParam := &struct {
|
||||||
Parameters struct {
|
Parameters struct {
|
||||||
Namespace string `json:"Namespace"`
|
Namespace string `json:"namespace"`
|
||||||
MetricName string `json:"MetricName"`
|
MetricName string `json:"metricName"`
|
||||||
Dimensions []*cloudwatch.DimensionFilter `json:"Dimensions"`
|
Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"`
|
||||||
} `json:"parameters"`
|
} `json:"parameters"`
|
||||||
}{}
|
}{}
|
||||||
json.Unmarshal([]byte(body), reqParam)
|
json.Unmarshal([]byte(body), reqParam)
|
||||||
@ -94,8 +94,8 @@ func ProxyCloudWatchDataSourceRequest(c *middleware.Context) {
|
|||||||
case "DescribeInstances":
|
case "DescribeInstances":
|
||||||
reqParam := &struct {
|
reqParam := &struct {
|
||||||
Parameters struct {
|
Parameters struct {
|
||||||
Filters []*ec2.Filter `json:"Filters"`
|
Filters []*ec2.Filter `json:"filters"`
|
||||||
InstanceIds []*string `json:"InstanceIds"`
|
InstanceIds []*string `json:"instanceIds"`
|
||||||
} `json:"parameters"`
|
} `json:"parameters"`
|
||||||
}{}
|
}{}
|
||||||
json.Unmarshal([]byte(body), reqParam)
|
json.Unmarshal([]byte(body), reqParam)
|
||||||
|
@ -64,7 +64,7 @@ function (angular, $, config) {
|
|||||||
|
|
||||||
$scope.appEvent("dashboard-loaded", $scope.dashboard);
|
$scope.appEvent("dashboard-loaded", $scope.dashboard);
|
||||||
}).catch(function(err) {
|
}).catch(function(err) {
|
||||||
console.log('Failed to initialize dashboard', err);
|
if (err.data && err.data.message) { err.message = err.data.message; }
|
||||||
$scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]);
|
$scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,7 @@ function (angular, _) {
|
|||||||
|
|
||||||
var module = angular.module('grafana.controllers');
|
var module = angular.module('grafana.controllers');
|
||||||
|
|
||||||
module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv, alertSrv) {
|
module.controller('TemplateEditorCtrl', function($scope, datasourceSrv, templateSrv, templateValuesSrv) {
|
||||||
|
|
||||||
var replacementDefaults = {
|
var replacementDefaults = {
|
||||||
type: 'query',
|
type: 'query',
|
||||||
@ -78,9 +78,9 @@ function (angular, _) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
$scope.runQuery = function() {
|
$scope.runQuery = function() {
|
||||||
return templateValuesSrv.updateOptions($scope.current).then(function() {
|
return templateValuesSrv.updateOptions($scope.current).then(null, function(err) {
|
||||||
}, function(err) {
|
if (err.data && err.data.message) { err.message = err.data.message; }
|
||||||
alertSrv.set('Templating', 'Failed to run query for variable values: ' + err.message, 'error');
|
$scope.appEvent("alert-error", ['Templating', 'Template variables could not be initialized: ' + err.message]);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ function (angular, _) {
|
|||||||
|
|
||||||
var module = angular.module('grafana.services');
|
var module = angular.module('grafana.services');
|
||||||
|
|
||||||
module.factory('CloudWatchDatasource', function($q, $http, templateSrv) {
|
module.factory('CloudWatchDatasource', function($q, backendSrv, templateSrv) {
|
||||||
|
|
||||||
function CloudWatchDatasource(datasource) {
|
function CloudWatchDatasource(datasource) {
|
||||||
this.type = 'cloudwatch';
|
this.type = 'cloudwatch';
|
||||||
@ -173,7 +173,7 @@ function (angular, _) {
|
|||||||
return _.isEmpty(u);
|
return _.isEmpty(u);
|
||||||
})
|
})
|
||||||
.map(function(u) {
|
.map(function(u) {
|
||||||
return $http({ method: 'GET', url: u });
|
return backendSrv.datasourceRequest({ method: 'GET', url: u });
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.then(function(allResponse) {
|
.then(function(allResponse) {
|
||||||
@ -218,7 +218,7 @@ function (angular, _) {
|
|||||||
|
|
||||||
var queries = [];
|
var queries = [];
|
||||||
_.each(options.targets, _.bind(function(target) {
|
_.each(options.targets, _.bind(function(target) {
|
||||||
if (!target.namespace || !target.metricName || _.isEmpty(target.statistics)) {
|
if (target.hide || !target.namespace || !target.metricName || _.isEmpty(target.statistics)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,19 +263,20 @@ function (angular, _) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) {
|
CloudWatchDatasource.prototype.performTimeSeriesQuery = function(query, start, end) {
|
||||||
var cloudwatch = this.getAwsClient(query.region, 'CloudWatch');
|
return this.awsRequest({
|
||||||
|
region: query.region,
|
||||||
var params = {
|
service: 'CloudWatch',
|
||||||
Namespace: query.namespace,
|
action: 'GetMetricStatistics',
|
||||||
MetricName: query.metricName,
|
parameters: {
|
||||||
Dimensions: query.dimensions,
|
namespace: query.namespace,
|
||||||
Statistics: query.statistics,
|
metricName: query.metricName,
|
||||||
StartTime: start,
|
dimensions: query.dimensions,
|
||||||
EndTime: end,
|
statistics: query.statistics,
|
||||||
Period: query.period
|
startTime: start,
|
||||||
};
|
endTime: end,
|
||||||
|
period: query.period
|
||||||
return cloudwatch.getMetricStatistics(params);
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.getRegions = function() {
|
CloudWatchDatasource.prototype.getRegions = function() {
|
||||||
@ -297,38 +298,36 @@ function (angular, _) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
|
CloudWatchDatasource.prototype.getDimensionValues = function(region, namespace, metricName, dimensions) {
|
||||||
region = templateSrv.replace(region);
|
var request = {
|
||||||
namespace = templateSrv.replace(namespace);
|
region: templateSrv.replace(region),
|
||||||
metricName = templateSrv.replace(metricName);
|
service: 'CloudWatch',
|
||||||
|
action: 'ListMetrics',
|
||||||
var cloudwatch = this.getAwsClient(region, 'CloudWatch');
|
parameters: {
|
||||||
var params = {Namespace: namespace, MetricName: metricName};
|
namespace: templateSrv.replace(namespace),
|
||||||
|
metricName: templateSrv.replace(metricName),
|
||||||
if (!_.isEmpty(dimensions)) {
|
dimensions: convertDimensionFormat(dimensions),
|
||||||
params.Dimensions = convertDimensionFormat(dimensions);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return cloudwatch.listMetrics(params).then(function(result) {
|
return this.awsRequest(request).then(function(result) {
|
||||||
return _.chain(result.Metrics).map(function(metric) {
|
return _.chain(result.Metrics).map(function(metric) {
|
||||||
return metric.Dimensions;
|
return _.pluck(metric.Dimensions, 'Value');
|
||||||
}).reject(function(metric) {
|
}).flatten().uniq().sortBy(function(name) {
|
||||||
return _.isEmpty(metric);
|
return name;
|
||||||
}).value();
|
}).value();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
|
CloudWatchDatasource.prototype.performEC2DescribeInstances = function(region, filters, instanceIds) {
|
||||||
var ec2 = this.getAwsClient(region, 'EC2');
|
return this.awsRequest({
|
||||||
|
region: region,
|
||||||
var params = {};
|
service: 'EC2',
|
||||||
if (filters.length > 0) {
|
action: 'DescribeInstances',
|
||||||
params.Filters = filters;
|
parameters: {
|
||||||
|
filter: filters,
|
||||||
|
instanceIds: instanceIds
|
||||||
}
|
}
|
||||||
if (instanceIds.length > 0) {
|
});
|
||||||
params.InstanceIds = instanceIds;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ec2.describeInstances(params);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.metricFindQuery = function(query) {
|
CloudWatchDatasource.prototype.metricFindQuery = function(query) {
|
||||||
@ -382,21 +381,7 @@ function (angular, _) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getDimensionValues(region, namespace, metricName, dimensions)
|
return this.getDimensionValues(region, namespace, metricName, dimensions).then(transformSuggestData);
|
||||||
.then(function(suggestData) {
|
|
||||||
return _.map(suggestData, function(dimensions) {
|
|
||||||
var result = _.chain(dimensions)
|
|
||||||
.sortBy(function(dimension) {
|
|
||||||
return dimension.Name;
|
|
||||||
})
|
|
||||||
.map(function(dimension) {
|
|
||||||
return dimension.Name + '=' + dimension.Value;
|
|
||||||
})
|
|
||||||
.value().join(',');
|
|
||||||
|
|
||||||
return { text: result };
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
|
var ebsVolumeIdsQuery = query.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
|
||||||
@ -431,43 +416,17 @@ function (angular, _) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.getAwsClient = function(region, service) {
|
CloudWatchDatasource.prototype.awsRequest = function(data) {
|
||||||
var self = this;
|
|
||||||
var generateRequestProxy = function(service, action) {
|
|
||||||
return function(params) {
|
|
||||||
var data = {
|
|
||||||
region: region,
|
|
||||||
service: service,
|
|
||||||
action: action,
|
|
||||||
parameters: params
|
|
||||||
};
|
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: self.proxyUrl,
|
url: this.proxyUrl,
|
||||||
data: data
|
data: data
|
||||||
};
|
};
|
||||||
|
|
||||||
return $http(options).then(function(result) {
|
return backendSrv.datasourceRequest(options).then(function(result) {
|
||||||
return result.data;
|
return result.data;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
switch (service) {
|
|
||||||
case 'CloudWatch': {
|
|
||||||
return {
|
|
||||||
getMetricStatistics: generateRequestProxy('CloudWatch', 'GetMetricStatistics'),
|
|
||||||
listMetrics: generateRequestProxy('CloudWatch', 'ListMetrics')
|
|
||||||
};
|
|
||||||
}
|
|
||||||
case 'EC2': {
|
|
||||||
return {
|
|
||||||
describeInstances: generateRequestProxy('EC2', 'DescribeInstances')
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
CloudWatchDatasource.prototype.getDefaultRegion = function() {
|
CloudWatchDatasource.prototype.getDefaultRegion = function() {
|
||||||
return this.defaultRegion;
|
return this.defaultRegion;
|
||||||
|
@ -99,13 +99,7 @@ function (angular, _) {
|
|||||||
$scope.target.metricName,
|
$scope.target.metricName,
|
||||||
$scope.target.dimensions
|
$scope.target.dimensions
|
||||||
).then(function(result) {
|
).then(function(result) {
|
||||||
var suggestData = _.chain(result)
|
callback(result);
|
||||||
.flatten(true)
|
|
||||||
.pluck('Value')
|
|
||||||
.uniq()
|
|
||||||
.value();
|
|
||||||
|
|
||||||
callback(suggestData);
|
|
||||||
}, function() {
|
}, function() {
|
||||||
callback([]);
|
callback([]);
|
||||||
});
|
});
|
||||||
|
@ -10,7 +10,7 @@ describe('CloudWatchDatasource', function() {
|
|||||||
|
|
||||||
beforeEach(angularMocks.module('grafana.services'));
|
beforeEach(angularMocks.module('grafana.services'));
|
||||||
beforeEach(angularMocks.module('grafana.controllers'));
|
beforeEach(angularMocks.module('grafana.controllers'));
|
||||||
beforeEach(ctx.providePhase(['templateSrv']));
|
beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
|
||||||
beforeEach(ctx.createService('CloudWatchDatasource'));
|
beforeEach(ctx.createService('CloudWatchDatasource'));
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds = new ctx.service({
|
ctx.ds = new ctx.service({
|
||||||
@ -53,24 +53,21 @@ describe('CloudWatchDatasource', function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds.getAwsClient = function() {
|
ctx.backendSrv.datasourceRequest = function(params) {
|
||||||
return {
|
|
||||||
getMetricStatistics: function(params) {
|
|
||||||
requestParams = params;
|
requestParams = params;
|
||||||
return ctx.$q.when(response);
|
return ctx.$q.when({data: response});
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should generate the correct query', function(done) {
|
it('should generate the correct query', function(done) {
|
||||||
ctx.ds.query(query).then(function() {
|
ctx.ds.query(query).then(function() {
|
||||||
expect(requestParams.Namespace).to.be(query.targets[0].namespace);
|
var params = requestParams.data.parameters;
|
||||||
expect(requestParams.MetricName).to.be(query.targets[0].metricName);
|
expect(params.namespace).to.be(query.targets[0].namespace);
|
||||||
expect(requestParams.Dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
|
expect(params.metricName).to.be(query.targets[0].metricName);
|
||||||
expect(requestParams.Dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
|
expect(params.dimensions[0].Name).to.be(Object.keys(query.targets[0].dimensions)[0]);
|
||||||
expect(requestParams.Statistics).to.eql(Object.keys(query.targets[0].statistics));
|
expect(params.dimensions[0].Value).to.be(query.targets[0].dimensions[Object.keys(query.targets[0].dimensions)[0]]);
|
||||||
expect(requestParams.Period).to.be(query.targets[0].period);
|
expect(params.statistics).to.eql(Object.keys(query.targets[0].statistics));
|
||||||
|
expect(params.period).to.be(query.targets[0].period);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
ctx.$rootScope.$apply();
|
ctx.$rootScope.$apply();
|
||||||
@ -105,13 +102,9 @@ describe('CloudWatchDatasource', function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
ctx.ds.getAwsClient = function() {
|
ctx.backendSrv.datasourceRequest = function(params) {
|
||||||
return {
|
|
||||||
listMetrics: function(params) {
|
|
||||||
requestParams = params;
|
requestParams = params;
|
||||||
return ctx.$q.when(response);
|
return ctx.$q.when({data: response});
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -158,7 +151,7 @@ describe('CloudWatchDatasource', function() {
|
|||||||
var query = 'dimension_values(us-east-1,AWS/EC2,CPUUtilization)';
|
var query = 'dimension_values(us-east-1,AWS/EC2,CPUUtilization)';
|
||||||
ctx.ds.metricFindQuery(query).then(function(result) {
|
ctx.ds.metricFindQuery(query).then(function(result) {
|
||||||
result = result.map(function(v) { return v.text; });
|
result = result.map(function(v) { return v.text; });
|
||||||
expect(result).to.eql(['InstanceId=i-12345678']);
|
expect(result).to.eql(['i-12345678']);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
ctx.$rootScope.$apply();
|
ctx.$rootScope.$apply();
|
||||||
|
Loading…
Reference in New Issue
Block a user