-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtelemetryTracker.ts
More file actions
73 lines (67 loc) · 2.7 KB
/
Copy pathtelemetryTracker.ts
File metadata and controls
73 lines (67 loc) · 2.7 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
import { ITelemetryCacheSync, ITelemetryCacheAsync } from '../storages/types';
import { EXCEPTION, SDK_NOT_READY } from '../utils/labels';
import { ITelemetryTracker } from './types';
import { timer } from '../utils/timeTracker/timer';
import { TOKEN_REFRESH, AUTH_REJECTION } from '../utils/constants';
import { UpdatesFromSSEEnum } from '../sync/submitters/types';
export function telemetryTrackerFactory(
telemetryCache?: ITelemetryCacheSync | ITelemetryCacheAsync,
now?: () => number
): ITelemetryTracker {
if (telemetryCache && now) {
const sessionTimer = timer(now);
return {
trackEval(method) {
const evalTimer = timer(now);
return (label) => {
switch (label) {
case EXCEPTION:
telemetryCache.recordException(method);
return; // Don't track latency on exceptions
case SDK_NOT_READY: // @ts-ignore ITelemetryCacheAsync doesn't implement the method
if (telemetryCache.recordNonReadyUsage) telemetryCache.recordNonReadyUsage();
}
telemetryCache.recordLatency(method, evalTimer());
};
},
trackHttp(operation) {
const httpTimer = timer(now);
return (error) => {
(telemetryCache as ITelemetryCacheSync).recordHttpLatency(operation, httpTimer());
if (error && error.statusCode) (telemetryCache as ITelemetryCacheSync).recordHttpError(operation, error.statusCode);
else (telemetryCache as ITelemetryCacheSync).recordSuccessfulSync(operation, Date.now());
};
},
sessionLength() { // @ts-ignore ITelemetryCacheAsync doesn't implement the method
if (telemetryCache.recordSessionLength) telemetryCache.recordSessionLength(sessionTimer());
},
streamingEvent(e, d) {
if (e === AUTH_REJECTION) {
(telemetryCache as ITelemetryCacheSync).recordAuthRejections();
} else {
(telemetryCache as ITelemetryCacheSync).recordStreamingEvents({
e, d, t: Date.now()
});
if (e === TOKEN_REFRESH) (telemetryCache as ITelemetryCacheSync).recordTokenRefreshes();
}
},
addTag(tag: string) {
// @ts-ignore
if (telemetryCache.addTag) telemetryCache.addTag(tag);
},
trackUpdatesFromSSE(type: UpdatesFromSSEEnum) {
(telemetryCache as ITelemetryCacheSync).recordUpdatesFromSSE(type);
}
};
} else { // If there is not `telemetryCache` or `now` time tracker, return a no-op telemetry tracker
const noopTrack = () => () => { };
return {
trackEval: noopTrack,
trackHttp: noopTrack,
sessionLength() { },
streamingEvent() { },
addTag() { },
trackUpdatesFromSSE() { },
};
}
}