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

uber-statsd-client

v1.7.3

Published

Yet another client for Etsy's statsd

Downloads

719

Readme

uber-statsd-client

Node.js client for statsd.

Example

var createStatsd = require('uber-statsd-client');

var sdc = createStatsd({
    host: 'statsd.example.com'
});

var timer = new Date();
sdc.increment('some.counter'); // Increment by one.
sdc.gauge('some.gauge', 10); // Set gauge to 10
sdc.timing('some.timer', timer); // Calculates time diff

sdc.close(); // Optional - stop NOW

Caveats

Apparently a udp based statsd client does not work with cluster in node0.10 or node0.12. I'd recommend finding a tcp based statsd client or not using cluster.

Docs

var createStatsd = require('uber-statsd-client')
var sdc = createStatsd({
    host: 'statsd.example.com',
    port: 8124
});

Available options:

options.prefix

type: String, default: ""

Prefix all stats written by the client with a particular string prefix value. This defaults to ""

options.host

type: String, default: ""

The hostname where stats should be send. Defaults to "localhost"

options.port

type: Number, default: 8125

The port where stats should be send. Defaults to 8125

options.socket_timeout

type: Number, default: 1000

The UDP socket will auto-close if it has not been written to within the socket_timeout period. Defaults to 1000 milliseconds.

If socket_timeout is set to 0 then the UDP socket will never auto close.

options.highWaterMark

type: Number, default: 100

The UDP socket implementation has an internal highWaterMark. There is a known issue in node core where it will buffer packets being written to the UDP socket if it's currently in the "resolving DNS host" state.

When the highWaterMark is reached all packets are dropped and ignored.

options.packetQueue

{
    block?: Number,
    flush?: Number,
    trailingNewLine?: Boolean
}

The UDP socket uses an internal packet queue that can be configured.

The semantics of the packet queue is that it will write to the UDP socket if it has buffered at least block amount of bytes OR it has elapsed at least flush amount of milliseconds.

By default the packet queue will join multiple messages with a new line. It will also append a trailing new line, however you can opt out of the trailing new line by setting trailingNewLine field to false.

The block field defaults to 1440 bytes. The flush field defaults to 1000 milliseconds. The trailingNewLine field defaults to true.

options.dnsResolver

{
    timeToLive?: Number,
    seedIP?: String,
    backoffSettings?: {
        maxDelay?: Number,
        minDelay?: Number,
        retries?: Number,
        factor?: Number
    }
}

The UDP socket will optionally use a DNS resolver to resolve DNS once instead of resolving it for every UDP packet being written.

It's strongly recommended you use the DNS resolver.

The DNS resolver will resolve its DNS lookup based on the configured timeToLive. i.e. it caches the DNS host for at least that amount of time.

The DNS resolver takes a seedIP value, this is used when it cannot resolve DNS due to DNS failures and will fallback to the static IP you gave it.

The DNS resolver retries DNS lookups on DNS failures based on the backoffSettings you supply.

The timeToLive field defaults to five minutes (in milliseconds) The seedIP field has no default. The backoffSettings.maxDelay field defaults to Infinity The backoffSettings.minDelay field defaults to 500 milliseconds The backoffSettings.retries field defaults to 10 retries The backoffSettings.factor field defaults to 2.

options.isDisabled

isDisabled is an optional predicate function. You can pass in a predicate function that allows you to disabled the statsd client at run time.

When this predicate function returns true the EphemeralSocket will stop writing to the statsd UDP server.

Counting stuff

Counters are supported, both as raw .counter(metric, delta) and with the shortcuts .increment(metric, [delta=1]) and .decrement(metric, [delta=-1]):

sdc.increment('systemname.subsystem.value'); // Increment by one
sdc.decrement('systemname.subsystem.value', -10); // Decrement by 10
sdc.counter('systemname.subsystem.value, 100); // Indrement by 100

Gauges

Sends an arbitrary number to the back-end:

sdc.gauge('what.you.gauge', 100);

Delays

Keep track of how fast (or slow) your stuff is:

var start = new Date();
setTimeout(function () {
		sdc.timing('random.timeout', start);
}, 100 * Math.random());

If it is given a Date, it will calculate the difference, and anything else will be passed straight through.

And don't let the name (or nifty interface) fool you - it can measure any kind of number, where you want to see the distribution (content lengths, list items, query sizes, ...)

Stopping gracefully

By default, the socket is closed if it hasn't been used for a second (see socket_timeout in the init-options), but it can also be force-closed with .close():

var start = new Date();
setTimeout(function () {
	sdc.timing('random.timeout', start); // 2 - implicitly re-creates socket.
	sdc.close(); // 3 - Closes socket after last use.
}, 100 * Math.random());
sdc.close(); // 1 - Closes socket early.

The call is idempotent, so you can call it "just to be sure". And if you submit new metrics later, the socket will automatically be re-created, and a new timeout-timer started.

Prefix-magic

The library supports getting "child" clients with extra prefixes, to help with making sane name-spacing in apps:

// Create generic client
var sdc = createSDC({host: 'statsd.example.com', prefix: 'systemname');
sdc.increment('foo'); // Increments 'systemname.foo'
... do great stuff ...

// Subsystem A
var sdcA = sdc.getChildClient('a');
sdcA.increment('foo'); // Increments 'systemname.a.foo'

// Subsystem B
var sdcB = sdc.getChildClient('b');
sdcB.increment('foo'); // Increments 'systemname.b.foo'

Internally, they all use the same socket, so calling .close() on any of them will allow the entire program to stop gracefully.

What's broken

Check the GitHub issues.

LICENSE

ISC - see LICENSE.txt.