Giter Club home page Giter Club logo

node-statsd's Introduction

node-statsd

A node.js client for Etsy's StatsD server.

This client will let you fire stats at your StatsD server from a node.js application.

node-statsd Runs and is tested on Node 0.6+ on all *nix platforms and 0.8+ on all platforms including Windows.

Build Status

Installation

$ npm install node-statsd

Usage

All initialization parameters are optional.

Parameters (specified as an options hash):

  • host: The host to send stats to default: localhost
  • port: The port to send stats to default: 8125
  • prefix: What to prefix each stat name with default: ''
  • suffix: What to suffix each stat name with default: ''
  • globalize: Expose this StatsD instance globally? default: false
  • cacheDns: Cache the initial dns lookup to host default: false
  • mock: Create a mock StatsD instance, sending no stats to the server? default: false
  • global_tags: Optional tags that will be added to every metric default: []

All StatsD methods have the same API:

  • name: Stat name required
  • value: Stat value required except in increment/decrement where it defaults to 1/-1 respectively
  • sampleRate: Sends only a sample of data to StatsD default: 1
  • tags: The Array of tags to add to metrics default: []
  • callback: The callback to execute once the metric has been sent

If an array is specified as the name parameter each item in that array will be sent along with the specified value.

  var StatsD = require('node-statsd'),
      client = new StatsD();

  // Timing: sends a timing command with the specified milliseconds
  client.timing('response_time', 42);

  // Increment: Increments a stat by a value (default is 1)
  client.increment('my_counter');

  // Decrement: Decrements a stat by a value (default is -1)
  client.decrement('my_counter');

  // Histogram: send data for histogram stat
  client.histogram('my_histogram', 42);

  // Gauge: Gauge a stat by a specified amount
  client.gauge('my_gauge', 123.45);

  // Set: Counts unique occurrences of a stat (alias of unique)
  client.set('my_unique', 'foobar');
  client.unique('my_unique', 'foobarbaz');

  // Incrementing multiple items
  client.increment(['these', 'are', 'different', 'stats']);

  // Sampling, this will sample 25% of the time the StatsD Daemon will compensate for sampling
  client.increment('my_counter', 1, 0.25);

  // Tags, this will add user-defined tags to the data
  client.histogram('my_histogram', 42, ['foo', 'bar']);

  // Using the callback
  client.set(['foo', 'bar'], 42, function(error, bytes){
    //this only gets called once after all messages have been sent
    if(error){
      console.error('Oh noes! There was an error:', error);
    } else {
      console.log('Successfully sent', bytes, 'bytes');
    }
  });

  // Sampling, tags and callback are optional and could be used in any combination
  client.histogram('my_histogram', 42, 0.25); // 25% Sample Rate
  client.histogram('my_histogram', 42, ['tag']); // User-defined tag
  client.histogram('my_histogram', 42, next); // Callback
  client.histogram('my_histogram', 42, 0.25, ['tag']);
  client.histogram('my_histogram', 42, 0.25, next);
  client.histogram('my_histogram', 42, ['tag'], next);
  client.histogram('my_histogram', 42, 0.25, ['tag'], next);

Errors

In the event that there is a socket error, node-statsd will allow this error to bubble up. If you would like to catch the errors, just attach a listener to the socket property on the instance.

client.socket.on('error', function(error) {
  return console.error("Error in socket: ", error);
});

If you want to catch errors in sending a message then use the callback provided.

License

node-statsd is licensed under the MIT license.

node-statsd's People

Contributors

devdazed avatar sivy avatar lbeschastny avatar humphd avatar trevorah avatar dieterbe avatar kbourgoin avatar salty-horse avatar cattail avatar joybro avatar stuintrepica avatar skabbes avatar mindreframer avatar rcrowley avatar goelvivek avatar fictorial avatar tmm1 avatar pifantastic avatar

Watchers

William Bert avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.