From c1ccc2805d8e16ad7a01424a0f35d79c57f7c28d Mon Sep 17 00:00:00 2001 From: Peter Kooijmans Date: Mon, 8 Jun 2015 14:36:46 +0200 Subject: [PATCH] Add statsd-client definitions and tests. --- statsd-client/statsd-client-tests.ts | 57 +++++++++++++++ statsd-client/statsd-client.d.ts | 103 +++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 statsd-client/statsd-client-tests.ts create mode 100644 statsd-client/statsd-client.d.ts diff --git a/statsd-client/statsd-client-tests.ts b/statsd-client/statsd-client-tests.ts new file mode 100644 index 0000000000..6854790e66 --- /dev/null +++ b/statsd-client/statsd-client-tests.ts @@ -0,0 +1,57 @@ +/// + +import SDC = require("statsd-client"); + +var sdc = new SDC( { 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 + +// Initialization +sdc = new SDC({host: 'statsd.example.com', port: 8124, debug: true}); + +// Counting stuff +sdc.increment('systemname.subsystem.value'); // Increment by one +sdc.decrement('systemname.subsystem.value', -10); // Decrement by 10 +sdc.counter('systemname.subsystem.value', 100); // Increment by 100 + +// Gauges +sdc.gauge('what.you.gauge', 100); +sdc.gaugeDelta('what.you.gauge', 20); // Will now count 120 +sdc.gaugeDelta('what.you.gauge', -70); // Will now count 50 +sdc.gauge('what.you.gauge', 10); // Will now count 10 + +// Set +sdc.set('your.set', 200); + +// Timeouts +var start = new Date(); +setTimeout(function () { + sdc.timing('random.timeout', start); +}, 100 * Math.random()); + +// Stopping gracefully +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. + +// Prefix magic +// Create generic client +var sdc = new SDC({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' diff --git a/statsd-client/statsd-client.d.ts b/statsd-client/statsd-client.d.ts new file mode 100644 index 0000000000..86e317afa4 --- /dev/null +++ b/statsd-client/statsd-client.d.ts @@ -0,0 +1,103 @@ +// Type definitions for statsd-client v0.1.0 +// Project: https://github.com/msiebuhr/node-statsd-client +// Definitions by: Peter Kooijmans +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "statsd-client" { + + interface CommonOptions { + /** + * Prefix all stats with this value (default ""). + */ + prefix?: string; + + /** + * Print what is being sent to stderr (default false). + */ + debug?: boolean; + + /** + * User specifically wants to use tcp (default false) + */ + tcp?: boolean; + + /** + * Dual-use timer. Will flush metrics every interval. For UDP, + * it auto-closes the socket after this long without activity + * (default 1000 ms; 0 disables this). For TCP, it auto-closes + * the socket after socketTimeoutsToClose number of timeouts + * have elapsed without activity. + */ + socketTimeout?: number; + } + + interface TcpOptions extends CommonOptions { + /** + * Where to send the stats (default localhost). + */ + host?: string; + + /** + * Port to contact the statsd-daemon on (default 8125). + */ + port?: number; + + /** + * Number of timeouts in which the socket auto-closes if it + * has been inactive. (default 10; 1 to auto-close after a + * single timeout). + */ + socketTimeoutsToClose: number; + } + + interface UdpOptions extends CommonOptions { + /** + * Where to send the stats (default localhost). + */ + host?: string; + + /** + * Port to contact the statsd-daemon on (default 8125). + */ + port?: number; + } + + interface HttpOptions extends CommonOptions { + /** + * Where to send the stats (default localhost). + */ + host?: string; + + /** + * Additional headers to send (default {}). + */ + headers?: { [index : string] : string }; + + /** + * What HTTP method to use (default "PUT"). + */ + method?: string; + } + + class StatsdClient { + constructor(options: TcpOptions | UdpOptions | HttpOptions); + + counter(metric: string, delta: number): void; + increment(metric: string, delta?: number): void; + decrement(metric: string, delta?: number): void; + + gauge(name: string, value: number): void; + gaugeDelta(name: string, delta: number): void; + + set(name: string, value: number): void; + + timing(name: string, start: Date): void; + timing(name: string, duration: number): void; + + close(): void; + + getChildClient(name: string): StatsdClient; + } + + export = StatsdClient; +}