-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmetrics.ts
213 lines (189 loc) · 5.48 KB
/
metrics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts
import { parseHeaders } from './util';
export interface MetricsOptions {
onError: OnError;
onSent?: OnSent;
appName: string;
metricsInterval: number;
disableMetrics?: boolean;
url: URL | string;
clientKey: string;
fetch: any;
headerName: string;
customHeaders?: Record<string, string>;
metricsIntervalInitial: number;
connectionId: string;
}
interface VariantBucket {
[s: string]: number;
}
interface Bucket {
start: Date;
stop: Date | null;
toggles: {
[s: string]: { yes: number; no: number; variants: VariantBucket };
};
}
interface Payload {
bucket: Bucket;
appName: string;
instanceId: string;
}
type OnError = (error: unknown) => void;
type OnSent = (payload: Payload) => void;
// eslint-disable-next-line @typescript-eslint/no-empty-function
const doNothing = () => {};
export default class Metrics {
private onError: OnError;
private onSent: OnSent;
private bucket: Bucket;
private appName: string;
private metricsInterval: number;
private disabled: boolean;
private url: URL;
private clientKey: string;
private timer: any;
private fetch: any;
private headerName: string;
private customHeaders: Record<string, string>;
private metricsIntervalInitial: number;
private connectionId: string;
constructor({
onError,
onSent,
appName,
metricsInterval,
disableMetrics = false,
url,
clientKey,
fetch,
headerName,
customHeaders = {},
metricsIntervalInitial,
connectionId,
}: MetricsOptions) {
this.onError = onError;
this.onSent = onSent || doNothing;
this.disabled = disableMetrics;
this.metricsInterval = metricsInterval * 1000;
this.metricsIntervalInitial = metricsIntervalInitial * 1000;
this.appName = appName;
this.url = url instanceof URL ? url : new URL(url);
this.clientKey = clientKey;
this.bucket = this.createEmptyBucket();
this.fetch = fetch;
this.headerName = headerName;
this.customHeaders = customHeaders;
this.connectionId = connectionId;
}
public start() {
if (this.disabled) {
return false;
}
if (
typeof this.metricsInterval === 'number' &&
this.metricsInterval > 0
) {
if (this.metricsIntervalInitial > 0) {
setTimeout(() => {
this.startTimer();
this.sendMetrics();
}, this.metricsIntervalInitial);
} else {
this.startTimer();
}
}
}
public stop() {
if (this.timer) {
clearTimeout(this.timer);
delete this.timer;
}
}
public createEmptyBucket(): Bucket {
return {
start: new Date(),
stop: null,
toggles: {},
};
}
private getHeaders() {
return parseHeaders({
clientKey: this.clientKey,
appName: this.appName,
connectionId: this.connectionId,
customHeaders: this.customHeaders,
headerName: this.headerName,
isPost: true,
});
}
public async sendMetrics(): Promise<void> {
/* istanbul ignore next if */
const url = `${this.url}/client/metrics`;
const payload = this.getPayload();
if (this.bucketIsEmpty(payload)) {
return;
}
try {
await this.fetch(url, {
cache: 'no-cache',
method: 'POST',
headers: this.getHeaders(),
body: JSON.stringify(payload),
});
this.onSent(payload);
} catch (e) {
console.error('Unleash: unable to send feature metrics', e);
this.onError(e);
}
}
public count(name: string, enabled: boolean): boolean {
if (this.disabled || !this.bucket) {
return false;
}
this.assertBucket(name);
this.bucket.toggles[name][enabled ? 'yes' : 'no']++;
return true;
}
public countVariant(name: string, variant: string): boolean {
if (this.disabled || !this.bucket) {
return false;
}
this.assertBucket(name);
if (this.bucket.toggles[name].variants[variant]) {
this.bucket.toggles[name].variants[variant] += 1;
} else {
this.bucket.toggles[name].variants[variant] = 1;
}
return true;
}
private assertBucket(name: string) {
if (this.disabled || !this.bucket) {
return false;
}
if (!this.bucket.toggles[name]) {
this.bucket.toggles[name] = {
yes: 0,
no: 0,
variants: {},
};
}
}
private startTimer(): void {
this.timer = setInterval(() => {
this.sendMetrics();
}, this.metricsInterval);
}
private bucketIsEmpty(payload: Payload) {
return Object.keys(payload.bucket.toggles).length === 0;
}
private getPayload(): Payload {
const bucket = { ...this.bucket, stop: new Date() };
this.bucket = this.createEmptyBucket();
return {
bucket,
appName: this.appName,
instanceId: 'browser',
};
}
}