• Stars
    star
    525
  • Rank 84,404 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js client for statsd, DogStatsD, and Telegraf

hot-shots

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

This project was originally a fork off of node-statsd. This project includes all changes in the latest node-statsd and many additional changes, including:

  • TypeScript types
  • Telegraf support
  • events
  • child clients
  • tcp protocol support
  • uds (Unix domain socket) protocol support
  • raw stream protocol support
  • mock mode
  • asyncTimer
  • asyncDistTimer
  • much more, including many bug fixes

hot-shots supports Node 10.x and higher.

Build Status

Usage

All initialization parameters are optional.

Parameters (specified as one object passed into hot-shots):

  • host: The host to send stats to, if not set, the constructor tries to retrieve it from the DD_AGENT_HOST environment variable, default: 'undefined' which as per UDP/datagram socket docs results in 127.0.0.1 or ::1 being used.
  • port: The port to send stats to, if not set, the constructor tries to retrieve it from the DD_DOGSTATSD_PORT environment variable, default: 8125
  • prefix: What to prefix each stat name with default: ''. Note prefix separator must be specified explicitly if desired (e.g. my_prefix.).
  • suffix: What to suffix each stat name with default: ''. Note suffix separator must be specified explicitly if desired (e.g. .my_suffix).
  • tagPrefix: Prefix tag list with character default: '#'. Note does not work with telegraf option.
  • tagSeparator: Separate tags with character default: ','. Note does not work with telegraf option.
  • globalize: Expose this StatsD instance globally. default: false
  • cacheDns: Caches dns lookup to host for cacheDnsTtl, only used when protocol is udp, default: false
  • cacheDnsTtl: time-to-live of dns lookups in milliseconds, when cacheDns is enabled. default: 60000
  • mock: Create a mock StatsD instance, sending no stats to the server and allowing data to be read from mockBuffer. Note that mockBuffer will keep growing, so only use for testing or clear out periodically. default: false
  • globalTags: Tags that will be added to every metric. Can be either an object or list of tags. default: {}. The following Datadog tags are appended to globalTags from the corresponding environment variable if the latter is set:
    • dd.internal.entity_id from DD_ENTITY_ID (docs)
    • env from DD_ENV (docs)
    • service from DD_SERVICE (docs)
    • version from DD_VERSION (docs)
  • 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
  • sampleRate: Sends only a sample of data to StatsD for all StatsD methods. Can be overridden at the method level. default: 1
  • errorHandler: A function with one argument. It is called to handle various errors. default: none, errors are thrown/logger to console
  • useDefaultRoute: Use the default interface on a Linux system. Useful when running in containers
  • protocol: Use tcp option for TCP protocol, or uds for the Unix Domain Socket protocol or stream for the raw stream. Defaults to udp otherwise.
  • path: Used only when the protocol is uds. Defaults to /var/run/datadog/dsd.socket.
  • stream: Reference to a stream instance. Used only when the protocol is stream.
  • tcpGracefulErrorHandling: Used only when the protocol is tcp. Boolean indicating whether to handle socket errors gracefully. Defaults to true.
  • tcpGracefulRestartRateLimit: Used only when the protocol is tcp. Time (ms) between re-creating the socket. Defaults to 1000.
  • udsGracefulErrorHandling: Used only when the protocol is uds. Boolean indicating whether to handle socket errors gracefully. Defaults to true.
  • udsGracefulRestartRateLimit: Used only when the protocol is uds. Time (ms) between re-creating the socket. Defaults to 1000.
  • closingFlushInterval: Before closing, StatsD will check for inflight messages. Time (ms) between each check. Defaults to 50.
  • udpSocketOptions: Used only when the protocol is uds. Specify the options passed into dgram.createSocket(). Defaults to { type: 'udp4' }

StatsD methods

All StatsD methods other than event, close, and check 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 tags to add to metrics. Can be either an object { tag: "value"} or an array of tags. 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.

close

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.

event

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 tags to add to metrics. Can be either an object { tag: "value"} or an array of tags. default: []
  • callback: The callback to execute once the metric has been sent.

check

