Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

cloudwatch-metrics

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cloudwatch-metrics - npm Package Compare versions

Comparing version
1.0.0
to
1.1.0
+169
spec/src/indexSpec.js
/* globals describe, afterEach, it, expect, spyOn */
var _ = require('underscore');
var rewire = require('rewire');
var cloudwatchMetric = rewire('../..');
describe('cloudwatch-metrics', function() {
var restoreAWS;
function attachHook(hook) {
restoreAWS = cloudwatchMetric.__set__('AWS', {
CloudWatch: function() {
this.putMetricData = hook;
}
});
}
afterEach(function() {
if (restoreAWS) {
restoreAWS();
restoreAWS = null;
}
});
describe('put', function() {
it('should buffer until timeout', function(done) {
attachHook(function(data, cb) {
expect(data).toEqual({
MetricData: [{
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 1
}],
Namespace: 'namespace'
});
cb();
});
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}], {
sendInterval: 1000,
sendCallback: done
});
metric.put(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
});
it('should call continually', function(done) {
attachHook(function(data, cb) {
expect(data).toEqual({
MetricData: [{
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 1
}],
Namespace: 'namespace'
});
cb();
});
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}], {
sendInterval: 500,
sendCallback: _.after(3, done)
});
var interval;
var stop = _.after(3, () => clearInterval(interval));
interval = setInterval(function() {
metric.put(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
stop();
}, 400);
});
it('should buffer until the cap is hit', function(done) {
attachHook(function(data, cb) {
expect(data).toEqual({
MetricData: [{
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 1
}, {
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 2
}],
Namespace: 'namespace'
});
cb();
});
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}], {
sendInterval: 3000, // mocha defaults to a 2 second timeout so setting
// larger than that will cause the test to fail if we
// hit the timeout
sendCallback: done,
maxCapacity: 2
});
metric.put(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
metric.put(2, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
});
});
describe('sample', function() {
it('should ignore metrics when not in the sample range', function() {
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}]);
spyOn(Math, 'random').and.returnValue(0.5);
spyOn(metric, 'put');
metric.sample(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}], 0.2);
expect(metric.put).not.toHaveBeenCalled();
});
it('should call put when the we decide to sample a metric', function() {
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}]);
spyOn(Math, 'random').and.returnValue(0.1);
// Just so we don't send anything to AWS.
spyOn(metric, 'put').and.returnValue(undefined);
metric.sample(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}], 0.2);
expect(metric.put).toHaveBeenCalled();
});
});
});
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
+16
-0

@@ -171,2 +171,18 @@ /**

/**
* Samples a metric so that we send the metric to Cloudwatch at the given
* sampleRate.
* @param {Integer|Long} value Data point to submit
* @param {String} namespace Name of the metric
* @param {Array} additionalDimensions Array of additional CloudWatch metric dimensions. See
* http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_Dimension.html for details.
* @param {Float} sampleRate The rate at which to sample the metric at.
* The sample rate must be between 0.0 an 1.0. As an example, if you provide
* a sampleRate of 0.1, then we will send the metric to Cloudwatch 10% of the
* time.
*/
Metric.prototype.sample = function(value, metricName, additionalDimensions, sampleRate) {
if (Math.random() < sampleRate) this.put(value, metricName, additionalDimensions);
};
/**
* _sendMetrics is called on a specified interval (defaults to 5 seconds but

@@ -173,0 +189,0 @@ * can be overridden but providing a `sendInterval` option when creating a

+4
-5
{
"name": "cloudwatch-metrics",
"version": "1.0.0",
"version": "1.1.0",
"description": "A simple wrapper for simplifying using Cloudwatch metrics",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "jasmine"
},

@@ -29,6 +29,5 @@ "repository": {

"devDependencies": {
"aws-sdk-mock": "^1.5.0",
"chai": "^3.5.0",
"mocha": "^3.0.2"
"jasmine": "^2.5.2",
"rewire": "^2.5.2"
}
}

@@ -13,3 +13,8 @@ ## cloudwatch-metrics

### Initialization
We should always initialize our environment first:
By default, the library will log metrics to the `us-east-1` region and read
AWS credentials from the AWS SDK's [default environment variables](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Credentials_from_Environment_Variables).
If you want to change these values, you can call `initialize`:
```js

@@ -57,3 +62,3 @@ var cloudwatchMetrics = require('cloudwatch-metrics');

```js
myMetric.put(value, metric, additionalDimensions, done);
myMetric.put(value, metric, additionalDimensions);
```

@@ -94,2 +99,3 @@

## Release History
1.1.0 Add `metric.sample()`
1.0.0 Initial release.
var expect = require('chai').expect;
var AWS = require('aws-sdk-mock');
var cloudwatchMetric = require('..');
var attachHook = (hook) => AWS.mock('CloudWatch', 'putMetricData', hook);
describe('cloudwatch-metrics', function() {
afterEach(function() {
AWS.restore('CloudWatch', 'putMetricData');
});
it('should buffer until timeout', function(done) {
this.timeout(5000);
attachHook(function(data, cb) {
expect(data).to.deep.equal({
MetricData: [{
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 1
}],
Namespace: 'namespace'
});
cb();
});
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}], {
sendInterval: 1000,
sendCallback: done
});
metric.put(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
});
it('should buffer until the cap is hit', function(done) {
attachHook(function(data, cb) {
expect(data).to.deep.equal({
MetricData: [{
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 1
}, {
Dimensions: [{
Name: "environment",
Value: "PROD"
}, {
Name: "ExtraDimension",
Value: "Value"
}],
MetricName: "metricName",
Unit: "Count",
Value: 2
}],
Namespace: 'namespace'
});
cb();
});
var metric = new cloudwatchMetric.Metric('namespace', 'Count', [{
Name: 'environment',
Value: 'PROD'
}], {
sendInterval: 3000, // mocha defaults to a 2 second timeout so setting
// larger than that will cause the test to fail if we
// hit the timeout
sendCallback: done,
maxCapacity: 2
});
metric.put(1, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
metric.put(2, 'metricName', [{Name:'ExtraDimension',Value: 'Value'}]);
});
});