Giter VIP home page Giter VIP logo

hot-shots's Introduction

hot-shots

A Node.js client for Etsy's StatsD server, Datadog's DogStatsD server, and InfluxDB's Telegraf StatsD server.

This project is a fork off of node-statsd. This project includes all changes in node-statsd, all open PRs to node-statsd when possible, and some additional goodies (like Telegraf support, child clients, and more).

Build Status

Installation

$ npm install hot-shots

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
  • globalTags: Tags that will be added to every metric default: []
  • maxBufferSize: If larger than 0, metrics will be buffered and only sent when the string length is greater than the size. default: 0
  • bufferFlushInterval: If buffering is in use, this is the time in ms to always flush any buffered metrics. default: 1000
  • telegraf: Use Telegraf's StatsD line protocol, which is slightly different than the rest default: false
  • errorHandler: A function with one argument. It is called to handle various errors. default: none, errors are thrown/logger to console

All StatsD methods other than event and close 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 or buffered

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

The close method has the following API:

  • callback: The callback to execute once close is complete. All other calls to statsd will fail once this is called.

The event method has the following API:

  • title: Event title required
  • text: Event description default is title
  • options: Options for the event
    • date_happened Assign a timestamp to the event default is now
    • hostname Assign a hostname to the event.
    • aggregation_key Assign an aggregation key to the event, to group it with some others.
    • priority Can be ‘normal’ or ‘low’ default: normal
    • source_type_name Assign a source type to the event.
    • alert_type Can be ‘error’, ‘warning’, ‘info’ or ‘success’ default: info
  • tags: The Array of tags to add to metrics default: []
  • callback: The callback to execute once the metric has been sent.
  var StatsD = require('hot-shots'),
      client = new StatsD();

  // Catch socket errors so they don't go unhandled, as explained
  // in the Errors section below
  client.socket.on('error', function(error) {
    console.error("Error in socket: ", error);
  });

  // 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 (DataDog and Telegraf only)
  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');

  // Event: sends the titled event (DataDog only)
  client.event('my_title', 'description');

  // 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 (DataDog and Telegraf only)
  client.histogram('my_histogram', 42, ['foo', 'bar']);

  // Using the callback.  This is the same format for the callback
  // with all non-close calls
  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 (DataDog and Telegraf only)
  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);

  // Use a child client to add more context to the client.
  // Clients can be nested.
  var childClient = client.childClient({
    prefix: 'additionalPrefix.',
    suffix: '.additionalSuffix',
    globalTags: ['globalTag1:forAllMetricsFromChildClient']
  });
  childClient.increment('my_counter_with_more_tags');

  // Close statsd.  This will ensure all stats are sent and stop statsd
  // from doing anything more.
  client.close(function(err) {
    console.log('The close did not work quite right: ', err);
  });

DogStatsD and Telegraf functionality

Some of the functionality mentioned above is specific to DogStatsD or Telegraf. They will not do anything if you are using the regular statsd client.

  • globalTags parameter- DogStatsD or Telegraf
  • tags parameter- DogStatsD or Telegraf
  • telegraf parameter- Telegraf
  • histogram method- DogStatsD or Telegraf
  • event method- DogStatsD

Errors

As usual, callbacks will have an error as their first parameter. You can have an error in both the message and close callbacks.

If the optional callback is not given, an error is thrown in some cases and a console.log message is used in others. An error will only be thrown when there is a missing callback if it is some potential configuration issue to be fixed.

In the event that there is a socket error, hot-shots 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) {
  console.error("Error in socket: ", error);
});

Submitting changes

Thanks for considering making any updates to this project! Here are the steps to take in your fork:

  1. Run "npm install"
  2. Add your changes in your fork as well as any new tests needed
  3. Run "npm test"
  4. Update the HEAD section in CHANGES.md with a description of what you have done
  5. Push your changes and create the PR

When you've done all this we're happy to try to get this merged in right away.

Name

Why is this project named hot-shots? Because:

  1. It's impossible to find another statsd name on npm
  2. It's the name of a dumb movie
  3. No good reason

License

hot-shots is licensed under the MIT license.

hot-shots's People

Contributors

devdazed avatar ash2k avatar sivy avatar lbeschastny avatar humphd avatar jjofseattle avatar salty-horse avatar mmoulton avatar kbourgoin avatar dieterbe avatar arlolra avatar trevorah avatar cattail avatar jjzeng-seattle avatar joybro avatar stuintrepica avatar skabbes avatar mindreframer avatar rcrowley avatar pchelolo avatar goelvivek avatar fictorial avatar bdeitte avatar tmm1 avatar pifantastic avatar

Watchers

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.