-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmetrics.ts
149 lines (131 loc) · 3.57 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
// Simplified version of: https://github.com/Unleash/unleash-client-node/blob/main/src/metrics.ts
export interface MetricsOptions {
appName: string;
metricsInterval: number;
disableMetrics?: boolean;
url: string;
clientKey: string;
fetch: any;
headerName: string
}
interface Bucket {
start: Date;
stop: Date | null;
toggles: { [s: string]: { yes: number; no: number } };
}
interface Payload {
bucket: Bucket;
appName: string;
instanceId: string;
}
export default class Metrics {
private bucket: Bucket;
private appName: string;
private metricsInterval: number;
private disabled: boolean;
private url: string;
private clientKey: string;
private timer: any;
private fetch: any;
private headerName: string
constructor({
appName,
metricsInterval,
disableMetrics = false,
url,
clientKey,
fetch,
headerName
}: MetricsOptions) {
this.disabled = disableMetrics;
this.metricsInterval = metricsInterval * 1000;
this.appName = appName;
this.url = url;
this.clientKey = clientKey;
this.bucket = this.createEmptyBucket();
this.fetch = fetch;
this.headerName = headerName
}
public start() {
if (this.disabled) {
return false;
}
if (
typeof this.metricsInterval === 'number' &&
this.metricsInterval > 0
) {
// send first metrics after two seconds.
setTimeout(() => {
this.startTimer();
this.sendMetrics();
}, 2000);
}
}
public stop() {
if (this.timer) {
clearTimeout(this.timer);
delete this.timer;
}
}
public createEmptyBucket(): Bucket {
return {
start: new Date(),
stop: null,
toggles: {},
};
}
public async sendMetrics(): Promise<void> {
/* istanbul ignore next if */
const url = `${this.url}/client/metrics`;
const payload = this.getPayload();
if (this.bucketIsEmpty(payload)) {
return;
}
await this.fetch(url, {
cache: 'no-cache',
method: 'POST',
headers: {
[this.headerName]: this.clientKey,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});
}
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;
}
private assertBucket(name: string) {
if (this.disabled || !this.bucket) {
return false;
}
if (!this.bucket.toggles[name]) {
this.bucket.toggles[name] = {
yes: 0,
no: 0,
};
}
}
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',
};
}
}