-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var AWS = require('aws-sdk');
var singleton = module.exports = function(config) {
if (singleton.cloudwatch) {
return;
}
singleton.namespace = config.namespace;
singleton.cloudwatch = new AWS.CloudWatch({region: config.region});
singleton.metrics = config.metrics;
singleton.put = put;
};
function put(metricName) {
if (metricName == null) {
throw new Error('No metricname found');
}
if (singleton.metrics[metricName] == null) {
throw new Error("Invalid metricName received : '" + metricName + "'");
}
var value = arguments[1] || 0;
var dimensions = arguments[2] || [];
var promise = new Promise(function(resolve, reject) {
wrapper(metricName, value, dimensions, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
return promise;
}
function wrapper(metric, value, dimensions, callback) {
var data = {
MetricName: metric,
Timestamp: new Date(),
Unit: singleton.metrics[metric].unit
};
if (typeof(value) === 'number') {
data.Value = value;
} else if (typeof(value) === 'object') {
data.StatisticValues = value;
} else {
throw new Error('Invalid value format ' + value);
}
singleton.cloudwatch.putMetricData({
MetricData: [data],
Namespace: singleton.namespace
}, callback);
}