Repository navigation
Expand file tree
/
Copy pathreporter-proxy.js
More file actions
111 lines (89 loc) · 2.99 KB
/
Copy pathreporter-proxy.js
File metadata and controls
111 lines (89 loc) · 2.99 KB
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
const pjson = require('../package.json');
export const FIVE_MINUTES_IN_MILLISECONDS = 1000 * 60 * 5;
// this class allows us to call reporter methods
// before the reporter is actually loaded, since we don't want to
// assume that the metrics package will load before the GitHub package.
class ReporterProxy {
constructor() {
this.reporter = null;
this.events = [];
this.timings = [];
this.counters = [];
this.gitHubPackageVersion = pjson.version;
this.timeout = null;
}
// function that is called after the reporter is actually loaded, to
// set the reporter and send any data that have accumulated while it was loading.
setReporter(reporter) {
this.reporter = reporter;
this.events.forEach(customEvent => {
this.reporter.addCustomEvent(customEvent.eventType, customEvent.event);
});
this.events = [];
this.timings.forEach(timing => {
this.reporter.addTiming(timing.eventType, timing.durationInMilliseconds, timing.metadata);
});
this.timings = [];
this.counters.forEach(counterName => {
this.reporter.incrementCounter(counterName);
});
this.counters = [];
}
incrementCounter(counterName) {
if (this.reporter === null) {
this.startTimer();
this.counters.push(counterName);
return;
}
this.reporter.incrementCounter(counterName);
}
addTiming(eventType, durationInMilliseconds, metadata = {}) {
if (this.reporter === null) {
this.startTimer();
this.timings.push({eventType, durationInMilliseconds, metadata});
return;
}
this.reporter.addTiming(eventType, durationInMilliseconds, metadata);
}
addEvent(eventType, event) {
if (this.reporter === null) {
this.startTimer();
this.events.push({eventType, event});
return;
}
this.reporter.addCustomEvent(eventType, event);
}
startTimer() {
if (this.timeout !== null) {
return;
}
// if for some reason a user disables the metrics package, we don't want to
// just keep accumulating events in memory until the heat death of the universe.
// Use a no-op class, clear all queues, move on with our lives.
this.timeout = setTimeout(FIVE_MINUTES_IN_MILLISECONDS, () => {
if (this.reporter === null) {
this.setReporter(new FakeReporter());
this.events = [];
this.timings = [];
this.counters = [];
}
});
}
}
export const reporterProxy = new ReporterProxy();
export class FakeReporter {
addCustomEvent() {}
addTiming() {}
incrementCounter() {}
}
export function incrementCounter(counterName) {
reporterProxy.incrementCounter(counterName);
}
export function addTiming(eventType, durationInMilliseconds, metadata = {}) {
metadata.gitHubPackageVersion = reporterProxy.gitHubPackageVersion;
reporterProxy.addTiming(eventType, durationInMilliseconds, metadata);
}
export function addEvent(eventType, event) {
event.gitHubPackageVersion = reporterProxy.gitHubPackageVersion;
reporterProxy.addEvent(eventType, event);
}