The check method has the following API:

  • name: Check name required
  • status: Check status required
  • options: Options for the check
    • date_happened Assign a timestamp to the check default is now
    • hostname Assign a hostname to the check.
    • message Assign a message to the check.
  • tags: The tags to add to metrics. Can be either an object { tag: "value"} or an array of tags. default: []
  • callback: The callback to execute once the metric has been sent.
  var StatsD = require('hot-shots'),
      client = new StatsD({
          port: 8020,
          globalTags: { env: process.env.NODE_ENV },
          errorHandler: errorHandler,
      });

  // 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);

  // Distribution: Tracks the statistical distribution of a set of values across your infrastructure.
  // (DataDog v6)
  client.distribution('my_distribution', 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');

  // Check: sends a service check (DataDog only)
  client.check('service.up', client.CHECKS.OK, { hostname: 'host-1' }, ['foo', 'bar'])

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

  // Incrementing with tags
  client.increment('my_counter', ['foo', 'bar']);

  // 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');
    }
    });

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

  // Timing: also accepts a Date object of which the difference is calculated
  client.timing('response_time', new Date());

  // Timer: Returns a function that you call to record how long the first
  // parameter takes to execute (in milliseconds) and then sends that value
  // using 'client.timing'.
  // The parameters after the first one (in this case 'fn')
  // match those in 'client.timing'.
  var fn = function(a, b) { return a + b };
  client.timer(fn, 'fn_execution_time')(2, 2);

  // Async timer: Similar to timer above, but you instead pass in a function
  // that returns a Promise.  And then it returns a Promise that will record the timing.
  var fn = function () { return new Promise(function (resolve, reject) { setTimeout(resolve, n); }); };
  var instrumented = statsd.asyncTimer(fn, 'fn_execution_time');
  instrumented().then(function() {
    console.log('Code run and metric sent');
  });

  // Async timer: Similar to asyncTimer above, but it instead emits a distribution.
  var fn = function () { return new Promise(function (resolve, reject) { setTimeout(resolve, n); }); };
  var instrumented = statsd.asyncDistTimer(fn, 'fn_execution_time');
  instrumented().then(function() {
    console.log('Code run and metric sent');
  });

  // 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: 'value'}); // User-defined tag
  client.histogram('my_histogram', 42, ['tag:value']); // Tags as an array
  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: 'value'}, next);
  client.histogram('my_histogram', 42, 0.25, { tag: 'value'}, 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
  • uds option in protocol parameter- DogStatsD
  • histogram method- DogStatsD or Telegraf
  • event method- DogStatsD
  • check 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 explicitly thrown when there is a missing callback or if it is some potential configuration issue to be fixed.

If you would like to ensure all errors are caught, specify an errorHandler in your root client. This will catch errors in socket setup, sending of messages, and closing of the socket. If you specify an errorHandler and a callback, the callback will take precedence.

// Using errorHandler
var client = new StatsD({
  errorHandler: function (error) {
    console.log("Socket errors caught here: ", error);
  }
})

Congestion error

If you get an error like Error sending hot-shots message: Error: congestion with an error code of 1, it is probably because you are sending large volumes of metrics to a single agent/ server. This error only arises when using the UDS protocol and means that packages are being dropped. Take a look at the Datadog docs for some tips on tuning your connection.

Unix domain socket support

The 'uds' option as the protocol is to support Unix Domain Sockets for Datadog. It has the following limitations:

  • It only works where 'node-gyp' works. If you don't know what this is, this is probably fine for you. If you had an troubles with libraries that you 'node-gyp' before, you will have problems here as well.
  • It does not work on Windows

The above will cause the underlying library that is used, unix-dgram, to not install properly. Given the library is listed as an optionalDependency, and how it's used in the codebase, this install failure will not cause any problems. It only means that you can't use the uds feature.

Migrating from node-statsd

You should only need to do one thing: change node-statsd to hot-shots in all requires.

You can check the detailed change log for what has changed since the last release of node-statsd.

Submitting changes

Thanks for considering making any updates to this project! This project is entirely community-driven, and so your changes are important. 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 README.md with any needed documentation
  5. If you have made any API changes, update types.d.ts
  6. 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.

Package versioning and security

Versions will attempt to follow semantic versioning, with major changes only coming in major versions.

npm publishing is possible by one person, bdeitte, who has two-factor authentication enabled for publishes. Publishes only contain one additional library, unix-dgram.

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.

More Repositories

1

videojs-thumbnails

A video.js plugin that displays thumbnail images over the scrubber.
JavaScript
168
star
2

brightcove-player-sdk-ios

Brightcove Player SDK for iOS
Objective-C
127
star
3

dog-watcher

A utility which can be used to watch your DataDog monitor and dashboard configs and save them to a separate repository when they change.
JavaScript
49
star
4

player-loader

An asynchronous script loader and embed generator for the Brightcove Player.
JavaScript
28
star
5

videojs-rtc

WebRTC chat along with VideoJS
JavaScript
26
star
6

PHP-API-Wrapper

This project provides a starting point for integrating the Brightcove CMS API into your application. It provides simple ways to interact with the API, as well as a long list of helper functions.
PHP
25
star
7

videojs-flashls-source-handler

A source handler to integrate flashls with video.js
JavaScript
24
star
8

pkcs7

A node module to add and remove pkcs7-style padding.
JavaScript
22
star
9

react-player-loader

A React component that embeds and loads Brightcove Player scripts.
JavaScript
21
star
10

videojs-debugger

A debugger for videojs
JavaScript
17
star
11

videojs-dock

Obsolete and no longer maintained, equivalent functionality will become a feature of Video.js itself in 8.0!
JavaScript
17
star
12

kacl

Keep a Changelog tooling for automating changelog releases
JavaScript
13
star
13

videojs-simpleoverlay

An example video.js plugin that will overlay some text on top of a video.
JavaScript
11
star
14

perception

Captures, aggregates and presents human perceived load times for web assets.
JavaScript
9
star
15

videojs-contextmenu

DEPRECATED - A cross-device context menu implementation for video.js players.
JavaScript
9
star
16

videojs-per-source-behaviors

A video.js plugin for enhancing a player with behaviors related to changing media sources.
JavaScript
9
star
17

brightcove-player-sdk-ios-ima

IMA Plugin for Brightcove Player SDK for iOS
Objective-C
9
star
18

videojs-schema

Injects schema.org JSON-LD metadata for Video Cloud videos
JavaScript
8
star
19

videojs-urlparams

A video.js plugin which reads the page's URL for a time to seek to and whether to automatically play.
JavaScript
8
star
20

player-loader-webpack-plugin

A webpack plugin to load a Brightcove Player into a webpack bundle.
JavaScript
7
star
21

videojs-mbr

A plugin for supporting multiple bit rate [MBR] playback in Video.js
JavaScript
7
star
22

watchmen

Who watches the watchmen?
JavaScript
6
star
23

BrightcoveSpecs

Podspecs for the Brightcove Native Player for iOS. This repository supersedes BCOVSpecs.git
Ruby
6
star
24

videojs-omniture

Adobe Omniture plugin for the Brightcove Player
JavaScript
6
star
25

Adobe-AEM-Brightcove-Connector

Brightcove connector to Adobe Experience Manager
Java
5
star
26

streaming-dataset

5
star
27

dog-retriever

A utility for downloading metric data from DataDog and saving it in CSV format.
JavaScript
5
star
28

live-streaming-investigation

Scripts and utilities for playing with live streams.
JavaScript
4
star
29

brightcove-player-sdk-ios-player-ui

HTML
4
star
30

salesforklift

A Salesforce Bulk API Ruby Wrapper
Ruby
4
star
31

brightcove-player-sdk-ios-widevine

BCOVWidevine Plugin for the Brightcove Player SDK for iOS
HTML
4
star
32

s3-s3

A Node.js library for S3 redunancy, for use with S3 replication
JavaScript
4
star
33

id3-ts-lib

An ID3-only MPEG2TS segment generator library.
JavaScript
3
star
34

brine

Cucumber DSL for testing REST APIs
Ruby
3
star
35

gallery-in-page-snippets

Custom In-Page Experience code snippets
JavaScript
3
star
36

valerie

First class validation library for the JVM
Groovy
3
star
37

brightcove-player-sdk-ios-fw

FreeWheel Plugin for Brightcove Player SDK for iOS
HTML
3
star
38

wait-for-it

A node.js module that helps you to perform a task just once while deferring any task-dependent threads of execution until after the task has completed.
JavaScript
3
star
39

sharepoint-connector

JavaScript
2
star
40

brightcove-player-sdk-ios-sidecarsubtitles

BCOVSidecarSubtitles Plugin for Brightcove Player SDK for iOS
HTML
2
star
41

brightcove-player-sdk-ios-ssai

SSAI Plugin for Brightcove Player SDK for iOS
Objective-C
2
star
42

brightcove-player-sdk-ios-fairplay

FairPlay plugin for the Brightcove Player SDK for iOS
HTML
2
star
43

brightcove-player-sdk-ios-oux

OnceUX plugin for the Brightcove Player SDK for iOS
HTML
2
star
44

chef-sync

Synchronize Chef nodes w/ Capistrano and other config
Ruby
2
star
45

brightcove-player-sdk-ios-googlecast

Google Cast plugin for the Brightcove Player SDK for iOS
HTML
1
star
46

player-management-client

Player Management API JavaScript SDK
JavaScript
1
star
47

typed-immutable-extensions

Extensions on top of typed-immutable
JavaScript
1
star
48

videojs-ttml

A video.js plugin that allows you to display TTML captions
JavaScript
1
star
49

player-plugins-walkthrough

A walkthrough of creating player plugins.
JavaScript
1
star
50

ml-abr-experiments

Experiments using TensorFlow to create a more accurate bandwidth predictor for ABR.
Python
1
star
51

videojs-swf-compiler

A bare bones implementation of MXMLC compiler.
Shell
1
star
52

typed-immutable-proptypes

React Prop-Types from typed-immutable and typed-immutable-extensions
JavaScript
1
star
53

BCOVSpecs

Brightcove-hosted podspecs
Ruby
1
star
54

cookbook-razor-server

Deploy puppetlabs/razor-server via Chef Solo (or Chef Client)
Ruby
1
star
55

stubby-request

This module holds common code used for stubbing request methods and tracking calls that were made.
JavaScript
1
star