|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var aws = require('aws-sdk'), |
| 4 | + cloudWatch = new aws.CloudWatch(), |
| 5 | + constants = { |
| 6 | + PLUGIN_NAME: 'cloudwatch', |
| 7 | + PLUGIN_PARAM_NAMESPACE: 'namespace', |
| 8 | + // PLUGIN_PARAM_METRICS: 'metrics', |
| 9 | + THE: 'The "', |
| 10 | + CONFIG_REQUIRED: '" plugin requires configuration under [script].config.plugins.', |
| 11 | + PARAM_REQUIRED: '" parameter is required', |
| 12 | + PARAM_MUST_BE_STRING: '" param must have a string value', |
| 13 | + PARAM_MUST_BE_ARRAY: '" param must have an array value', |
| 14 | + // Report Array Positions |
| 15 | + TIMESTAMP: 0, |
| 16 | + REQUEST_ID: 1, |
| 17 | + LATENCY: 2, |
| 18 | + STATUS_CODE: 3 |
| 19 | + }, |
| 20 | + messages = { |
| 21 | + pluginConfigRequired: constants.THE + constants.PLUGIN_NAME + constants.CONFIG_REQUIRED + constants.PLUGIN_NAME, |
| 22 | + pluginParamNamespaceRequired: constants.THE + constants.PLUGIN_PARAM_NAMESPACE + constants.PARAM_REQUIRED, |
| 23 | + pluginParamNamespaceMustBeString: constants.THE + constants.PLUGIN_PARAM_NAMESPACE + constants.PARAM_MUST_BE_STRING//, |
| 24 | + // pluginParamMetricsRequired: constants.THE + constants.PLUGIN_PARAM_METRICS + constants.PARAM_REQUIRED, |
| 25 | + // pluginParamMetricsMustBeArray: constants.THE + constants.PLUGIN_PARAM_METRICS + constants.PARAM_MUST_BE_ARRAY |
| 26 | + }, |
| 27 | + impl = { |
| 28 | + validateConfig: function(scriptConfig) { |
| 29 | + // Validate that plugin config exists |
| 30 | + if (!(scriptConfig && scriptConfig.plugins && constants.PLUGIN_NAME in scriptConfig.plugins)) { |
| 31 | + throw new Error(messages.pluginConfigRequired); |
| 32 | + } |
| 33 | + // Validate NAMESPACE |
| 34 | + if (!(constants.PLUGIN_PARAM_NAMESPACE in scriptConfig.plugins[constants.PLUGIN_NAME])) { |
| 35 | + throw new Error(messages.pluginParamNamespaceRequired); |
| 36 | + } else if (!('string' === typeof scriptConfig.plugins[constants.PLUGIN_NAME][constants.PLUGIN_PARAM_NAMESPACE] || |
| 37 | + scriptConfig.plugins[constants.PLUGIN_NAME][constants.PLUGIN_PARAM_NAMESPACE] instanceof String)) { |
| 38 | + throw new Error(messages.pluginParamNamespaceMustBeString); |
| 39 | + } |
| 40 | + // // Validate METRICS |
| 41 | + // if (!(messages.PLUGIN_PARAM_METRICS in pluginConfig)) { |
| 42 | + // throw new Error(messages.pluginParamMetricsRequired) |
| 43 | + // } else if (!Array.isArray(pluginConfig[messages.PLUGIN_PARAM_METRICS])) { |
| 44 | + // throw new Error(messages.pluginParamMetricsMustBeArray); |
| 45 | + // } |
| 46 | + // for(var i = 0; pluginConfig[messages.PLUGIN_PARAM_METRICS].length; i++) { |
| 47 | + // validateMetric(pluginConfig[messages.PLUGIN_PARAM_METRICS][i]); |
| 48 | + // } |
| 49 | + }, |
| 50 | + buildCloudWatchParams: function(namespace, latency, latencies) { |
| 51 | + var cloudWatchParams = { |
| 52 | + Namespace: namespace, |
| 53 | + MetricData: [] |
| 54 | + }, |
| 55 | + lastLatency = Math.min(latency + 20, latencies.length); |
| 56 | + for(var i = latency; i < lastLatency; i++) { |
| 57 | + cloudWatchParams.MetricData.push({ |
| 58 | + MetricName: 'ResultLatency', |
| 59 | + Dimensions: [], |
| 60 | + Timestamp: (new Date(latencies[i][constants.TIMESTAMP])).toISOString(), |
| 61 | + Value: latencies[i][constants.LATENCY] / 1000000, |
| 62 | + Unit: 'Milliseconds' |
| 63 | + }); |
| 64 | + } |
| 65 | + return cloudWatchParams; |
| 66 | + } |
| 67 | + }, |
| 68 | + api = { |
| 69 | + init: function(scriptConfig, eventEmitter) { |
| 70 | + return new CloudWatchPlugin(scriptConfig, eventEmitter); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | +function CloudWatchPlugin(scriptConfig, eventEmitter) { |
| 75 | + var self = this; |
| 76 | + impl.validateConfig(scriptConfig); |
| 77 | + self.config = JSON.parse(JSON.stringify(scriptConfig.plugins[constants.PLUGIN_NAME])); |
| 78 | + eventEmitter.on('done', function(report) { |
| 79 | + var latency = 0, |
| 80 | + latencies = report.aggregate.latencies, |
| 81 | + cloudWatchParams; |
| 82 | + while(latency < latencies.length) { |
| 83 | + cloudWatchParams = impl.buildCloudWatchParams(self.config[constants.PLUGIN_PARAM_NAMESPACE], latency, latencies); |
| 84 | + cloudWatch.putMetricData(cloudWatchParams, function (err) { |
| 85 | + if (err) { |
| 86 | + console.log('Error reporting metrics to CloudWatch via putMetricData:', err); |
| 87 | + } |
| 88 | + }); |
| 89 | + latency += cloudWatchParams.MetricData.length; |
| 90 | + } |
| 91 | + console.log('Metrics reported to CloudWatch'); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +CloudWatchPlugin.prototype.report = function() { |
| 96 | + var reports = null; // an array, if returning any reports |
| 97 | + // build reports and add them to reports |
| 98 | + // enact custom reporting actions |
| 99 | + return reports; |
| 100 | +}; |
| 101 | + |
| 102 | +/** |
| 103 | + * Configuration: |
| 104 | + * { |
| 105 | + * "config": { |
| 106 | + * "plugins": { |
| 107 | + * "cloudwatch": { |
| 108 | + * "namespace": "[INSERT_NAMESPACE]", |
| 109 | + // * "metrics": [ |
| 110 | + // * { |
| 111 | + // * "name": "[METRIC_NAME]", |
| 112 | + // * "dimensions": [...], |
| 113 | + // * |
| 114 | + // * } |
| 115 | + // * ] |
| 116 | + * } |
| 117 | + * } |
| 118 | + * } |
| 119 | + * } |
| 120 | + */ |
| 121 | +module.exports = api.init; |
| 122 | + |
| 123 | +/* test-code */ |
| 124 | +module.exports.constants = constants; |
| 125 | +module.exports.messages = messages; |
| 126 | +module.exports.impl = impl; |
| 127 | +module.exports.api = api; |
| 128 | +/* end-test-code */ |
0 commit comments