Merge pull request #4576 from peterkooijmans/master

Add statsd-client definitions and tests.
This commit is contained in:
Horiuchi_H
2015-06-09 14:33:19 +09:00
2 changed files with 160 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/// <reference path="statsd-client.d.ts" />
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'

103
statsd-client/statsd-client.d.ts vendored Normal file
View File

@@ -0,0 +1,103 @@
// Type definitions for statsd-client v0.1.0
// Project: https://github.com/msiebuhr/node-statsd-client
// Definitions by: Peter Kooijmans <https://github.com/peterkooijmans/>
// 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;
}