2022-05-03 13:07:28 +00:00
|
|
|
import { isCI } from 'ci-info';
|
|
|
|
import debug from 'debug';
|
2022-06-27 21:16:07 +00:00
|
|
|
import { randomBytes } from 'node:crypto';
|
|
|
|
import * as KEY from './config-keys.js';
|
|
|
|
import { GlobalConfig } from './config.js';
|
2022-05-03 13:07:28 +00:00
|
|
|
import { post } from './post.js';
|
2022-06-27 21:16:07 +00:00
|
|
|
import { getProjectInfo, ProjectInfo } from './project-info.js';
|
|
|
|
import { getSystemInfo, SystemInfo } from './system-info.js';
|
2022-05-03 13:07:28 +00:00
|
|
|
|
2022-06-29 21:54:33 +00:00
|
|
|
export type AstroTelemetryOptions = { astroVersion: string; viteVersion: string };
|
2022-05-03 13:07:28 +00:00
|
|
|
export type TelemetryEvent = { eventName: string; payload: Record<string, any> };
|
2022-07-07 18:12:31 +00:00
|
|
|
|
2022-07-07 21:08:44 +00:00
|
|
|
type EventMeta = SystemInfo;
|
2022-07-07 18:12:31 +00:00
|
|
|
interface EventContext extends ProjectInfo {
|
2022-05-03 13:07:28 +00:00
|
|
|
anonymousId: string;
|
2022-06-27 21:16:07 +00:00
|
|
|
anonymousSessionId: string;
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
export class AstroTelemetry {
|
2022-06-27 21:16:07 +00:00
|
|
|
private _anonymousSessionId: string | undefined;
|
|
|
|
private _anonymousProjectInfo: ProjectInfo | undefined;
|
|
|
|
private config = new GlobalConfig({ name: 'astro' });
|
2022-05-03 13:07:28 +00:00
|
|
|
private debug = debug('astro:telemetry');
|
|
|
|
|
|
|
|
private get astroVersion() {
|
2022-06-29 21:54:33 +00:00
|
|
|
return this.opts.astroVersion;
|
|
|
|
}
|
|
|
|
private get viteVersion() {
|
|
|
|
return this.opts.viteVersion;
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
private get ASTRO_TELEMETRY_DISABLED() {
|
|
|
|
return process.env.ASTRO_TELEMETRY_DISABLED;
|
|
|
|
}
|
|
|
|
private get TELEMETRY_DISABLED() {
|
|
|
|
return process.env.TELEMETRY_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(private opts: AstroTelemetryOptions) {
|
2022-05-03 19:29:34 +00:00
|
|
|
// TODO: When the process exits, flush any queued promises
|
2022-06-27 21:16:07 +00:00
|
|
|
// This caused a "cannot exist astro" error when it ran, so it was removed.
|
2022-05-03 19:29:34 +00:00
|
|
|
// process.on('SIGINT', () => this.flush());
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
/**
|
|
|
|
* Get value from either the global config or the provided fallback.
|
|
|
|
* If value is not set, the fallback is saved to the global config,
|
|
|
|
* persisted for later sessions.
|
|
|
|
*/
|
|
|
|
private getConfigWithFallback<T>(key: string, getValue: () => T): T {
|
|
|
|
const currentValue = this.config.get(key);
|
|
|
|
if (currentValue) {
|
|
|
|
return currentValue;
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
2022-06-27 21:16:07 +00:00
|
|
|
const newValue = getValue();
|
|
|
|
this.config.set(key, newValue);
|
|
|
|
return newValue;
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private get enabled(): boolean {
|
2022-06-27 21:16:07 +00:00
|
|
|
return this.getConfigWithFallback(KEY.TELEMETRY_ENABLED, () => true);
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
private get notifyDate(): string {
|
|
|
|
return this.getConfigWithFallback(KEY.TELEMETRY_NOTIFY_DATE, () => '');
|
2022-06-14 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
private get anonymousId(): string {
|
|
|
|
return this.getConfigWithFallback(KEY.TELEMETRY_ID, () => randomBytes(32).toString('hex'));
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
private get anonymousSessionId(): string {
|
|
|
|
// NOTE(fks): this value isn't global, so it can't use getConfigWithFallback().
|
|
|
|
this._anonymousSessionId = this._anonymousSessionId || randomBytes(32).toString('hex');
|
|
|
|
return this._anonymousSessionId;
|
2022-05-03 13:07:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
private get anonymousProjectInfo(): ProjectInfo {
|
|
|
|
// NOTE(fks): this value isn't global, so it can't use getConfigWithFallback().
|
|
|
|
this._anonymousProjectInfo = this._anonymousProjectInfo || getProjectInfo(isCI);
|
|
|
|
return this._anonymousProjectInfo;
|
2022-06-14 20:30:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 13:07:28 +00:00
|
|
|
private get isDisabled(): boolean {
|
|
|
|
if (Boolean(this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.enabled === false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setEnabled(value: boolean) {
|
|
|
|
this.config.set(KEY.TELEMETRY_ENABLED, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
return this.config.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
async notify(callback: () => Promise<boolean>) {
|
|
|
|
if (this.isDisabled || isCI) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The end-user has already been notified about our telemetry integration!
|
|
|
|
// Don't bother them about it again.
|
|
|
|
// In the event of significant changes, we should invalidate old dates.
|
|
|
|
if (this.notifyDate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const enabled = await callback();
|
|
|
|
this.config.set(KEY.TELEMETRY_NOTIFY_DATE, Date.now().toString());
|
|
|
|
this.config.set(KEY.TELEMETRY_ENABLED, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
async record(event: TelemetryEvent | TelemetryEvent[] = []) {
|
|
|
|
const events: TelemetryEvent[] = Array.isArray(event) ? event : [event];
|
|
|
|
if (events.length < 1) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip recording telemetry if the feature is disabled
|
|
|
|
if (this.isDisabled) {
|
2022-07-07 18:12:31 +00:00
|
|
|
this.debug('telemetry disabled');
|
2022-05-03 13:07:28 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
const meta: EventMeta = {
|
2022-06-29 21:54:33 +00:00
|
|
|
...getSystemInfo({ astroVersion: this.astroVersion, viteVersion: this.viteVersion }),
|
2022-06-27 21:16:07 +00:00
|
|
|
};
|
|
|
|
|
2022-05-03 13:07:28 +00:00
|
|
|
const context: EventContext = {
|
2022-07-07 18:12:31 +00:00
|
|
|
...this.anonymousProjectInfo,
|
2022-05-03 13:07:28 +00:00
|
|
|
anonymousId: this.anonymousId,
|
2022-06-27 21:16:07 +00:00
|
|
|
anonymousSessionId: this.anonymousSessionId,
|
2022-05-03 13:07:28 +00:00
|
|
|
};
|
|
|
|
|
2022-06-29 21:54:33 +00:00
|
|
|
// Every CI session also creates a new user, which blows up telemetry.
|
|
|
|
// To solve this, we track all CI runs under a single "CI" anonymousId.
|
|
|
|
if (meta.isCI) {
|
|
|
|
context.anonymousId = `CI.${meta.ciName || 'UNKNOWN'}`;
|
|
|
|
}
|
|
|
|
|
2022-07-07 18:12:31 +00:00
|
|
|
if (this.debug.enabled) {
|
|
|
|
// Print to standard error to simplify selecting the output
|
|
|
|
this.debug({ context, meta });
|
|
|
|
this.debug(JSON.stringify(events, null, 2));
|
|
|
|
// Do not send the telemetry data if debugging. Users may use this feature
|
|
|
|
// to preview what data would be sent.
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2022-06-27 21:16:07 +00:00
|
|
|
return post({
|
2022-05-03 13:07:28 +00:00
|
|
|
context,
|
|
|
|
meta,
|
|
|
|
events,
|
2022-06-27 21:16:07 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
// Log the error to the debugger, but otherwise do nothing.
|
|
|
|
this.debug(`Error sending event: ${err.message}`);
|
2022-05-03 13:07:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|