npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloudwatch-metrics-kevin

v1.2.0

Published

A simple wrapper for simplifying using Cloudwatch metrics. Forked from https://github.com/mixmaxhq/cloudwatch-metrics.git

Downloads

2

Readme

cloudwatch-metrics

This module provides a simplified wrapper for creating and publishing CloudWatch metrics.

Install

$ npm install cloudwatch-metrics

or

$ npm install cloudwatch-metrics --save

Usage

Initialization

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.

If you want to change these values, you can call initialize:

var cloudwatchMetrics = require('cloudwatch-metrics');
cloudwatchMetrics.initialize({
	region: 'us-east-1'
});

Metric creation

For creating a metric, we simply need to provide the namespace and the type of metric:

var myMetric = new cloudwatchMetrics.Metric('namespace', 'Count');

Metric creation - w/ default dimensions

If we want to add our own default dimensions, such as environment information, we can add it in the following manner:

var myMetric = new cloudwatchMetrics.Metric('namespace', 'Count', [{
	Name: 'environment',
	Value: 'PROD'
}]);

Metric creation - w/ options

If we want to disable a metric in certain environments (such as local development), we can make the metric in the following manner:

// isLocal is a boolean
var isLocal = someWayOfDetermingIfLocal();

var myMetric = new cloudwatchMetrics.Metric('namespace', 'Count', [{
	Name: 'environment',
	Value: 'PROD'
}], {
	enabled: isLocal
});

The full list of configuration options is:

Option | Purpose ------ | ------- enabled | Whether or not we should send the metric to CloudWatch (useful for dev vs prod environments). sendInterval | The interval in milliseconds at which we send any buffered metrics, defaults to 5000 milliseconds. sendCallback | A callback to be called when we send metric data to CloudWatch (useful for logging any errors in sending data). maxCapacity | A maximum number of events to buffer before we send immediately (before the sendInterval is reached). withTimestamp | Include the timestamp with the metric value. storageResolution | The metric storage resolution to use in seconds. Set to 1 for high resolution metrics. See (https://aws.amazon.com/blogs/aws/new-high-resolution-custom-metrics-and-alarms-for-amazon-cloudwatch/)

Publishing metric data

Then, whenever we want to publish a metric, we simply do:

myMetric.put(value, metric, additionalDimensions);

Using summary metrics

Instead of sending individual data points for your metric, you may want to send summary metrics. Summary metrics track statistics over time, and send those statistics to CloudWatch on a configurable interval. For instance, you might want to know your total network throughput, but you don't care about individual request size percentiles. You could use summaryPut to track this data and send it to CloudWatch with fewer requests:

var metric = new cloudwatchMetrics.Metric('namespace', 'Bytes');

function onRequest(req) {
	// This will still track maximum, minimum, sum, count, and average, but won't
	// take up lots of CloudWatch requests doing so.
	metric.summaryPut(req.size, 'requestSize');
}

Note that metrics use different summaries for different dimensions, and that the order of the dimensions is significant! In other words, these track different metric sets:

var metric = new cloudwatchMetrics.Metric('namespace', 'Bytes');
// Different statistic sets!
metric.summaryPut(45, 'requestSize', [{Name: 'Region', Value: 'US'}, {Name: 'Server', Value: 'Card'}]);
metric.summaryPut(894, 'requestSize', [{Name: 'Server', Value: 'Card'}, {Name: 'Region', Value: 'US'}]);

NOTES

Be aware that the put call does not actually send the metric to CloudWatch at that moment. Instead, it stores unsent metrics and sends them to CloudWatch on a predetermined interval (to help get around sending too many metrics at once - CloudWatch limits you by default to 150 put-metric data calls per second). The default interval is 5 seconds, if you want metrics sent at a different interval, then provide that option when constructing your CloudWatch Metric:

var myMetric = new cloudwatchMetrics.Metric('namespace', 'Count', [{
	Name: 'environment',
	Value: 'PROD'
}], {
	sendInterval: 3 * 1000 // It's specified in milliseconds.
});

You can also register a callback to be called when we actually send metrics to CloudWatch - this can be useful for logging put-metric-data errors:

var myMetric = new cloudwatchMetrics.Metric('namespace', 'Count', [{
	Name: 'environment',
	Value: 'PROD'
}], {
	sendCallback: (err) => {
		if (!err) return;
		// Do your error handling here.
	}
});

Release History

  • 1.2.0 Add the ability to use summary metrics.
  • 1.1.0 Add metric.sample()
  • 1.0.0 Initial release.