diff --git a/types/statsd-client/index.d.ts b/types/statsd-client/index.d.ts index 7bb83b7393..2b5c0c59cc 100644 --- a/types/statsd-client/index.d.ts +++ b/types/statsd-client/index.d.ts @@ -1,9 +1,11 @@ -// Type definitions for statsd-client v0.1.0 +// Type definitions for statsd-client v0.4.0 // Project: https://github.com/msiebuhr/node-statsd-client // Definitions by: Peter Kooijmans +// Christopher Eck // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 - +import * as express from "express"; interface CommonOptions { /** @@ -16,6 +18,13 @@ interface CommonOptions { */ debug?: boolean; + /** + * Object of string key/value pairs which will be appended on + * to all StatsD payloads (excluding raw payloads) + * (default {}) + */ + tags?: { [key: string]: string }; + /** * User specifically wants to use tcp (default false) */ @@ -79,24 +88,53 @@ interface HttpOptions extends CommonOptions { method?: string; } +interface ExpressMiddlewareOptions { + /** + * Metric name to use for reporting if a matching route is not + * found (default "unknown_express_route"). + */ + notFoundRouteName?: string; + + /** + * Optional callback called after reporting metrics for an + * express route. + */ + onResponseEnd?: (client: StatsdClient, startTime: Date, req: express.Request, res: express.Response) => void; + + /** + * Enables inclusion of per-URL response code and timing + * metrics (default false). + */ + timeByUrl?: boolean; +} + declare 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; + counter(metric: string, delta: number, tags?: {}): this; + increment(metric: string, delta?: number, tags?: {}): this; + decrement(metric: string, delta?: number, tags?: {}): this; - gauge(name: string, value: number): void; - gaugeDelta(name: string, delta: number): void; + gauge(name: string, value: number, tags?: {}): this; + gaugeDelta(name: string, delta: number, tags?: {}): this; - set(name: string, value: number): void; + set(name: string, value: number, tags?: {}): this; - timing(name: string, start: Date): void; - timing(name: string, duration: number): void; + timing(name: string, startOrDuration: Date | number, tags?: {}): this; - close(): void; + histogram(name: string, value: number, tags?: {}): this; + + raw(rawData: string): this; + + close(): this; getChildClient(name: string): StatsdClient; + + formatTags(tags?: {}): string; + + helpers: { + getExpressMiddleware(prefix?: string, options?: ExpressMiddlewareOptions): express.RequestHandler; + }; } declare namespace StatsdClient {} diff --git a/types/statsd-client/statsd-client-tests.ts b/types/statsd-client/statsd-client-tests.ts index d8490448a5..205b28b851 100644 --- a/types/statsd-client/statsd-client-tests.ts +++ b/types/statsd-client/statsd-client-tests.ts @@ -1,9 +1,9 @@ import SDC = require("statsd-client"); -var sdc = new SDC( { host: 'statsd.example.com' }); +let sdc = new SDC( { host: 'statsd.example.com' }); -var timer = new Date(); +const 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 @@ -11,12 +11,16 @@ 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}); +sdc = new SDC({host: 'statsd.example.com', port: 8124, debug: true, tags: {foo: 'bar'}}); // 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 +sdc.increment('systemname.subsystem.value.tagged', 1, {biz: 'baz'}); // Increment tagged metric by one +sdc.decrement('systemname.subsystem.value.tagged', -10, {biz: 'baz'}); // Decrement tagged metric by 10 +sdc.counter('systemname.subsystem.value.tagged', 100, {biz: 'baz'}); // Increment tagged metric by 100 + // Gauges sdc.gauge('what.you.gauge', 100); @@ -24,17 +28,37 @@ 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 +sdc.gauge('what.you.gauge.tagged', 100, {biz: 'baz'}); +sdc.gaugeDelta('what.you.gauge.tagged', 20, {biz: 'baz'}); // Will now count 120 +sdc.gaugeDelta('what.you.gauge.tagged', -70, {biz: 'baz'}); // Will now count 50 +sdc.gauge('what.you.gauge.tagged', 10, {biz: 'baz'}); // Will now count 10 + // Set sdc.set('your.set', 200); +sdc.set('your.set.tagged', 200, {biz: 'baz'}); // Timeouts -var start = new Date(); +let start = new Date(); setTimeout(function () { sdc.timing('random.timeout', start); }, 100 * Math.random()); +setTimeout(function () { + sdc.timing('random.timeout.tagged', start, {biz: 'baz'}); +}, 100 * Math.random()); + +// Histogram +sdc.histogram('histogram.stuff', 40); +sdc.histogram('histogram.stuff', 44, {biz: 'baz'}); + +// Raw string output +sdc.raw('my.metric:123|g'); + +// Internal tags formatting +sdc.formatTags({biz: 'baz'}); + // Stopping gracefully -var start = new Date(); +start = new Date(); setTimeout(function () { sdc.timing('random.timeout', start); // 2 - implicitly re-creates socket. sdc.close(); // 3 - Closes socket after last use. @@ -43,14 +67,17 @@ sdc.close(); // 1 - Closes socket early. // Prefix magic // Create generic client -var sdc = new SDC({host: 'statsd.example.com', prefix: 'systemname'}); +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'); +const sdcA = sdc.getChildClient('a'); sdcA.increment('foo'); // Increments 'systemname.a.foo' // Subsystem B -var sdcB = sdc.getChildClient('b'); +const sdcB = sdc.getChildClient('b'); sdcB.increment('foo'); // Increments 'systemname.b.foo' + +// Express middleware helper +sdc.helpers.getExpressMiddleware('express.metrics', {timeByUrl: true}); // Returns an express handler