/
index.ts
123 lines (103 loc) · 2.8 KB
/
index.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
import {
AnalyticsTracker,
IAggregateStats,
ITelemetryTagsPayload,
ITelemetryOptsPayload,
IDefaultTelemetryTagsPayload,
} from "./telemetry"
import { Request, Response } from "express"
import { createFlush } from "./create-flush"
const instance = new AnalyticsTracker()
const flush = createFlush(instance.isTrackingEnabled())
process.on(`exit`, flush)
// For long running commands we want to occasionally flush the data
//
// The data is also sent on exit.
const intervalDuration = process.env.TELEMETRY_BUFFER_INTERVAL
const interval =
intervalDuration && Number.isFinite(+intervalDuration)
? Math.max(Number(intervalDuration), 1000)
: 10 * 60 * 1000 // 10 min
function tick(): void {
flush()
.catch(console.error)
.then(() => setTimeout(tick, interval))
}
export function trackFeatureIsUsed(name: string): void {
instance.trackFeatureIsUsed(name)
}
export function trackCli(
input: string | Array<string>,
tags?: ITelemetryTagsPayload,
opts?: ITelemetryOptsPayload
): void {
instance.captureEvent(input, tags, opts)
}
export function trackError(input: string, tags?: ITelemetryTagsPayload): void {
instance.captureError(input, tags)
}
export function trackBuildError(
input: string,
tags?: ITelemetryTagsPayload
): void {
instance.captureBuildError(input, tags)
}
export function setDefaultTags(tags: IDefaultTelemetryTagsPayload): void {
instance.decorateAll(tags)
}
export function decorateEvent(
event: string,
tags?: Record<string, unknown>
): void {
instance.decorateNextEvent(event, tags)
}
export function setTelemetryEnabled(enabled: boolean): void {
instance.setTelemetryEnabled(enabled)
}
export function startBackgroundUpdate(): void {
setTimeout(tick, interval)
}
export function isTrackingEnabled(): boolean {
return instance.isTrackingEnabled()
}
export function aggregateStats(data: Array<number>): IAggregateStats {
return instance.aggregateStats(data)
}
export function addSiteMeasurement(event: string, obj): void {
instance.addSiteMeasurement(event, obj)
}
export function expressMiddleware(source: string) {
return function (req: Request, _res: Response, next): void {
try {
instance.trackActivity(`${source}_ACTIVE`, {
userAgent: req.headers[`user-agent`],
})
} catch (e) {
// ignore
}
next()
}
}
// Internal
export function setDefaultComponentId(componentId: string): void {
instance.componentId = componentId
}
export function setGatsbyCliVersion(version: string): void {
instance.gatsbyCliVersion = version
}
module.exports = {
trackFeatureIsUsed,
trackCli,
trackError,
trackBuildError,
setDefaultTags,
decorateEvent,
setTelemetryEnabled,
startBackgroundUpdate,
isTrackingEnabled,
aggregateStats,
addSiteMeasurement,
expressMiddleware,
setDefaultComponentId,
setGatsbyCliVersion,
}