Skip to content

Commit 63f82b6

Browse files
committed
Add type definitions
1 parent a335b8a commit 63f82b6

File tree

2 files changed

+140
-2
lines changed

2 files changed

+140
-2
lines changed

client.d.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { EventEmitter } from 'events';
2+
import { Transform, TransformOptions } from 'readable-stream';
3+
import { MetricOptions } from '@metrics/metric';
4+
5+
interface BaseMetricsOptions {
6+
/**
7+
* Valid characters: `a-zA-Z0-9_`
8+
*/
9+
name: string;
10+
description: string;
11+
labels?: Record<string, string | number | boolean | null>;
12+
}
13+
14+
export interface MetricsCounter extends EventEmitter {
15+
/**
16+
* Increment the counter
17+
*
18+
* @example <caption>Increment by 1</caption>
19+
* counter.inc();
20+
* @example <caption>Increment by 10</caption>
21+
* counter.inc(10);
22+
* @example <caption>Increment by 1 with labels</caption>
23+
* counter.inc({ labels: { url: 'https://www.mysite.com' } });
24+
* @example <caption>Increment by 10 with labels</caption>
25+
* counter.inc(10, { labels: { url: 'https://www.mysite.com' } });
26+
*/
27+
inc(
28+
value?: number | BaseMetricsOptions,
29+
options?: Pick<BaseMetricsOptions, 'labels'>,
30+
): void;
31+
}
32+
33+
export interface MetricsGauge extends EventEmitter {
34+
set(value: number, options?: Pick<BaseMetricsOptions, 'labels'>): void;
35+
}
36+
37+
export interface MetricsHistogramOptions extends BaseMetricsOptions {
38+
buckets?: number[];
39+
}
40+
41+
export type EndTimer = (options?: Pick<BaseMetricsOptions, 'labels'>) => void;
42+
43+
export interface MetricsHistogram extends EventEmitter {
44+
/**
45+
* When called, will popupale the metrics stream with a histogram value.
46+
*
47+
* @param value
48+
* @param options
49+
*/
50+
observe(
51+
value: number,
52+
options?: Pick<MetricsHistogramOptions, 'labels' | 'buckets'>,
53+
): void;
54+
/**
55+
* Measure time between two points.
56+
*
57+
* @example <caption>Measure time between two points</caption>
58+
* const end = histogram.timer();
59+
* // some stuff happens
60+
* end();
61+
* @example <caption>Measure time between two points with labels</caption>
62+
* const end = histogram.timer({ labels: { url: 'https://www.mysite.com' } });
63+
* // some stuff happens
64+
* end();
65+
* @example <caption>Measure time between two points with labels in the end function</caption>
66+
* const end = histogram.timer();
67+
* // some stuff happens
68+
* end({ labels: { url: 'https://www.mysite.com' } });
69+
* @example <caption>Set custom buckets</caption>
70+
* const end = histogram.timer({ buckets: [0.1, 0.5, 1, 2, 5] });
71+
* // some stuff happens
72+
* end();
73+
*/
74+
timer(
75+
options?: Pick<MetricsHistogramOptions, 'labels' | 'buckets'>,
76+
): EndTimer;
77+
}
78+
79+
export interface MetricsSummaryOptions extends BaseMetricsOptions {
80+
quantiles?: number[];
81+
}
82+
83+
export interface MetricsSummary extends EventEmitter {
84+
/**
85+
* When called, will popupale the metrics stream with a histogram value.
86+
*
87+
* @param value
88+
* @param options
89+
*/
90+
observe(
91+
value: number,
92+
options?: Pick<MetricsSummaryOptions, 'labels' | 'quantiles'>,
93+
): void;
94+
/**
95+
* Measure time between two points.
96+
*
97+
* @example <caption>Measure time between two points</caption>
98+
* const end = summary.timer();
99+
* // some stuff happens
100+
* end();
101+
* @example <caption>Measure time between two points with labels</caption>
102+
* const end = summary.timer({ labels: { url: 'https://www.mysite.com' } });
103+
* // some stuff happens
104+
* end();
105+
* @example <caption>Measure time between two points with labels in the end function</caption>
106+
* const end = summary.timer();
107+
* // some stuff happens
108+
* end({ labels: { url: 'https://www.mysite.com' } });
109+
* @example <caption>Set custom buckets</caption>
110+
* const end = summary.timer({ quantiles: [0.001, 0.01, 0.5, 0.9, 0.99] });
111+
* // some stuff happens
112+
* end();
113+
*/
114+
timer(
115+
options?: Pick<MetricsSummaryOptions, 'labels' | 'quantiles'>,
116+
): EndTimer;
117+
}
118+
119+
export interface MetricsClientOptions extends TransformOptions {
120+
/**
121+
* An optional unique identifier of the MetricsClient instance.
122+
* A random ID will be generated if not provided.
123+
*/
124+
id?: string;
125+
}
126+
127+
export default class MetricsClient extends Transform {
128+
constructor(options?: MetricsClientOptions);
129+
130+
counter(options: BaseMetricsOptions): MetricsCounter;
131+
gauge(options: BaseMetricsOptions): MetricsGauge;
132+
histogram(options: MetricsHistogramOptions): MetricsHistogram;
133+
summary(options: BaseMetricsOptions): MetricsHistogram;
134+
metric(options: MetricOptions): void;
135+
timer(options: MetricOptions): (options?: Partial<MetricOptions>) => void;
136+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "@metrics/client",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"description": "A streaming metric producer. Allows producing counters, gauges, histograms and summaries in a way that is independent of your metrics system.",
55
"main": "lib/client.js",
6+
"types": "client.d.ts",
67
"files": [
7-
"lib"
8+
"lib",
9+
"client.d.ts"
810
],
911
"scripts": {
1012
"bench": "node benchmark/benchmark.js",

0 commit comments

Comments
 (0)