-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
PerformanceBackend.ts
49 lines (38 loc) · 1.25 KB
/
PerformanceBackend.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
import { EchoBackend, EchoEvent, EchoEventType } from '@grafana/runtime';
export interface PerformanceEventPayload {
metricName: string;
duration: number;
}
export interface PerformanceEvent extends EchoEvent<EchoEventType.Performance, PerformanceEventPayload> {}
export interface PerformanceBackendOptions {
url?: string;
}
/**
* Echo's performance metrics consumer
* Reports performance metrics to given url (TODO)
*/
export class PerformanceBackend implements EchoBackend<PerformanceEvent, PerformanceBackendOptions> {
private buffer: PerformanceEvent[] = [];
supportedEvents = [EchoEventType.Performance];
constructor(public options: PerformanceBackendOptions) {}
addEvent = (e: EchoEvent) => {
this.buffer.push(e);
};
flush = () => {
if (this.buffer.length === 0) {
return;
}
const result = {
metrics: this.buffer,
};
// Currently we don have API for sending the metrics hence loging to console in dev environment
if (process.env.NODE_ENV === 'development') {
console.log('PerformanceBackend flushing:', result);
}
this.buffer = [];
// TODO: Enable backend request when we have metrics API
// if (this.options.url) {
// backendSrv.post(this.options.url, result);
// }
};
}