-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplicatorConsoleTracer.ts
54 lines (47 loc) · 1.86 KB
/
replicatorConsoleTracer.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
import { Tracer } from "jinaga";
export class ReplicatorConsoleTracer implements Tracer {
private counterAccumulation: { [key: string]: number } = {};
private counterTimeout: NodeJS.Timeout | null = null;
info(message: string): void {
// Do not output INFO messages to the console.
}
warn(message: string): void {
console.warn(`WARN: ${message}`);
}
error(error: any): void {
console.error(`ERROR: ${error}`);
}
dependency<T>(name: string, data: string, operation: () => Promise<T>): Promise<T> {
console.info(`DEPENDENCY: ${name} with data ${data}`);
return operation().then(result => {
console.info(`DEPENDENCY: ${name} completed`);
return result;
}).catch(err => {
console.error(`DEPENDENCY: ${name} failed with error ${err}`);
throw err;
});
}
metric(message: string, measurements: { [key: string]: number; }): void {
if (message === "Postgres connected" ||
message === "Postgres acquired" ||
message === "Postgres disconnected"
) {
return;
}
console.info(`METRIC: ${message} with measurements ${JSON.stringify(measurements)}`);
}
counter(name: string, value: number): void {
if (this.counterTimeout) {
this.counterAccumulation[name] = (this.counterAccumulation[name] || 0) + value;
} else {
this.counterAccumulation[name] = value;
this.counterTimeout = setTimeout(() => {
for (const [counterName, counterValue] of Object.entries(this.counterAccumulation)) {
console.info(`COUNTER: ${counterName} incremented by ${counterValue}`);
}
this.counterAccumulation = {};
this.counterTimeout = null;
}, 1000);
}
}
}