|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { Logger } from '../utils/logger'; |
| 3 | +import { InstalledBundle, Profile, RegistrySource, SourceSyncedEvent } from '../types/registry'; |
| 4 | +import { RegistryManager } from './RegistryManager'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Telemetry service that tracks bundle lifecycle events using VS Code's |
| 8 | + * built-in TelemetryLogger infrastructure. |
| 9 | + * |
| 10 | + * Uses `vscode.env.createTelemetryLogger` with a Logger-backed sender, |
| 11 | + * so VS Code automatically respects the user's telemetry preferences |
| 12 | + * (`telemetry.telemetryLevel`): |
| 13 | + * - `all` → usage + error events are sent |
| 14 | + * - `error` → only error events are sent |
| 15 | + * - `crash` / `off` → nothing is sent |
| 16 | + * |
| 17 | + * The TelemetryLogger gates calls to the sender based on `isUsageEnabled` |
| 18 | + * and `isErrorsEnabled`, so the sender itself logs unconditionally. |
| 19 | + * Currently logs events locally — no data is sent to external servers. |
| 20 | + */ |
| 21 | +export class TelemetryService { |
| 22 | + private static instance: TelemetryService; |
| 23 | + private readonly telemetryLogger: vscode.TelemetryLogger; |
| 24 | + private disposables: vscode.Disposable[] = []; |
| 25 | + |
| 26 | + private constructor() { |
| 27 | + const logger = Logger.getInstance(); |
| 28 | + |
| 29 | + const sender: vscode.TelemetrySender = { |
| 30 | + sendEventData(eventName: string, data?: Record<string, any>): void { |
| 31 | + logger.info(`[Telemetry] ${eventName} ${JSON.stringify(data)}`); |
| 32 | + }, |
| 33 | + sendErrorData(error: Error, data?: Record<string, any>): void { |
| 34 | + logger.error(`[Telemetry] ${error.message} ${JSON.stringify(data)}`); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + this.telemetryLogger = vscode.env.createTelemetryLogger(sender); |
| 39 | + this.telemetryLogger.logUsage('telemetryService.started'); |
| 40 | + this.disposables.push(this.telemetryLogger); |
| 41 | + } |
| 42 | + |
| 43 | + public static getInstance(): TelemetryService { |
| 44 | + if (!TelemetryService.instance) { |
| 45 | + TelemetryService.instance = new TelemetryService(); |
| 46 | + } |
| 47 | + return TelemetryService.instance; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Reset the singleton instance (for testing only). |
| 52 | + */ |
| 53 | + public static resetInstance(): void { |
| 54 | + if (TelemetryService.instance) { |
| 55 | + TelemetryService.instance.dispose(); |
| 56 | + } |
| 57 | + TelemetryService.instance = undefined!; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Subscribe to RegistryManager bundle lifecycle events. |
| 62 | + * Subscriptions are owned by this service and cleaned up on dispose(). |
| 63 | + */ |
| 64 | + public subscribeToRegistryEvents(registryManager: RegistryManager): void { |
| 65 | + this.disposables.push( |
| 66 | + // Bundle events |
| 67 | + registryManager.onBundleInstalled((bundle) => this.trackBundleEvent('bundle.installed', bundle)), |
| 68 | + registryManager.onBundleUninstalled((bundleId) => this.telemetryLogger.logUsage('bundle.uninstalled', { bundleId })), |
| 69 | + registryManager.onBundleUpdated((bundle) => this.trackBundleEvent('bundle.updated', bundle)), |
| 70 | + registryManager.onBundlesInstalled((bundles) => this.telemetryLogger.logUsage('bundles.installed', { count: bundles.length, bundleIds: bundles.map(b => b.bundleId) })), |
| 71 | + registryManager.onBundlesUninstalled((bundleIds) => this.telemetryLogger.logUsage('bundles.uninstalled', { count: bundleIds.length, bundleIds })), |
| 72 | + // Profile events |
| 73 | + registryManager.onProfileActivated((profile) => this.trackProfileEvent('profile.activated', profile)), |
| 74 | + registryManager.onProfileDeactivated((profileId) => this.telemetryLogger.logUsage('profile.deactivated', { profileId })), |
| 75 | + registryManager.onProfileCreated((profile) => this.trackProfileEvent('profile.created', profile)), |
| 76 | + registryManager.onProfileUpdated((profile) => this.trackProfileEvent('profile.updated', profile)), |
| 77 | + registryManager.onProfileDeleted((profileId) => this.telemetryLogger.logUsage('profile.deleted', { profileId })), |
| 78 | + // Source events |
| 79 | + registryManager.onSourceAdded((source) => this.trackSourceEvent('source.added', source)), |
| 80 | + registryManager.onSourceRemoved((sourceId) => this.telemetryLogger.logUsage('source.removed', { sourceId })), |
| 81 | + registryManager.onSourceUpdated((sourceId) => this.telemetryLogger.logUsage('source.updated', { sourceId })), |
| 82 | + registryManager.onSourceSynced((event) => this.trackSourceSyncedEvent('source.synced', event)), |
| 83 | + // Preference events |
| 84 | + registryManager.onAutoUpdatePreferenceChanged((event) => this.telemetryLogger.logUsage('autoUpdate.preferenceChanged', { bundleId: event.bundleId, enabled: event.enabled })), |
| 85 | + registryManager.onRepositoryBundlesChanged(() => this.telemetryLogger.logUsage('repository.bundlesChanged')), |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + private trackBundleEvent(eventName: string, bundle: InstalledBundle): void { |
| 90 | + this.telemetryLogger.logUsage(eventName, { |
| 91 | + bundleId: bundle.bundleId, |
| 92 | + version: bundle.version, |
| 93 | + scope: bundle.scope, |
| 94 | + sourceType: bundle.sourceType ?? 'unknown' |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private trackProfileEvent(eventName: string, profile: Profile): void { |
| 99 | + this.telemetryLogger.logUsage(eventName, { |
| 100 | + profileId: profile.id, |
| 101 | + name: profile.name |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private trackSourceEvent(eventName: string, source: RegistrySource): void { |
| 106 | + this.telemetryLogger.logUsage(eventName, { |
| 107 | + sourceId: source.id, |
| 108 | + type: source.type |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + private trackSourceSyncedEvent(eventName: string, event: SourceSyncedEvent): void { |
| 113 | + this.telemetryLogger.logUsage(eventName, { |
| 114 | + sourceId: event.sourceId, |
| 115 | + bundleCount: event.bundleCount |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Dispose the telemetry logger and all event subscriptions. |
| 121 | + */ |
| 122 | + public dispose(): void { |
| 123 | + this.telemetryLogger.logUsage('telemetryService.stopped'); |
| 124 | + this.disposables.forEach(d => d.dispose()); |
| 125 | + this.disposables = []; |
| 126 | + } |
| 127 | +} |
0 commit comments