|
| 1 | +import { createId } from '@paralleldrive/cuid2'; |
| 2 | +import { init, type Mixpanel } from 'mixpanel'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import * as os from 'os'; |
| 5 | +import { TELEMETRY_TRACKING_TOKEN } from './constants'; |
| 6 | +import { isInCi } from './utils/is-ci'; |
| 7 | +import { isInContainer } from './utils/is-container'; |
| 8 | +import isDocker from './utils/is-docker'; |
| 9 | +import { isWsl } from './utils/is-wsl'; |
| 10 | +import { getMachineId } from './utils/machine-id-utils'; |
| 11 | +import { getVersion } from './utils/version-utils'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Telemetry events |
| 15 | + */ |
| 16 | +export type TelemetryEvents = |
| 17 | + | 'cli:start' |
| 18 | + | 'cli:complete' |
| 19 | + | 'cli:error' |
| 20 | + | 'cli:command:start' |
| 21 | + | 'cli:command:complete' |
| 22 | + | 'cli:command:error' |
| 23 | + | 'cli:plugin:start' |
| 24 | + | 'cli:plugin:complete' |
| 25 | + | 'cli:plugin:error'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility class for sending telemetry |
| 29 | + */ |
| 30 | +export class Telemetry { |
| 31 | + private readonly mixpanel: Mixpanel | undefined; |
| 32 | + private readonly hostId = getMachineId(); |
| 33 | + private readonly sessionid = createId(); |
| 34 | + private readonly _os_type = os.type(); |
| 35 | + private readonly _os_release = os.release(); |
| 36 | + private readonly _os_arch = os.arch(); |
| 37 | + private readonly _os_version = os.version(); |
| 38 | + private readonly _os_platform = os.platform(); |
| 39 | + private readonly version = getVersion(); |
| 40 | + private readonly prismaVersion = this.getPrismaVersion(); |
| 41 | + private readonly isDocker = isDocker(); |
| 42 | + private readonly isWsl = isWsl(); |
| 43 | + private readonly isContainer = isInContainer(); |
| 44 | + private readonly isCi = isInCi; |
| 45 | + |
| 46 | + constructor() { |
| 47 | + if (process.env['DO_NOT_TRACK'] !== '1' && TELEMETRY_TRACKING_TOKEN) { |
| 48 | + this.mixpanel = init(TELEMETRY_TRACKING_TOKEN, { |
| 49 | + geolocate: true, |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + get isTracking() { |
| 55 | + return !!this.mixpanel; |
| 56 | + } |
| 57 | + |
| 58 | + track(event: TelemetryEvents, properties: Record<string, unknown> = {}) { |
| 59 | + if (this.mixpanel) { |
| 60 | + const payload = { |
| 61 | + distinct_id: this.hostId, |
| 62 | + session: this.sessionid, |
| 63 | + time: new Date(), |
| 64 | + $os: this._os_type, |
| 65 | + osType: this._os_type, |
| 66 | + osRelease: this._os_release, |
| 67 | + osPlatform: this._os_platform, |
| 68 | + osArch: this._os_arch, |
| 69 | + osVersion: this._os_version, |
| 70 | + nodeVersion: process.version, |
| 71 | + version: this.version, |
| 72 | + prismaVersion: this.prismaVersion, |
| 73 | + isDocker: this.isDocker, |
| 74 | + isWsl: this.isWsl, |
| 75 | + isContainer: this.isContainer, |
| 76 | + isCi: this.isCi, |
| 77 | + ...properties, |
| 78 | + }; |
| 79 | + this.mixpanel.track(event, payload); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + trackError(err: Error) { |
| 84 | + this.track('cli:error', { |
| 85 | + message: err.message, |
| 86 | + stack: err.stack, |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + async trackSpan<T>( |
| 91 | + startEvent: TelemetryEvents, |
| 92 | + completeEvent: TelemetryEvents, |
| 93 | + errorEvent: TelemetryEvents, |
| 94 | + properties: Record<string, unknown>, |
| 95 | + action: () => Promise<T> | T, |
| 96 | + ) { |
| 97 | + this.track(startEvent, properties); |
| 98 | + const start = Date.now(); |
| 99 | + let success = true; |
| 100 | + try { |
| 101 | + return await action(); |
| 102 | + } catch (err: any) { |
| 103 | + this.track(errorEvent, { |
| 104 | + message: err.message, |
| 105 | + stack: err.stack, |
| 106 | + ...properties, |
| 107 | + }); |
| 108 | + success = false; |
| 109 | + throw err; |
| 110 | + } finally { |
| 111 | + this.track(completeEvent, { |
| 112 | + duration: Date.now() - start, |
| 113 | + success, |
| 114 | + ...properties, |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + async trackCommand(command: string, action: () => Promise<void> | void) { |
| 120 | + await this.trackSpan('cli:command:start', 'cli:command:complete', 'cli:command:error', { command }, action); |
| 121 | + } |
| 122 | + |
| 123 | + async trackCli(action: () => Promise<void> | void) { |
| 124 | + await this.trackSpan('cli:start', 'cli:complete', 'cli:error', {}, action); |
| 125 | + } |
| 126 | + |
| 127 | + getPrismaVersion() { |
| 128 | + try { |
| 129 | + const packageJsonPath = import.meta.resolve('prisma/package.json'); |
| 130 | + const packageJsonUrl = new URL(packageJsonPath); |
| 131 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonUrl, 'utf8')); |
| 132 | + return packageJson.version; |
| 133 | + } catch { |
| 134 | + return undefined; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export const telemetry = new Telemetry(); |
0 commit comments