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

@shopyourway/metrics

v0.3.3

Published

Metrics reporting framework for reporting data point information to aggregators (like Graphite)

Downloads

1

Readme

Metrics

Metrics is a reporting framework for data point information (measurements and time series) to aggregators such as Graphite

Highlights

  • Time series reporting
  • Support pluggable reporters
  • Built in reporters:
    • Graphite
    • String
    • Console
    • InMemory (for testing)
  • Simple, easy to use API

Getting started

Installation

npm (scoped)

Configuration

Import metrics package:

const Metrics = require('@shopyourway/metrics').Metrics;

Initialize the metrics instance with the required reporters:

const stringReporter = new require('@shopyourway/metrics').StringReporter(metricString => {
	// Do something
});
const consoleReporter = new require('@shopyourway/metrics').ConsoleReporter();

const metrics = new Metrics([stringReporter, consoleReporter], errorHandler);

Reporting execution time

Use the Metrics instance to report execution time of a function:

metrics.space('users.get').meter(function(userIds, callback) {
	// read users from database
	callback(...);
});
  • You may use the space function to define the namespace key that will be used to aggregate the time
  • when you wish to meter a function, use the meter function and pass a function with you code into it. the meter function will run your code, while measuring the time it took to execute, and report it to the configured reporters

Note that the metrics is reported only after the callback is called.

Reporters

Metrics comes with several built-in reporters

Graphite

Reports metrics to a graphite server:

const metrics = require('@shopyourway/metrics').Metrics;

const graphiteHost = '1.1.1.1'; // Graphite server IP address
const graphitePort = 2003; // Optional - port number. Defaults to 2003
const spacePrefix = 'My.Project'; // Optional - prefix to all metrics spaces

const graphiteReporter = new require('@shopyourway/metrics').GraphiteReporter({
		host: graphiteHost,
		port: graphitePort,
		prefix: spacePrefix
	});

const metrics = new Metrics([graphiteReporter], errorHandler);

Console

Console reporter comes in handy when you need to debug metrics calls:

const Metrics = require('@shopyourway/metrics').Metrics;

const consoleReporter = new require('@shopyourway/metrics').ConsoleReporter();
	
const metrics = new Metrics([consoleReporter], errorHandler);

When a metrics will be reported, a message will appear in the terminal, that includes the key and the value reported.

String

const Metrics = require('@shopyourway/metrics').Metrics;
const fs = require('fs');

const stringReporter = new require('@shopyourway/metrics').StringReporter(metricString => {
	fs.appendFile('metrics.log', metricsString);
});
	
const metrics = new Metrics([stringReporter], errorHandler);

Here, StringReporter is used to build a log file from the metrics reports.

InMemory

InMemoryReporter can be used for testing purposed, in order to make sure your code reports metrics as expected.

const Metrics = require('@shopyourway/metrics').Metrics;

const metricsStorage = [];

const memoryReporter = new require('@shopyourway/metrics').InMemoryReporter(metricsStorage);
	
const metrics = new Metrics([memoryReporter], errorHandler);

When a metric is reported, an object with key and value properties is pushed to the array. Then, the array can be used in order to validate the report.

Building new reporters

Metrics support creating new reports acccording to an application needs.

A reporter must contain a method called report. The method gets the reported key and value.

For example, lets see how to implement a reporter for redis:

const client = require('redis').createClient();

module.exports = function RedisReporter(channel) {
  this.report = function(key, value) {
    client.publish(channel, JSON.stringify({ key: key, value: value }));
  }
};

The new reporter will pusblish a message to a specified channel in redis when a metric is reported.

Development

How to contribute

We encorage contribution via pull requests on any feature you see fit.

When submitting a pull request make sure to do the following:

  • Check that new and updated code follows OhioBox existing code formatting and naming standard
  • Run all unit and integration tests to ensure no existing functionality has been affected
  • Write unit or integration tests to test your changes. All features and fixed bugs must have tests to verify they work Read GitHub Help for more details about creating pull requests

Running tests

To run tests, in command line simply run npm test