-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: CLI telemetry #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: CLI telemetry #220
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // replaced at build time | ||
| export const TELEMETRY_TRACKING_TOKEN = '<TELEMETRY_TRACKING_TOKEN>'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { init, type Mixpanel } from 'mixpanel'; | ||
| import { randomUUID } from 'node:crypto'; | ||
| import fs from 'node:fs'; | ||
| import * as os from 'os'; | ||
| import { TELEMETRY_TRACKING_TOKEN } from './constants'; | ||
| import { isInCi } from './utils/is-ci'; | ||
| import { isInContainer } from './utils/is-container'; | ||
| 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'; | ||
|
|
||
| /** | ||
| * Telemetry events | ||
| */ | ||
| export type TelemetryEvents = | ||
| | 'cli:start' | ||
| | 'cli:complete' | ||
| | 'cli:error' | ||
| | 'cli:command:start' | ||
| | 'cli:command:complete' | ||
| | 'cli:command:error' | ||
| | 'cli:plugin:start' | ||
| | 'cli:plugin:complete' | ||
| | 'cli:plugin:error'; | ||
|
|
||
| /** | ||
| * Utility class for sending telemetry | ||
| */ | ||
| export class Telemetry { | ||
| private readonly mixpanel: Mixpanel | undefined; | ||
| private readonly hostId = getMachineId(); | ||
| private readonly sessionid = randomUUID(); | ||
| 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 = this.getPrismaVersion(); | ||
| private readonly isDocker = isDocker(); | ||
| private readonly isWsl = isWsl(); | ||
| private readonly isContainer = isInContainer(); | ||
| private readonly isCi = isInCi; | ||
|
|
||
| constructor() { | ||
| if (process.env['DO_NOT_TRACK'] !== '1' && TELEMETRY_TRACKING_TOKEN) { | ||
| this.mixpanel = init(TELEMETRY_TRACKING_TOKEN, { | ||
| geolocate: true, | ||
| }); | ||
| } | ||
| } | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| get isTracking() { | ||
| return !!this.mixpanel; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| trackError(err: Error) { | ||
| this.track('cli:error', { | ||
| message: err.message, | ||
| stack: err.stack, | ||
| }); | ||
| } | ||
|
|
||
| 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(); | ||
| } catch (err: any) { | ||
| this.track(errorEvent, { | ||
| message: err.message, | ||
| stack: err.stack, | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ...properties, | ||
| }); | ||
| success = false; | ||
| throw err; | ||
| } finally { | ||
| this.track(completeEvent, { | ||
| duration: Date.now() - start, | ||
| success, | ||
| ...properties, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async trackCommand(command: string, action: () => Promise<void> | void) { | ||
| await this.trackSpan('cli:command:start', 'cli:command:complete', 'cli:command:error', { command }, action); | ||
| } | ||
|
|
||
| async trackCli(action: () => Promise<void> | void) { | ||
| await this.trackSpan('cli:start', 'cli:complete', 'cli:error', {}, action); | ||
| } | ||
|
|
||
| getPrismaVersion() { | ||
| try { | ||
| const packageJsonPath = import.meta.resolve('prisma/package.json'); | ||
| const packageJsonUrl = new URL(packageJsonPath); | ||
| const packageJson = JSON.parse(fs.readFileSync(packageJsonUrl, 'utf8')); | ||
| return packageJson.version; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export const telemetry = new Telemetry(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { env } from 'node:process'; | ||
| export const isInCi = | ||
| env['CI'] !== '0' && | ||
| env['CI'] !== 'false' && | ||
| ('CI' in env || 'CONTINUOUS_INTEGRATION' in env || Object.keys(env).some((key) => key.startsWith('CI_'))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import fs from 'node:fs'; | ||
| import isDocker from './is-docker'; | ||
|
|
||
| let cachedResult: boolean | undefined; | ||
|
|
||
| // Podman detection | ||
| const hasContainerEnv = () => { | ||
| try { | ||
| fs.statSync('/run/.containerenv'); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| export function isInContainer() { | ||
| // TODO: Use `??=` when targeting Node.js 16. | ||
| if (cachedResult === undefined) { | ||
| cachedResult = hasContainerEnv() || isDocker(); | ||
| } | ||
|
|
||
| return cachedResult; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copied over from https://github.com/sindresorhus/is-docker for CJS compatibility | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import fs from 'node:fs'; | ||
|
|
||
| let isDockerCached: boolean | undefined; | ||
|
|
||
| function hasDockerEnv() { | ||
| try { | ||
| fs.statSync('/.dockerenv'); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function hasDockerCGroup() { | ||
| try { | ||
| return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker'); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export default function isDocker() { | ||
| // TODO: Use `??=` when targeting Node.js 16. | ||
| if (isDockerCached === undefined) { | ||
| isDockerCached = hasDockerEnv() || hasDockerCGroup(); | ||
| } | ||
|
|
||
| return isDockerCached; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import process from 'node:process'; | ||
| import os from 'node:os'; | ||
| import fs from 'node:fs'; | ||
| export const isWsl = () => { | ||
| if (process.platform !== 'linux') { | ||
| return false; | ||
| } | ||
|
|
||
| if (os.release().toLowerCase().includes('microsoft')) { | ||
| return true; | ||
| } | ||
|
|
||
| try { | ||
| return fs.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft'); | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.