-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathtelemetry.ts
More file actions
154 lines (144 loc) · 4.99 KB
/
telemetry.ts
File metadata and controls
154 lines (144 loc) · 4.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { createId } from '@paralleldrive/cuid2';
import { getPrismaVersion } from '@zenstackhq/sdk/prisma';
import { sleep } from '@zenstackhq/runtime/local-helpers';
import exitHook from 'async-exit-hook';
import { CommanderError } from 'commander';
import { init, Mixpanel } from 'mixpanel';
import * as os from 'os';
import { CliError } from './cli/cli-error';
import { TELEMETRY_TRACKING_TOKEN } from './constants';
import isDocker from './utils/is-docker';
import { isWsl } from './utils/is-wsl';
import { getMachineId } from './utils/machine-id-utils';
import { getVersion } from './utils/version-utils';
import { isInCi } from './utils/is-ci';
import { isInContainer } from './utils/is-container';
/**
* Telemetry events
*/
export type TelemetryEvents =
| 'cli:start'
| 'cli:complete'
| 'cli:error'
| 'cli:crash'
| 'cli:command:start'
| 'cli:command:complete'
| 'cli:command:error'
| 'cli:plugin:start'
| 'cli:plugin:complete'
| 'cli:plugin:error'
| 'prisma:error';
/**
* Utility class for sending telemetry
*/
export class Telemetry {
private readonly mixpanel: Mixpanel | undefined;
private readonly hostId = getMachineId();
private readonly sessionid = createId();
private readonly _os_type = os.type();
private readonly _os_release = os.release();
private readonly _os_arch = os.arch();
private readonly _os_version = os.version();
private readonly _os_platform = os.platform();
private readonly version = getVersion();
private readonly prismaVersion = getPrismaVersion();
private readonly isDocker = isDocker();
private readonly isWsl = isWsl();
private readonly isContainer = isInContainer();
private readonly isCi = isInCi;
private exitWait = 200;
constructor() {
if (process.env.DO_NOT_TRACK !== '1' && TELEMETRY_TRACKING_TOKEN) {
this.mixpanel = init(TELEMETRY_TRACKING_TOKEN, {
geolocate: true,
});
}
exitHook(async (callback) => {
if (this.mixpanel) {
// a small delay to ensure telemetry is sent
await sleep(this.exitWait);
}
callback();
});
const errorHandler = async (err: Error) => {
if (err instanceof CliError || err instanceof CommanderError) {
this.track('cli:error', {
message: err.message,
stack: err.stack,
});
if (this.mixpanel) {
// a small delay to ensure telemetry is sent
await sleep(this.exitWait);
}
// error already logged
} else {
console.error('\nAn unexpected error occurred:\n', err);
this.track('cli:crash', {
message: err.message,
stack: err.stack,
});
if (this.mixpanel) {
// a small delay to ensure telemetry is sent
await sleep(this.exitWait);
}
}
process.exit(1);
};
exitHook.unhandledRejectionHandler(errorHandler);
exitHook.uncaughtExceptionHandler(errorHandler);
}
track(event: TelemetryEvents, properties: Record<string, unknown> = {}) {
if (this.mixpanel) {
const payload = {
distinct_id: this.hostId,
session: this.sessionid,
time: new Date(),
$os: this._os_type,
osType: this._os_type,
osRelease: this._os_release,
osPlatform: this._os_platform,
osArch: this._os_arch,
osVersion: this._os_version,
nodeVersion: process.version,
version: this.version,
prismaVersion: this.prismaVersion,
isDocker: this.isDocker,
isWsl: this.isWsl,
isContainer: this.isContainer,
isCi: this.isCi,
...properties,
};
this.mixpanel.track(event, payload);
}
}
async trackSpan<T>(
startEvent: TelemetryEvents,
completeEvent: TelemetryEvents,
errorEvent: TelemetryEvents,
properties: Record<string, unknown>,
action: () => Promise<T> | T
) {
this.track(startEvent, properties);
const start = Date.now();
let success = true;
try {
return await action();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
this.track(errorEvent, {
message: err.message,
stack: err.stack,
...properties,
});
success = false;
throw err;
} finally {
this.track(completeEvent, {
duration: Date.now() - start,
success,
...properties,
});
}
}
}
export default new Telemetry();