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
| import * as vscode from 'vscode';
export class TelemetryReporter { private static instance: TelemetryReporter; private reporter?: any; private constructor() { this.initializeReporter(); } static getInstance(): TelemetryReporter { if (!TelemetryReporter.instance) { TelemetryReporter.instance = new TelemetryReporter(); } return TelemetryReporter.instance; } private async initializeReporter() { try { const config = vscode.workspace.getConfiguration('telemetry'); const telemetryEnabled = config.get('enableTelemetry', true); if (telemetryEnabled) { const { default: Reporter } = await import('vscode-extension-telemetry'); this.reporter = new Reporter( 'your-extension-id', '1.0.0', 'your-application-insights-key' ); } } catch (error) { console.error('遥测初始化失败:', error); } } trackFeatureUsage(featureName: string, properties?: Record<string, string>) { if (!this.reporter) return; this.reporter.sendTelemetryEvent('featureUsage', { feature: featureName, timestamp: new Date().toISOString(), ...properties }); } trackError(error: Error, context?: string) { if (!this.reporter) return; this.reporter.sendTelemetryErrorEvent('error', { context: context || 'unknown', errorMessage: error.message, errorStack: error.stack || '', timestamp: new Date().toISOString() }); } trackPerformance(operation: string, duration: number, success: boolean) { if (!this.reporter) return; this.reporter.sendTelemetryEvent('performance', { operation, duration: duration.toString(), success: success.toString(), timestamp: new Date().toISOString() }); } trackConfiguration(configChanges: Record<string, any>) { if (!this.reporter) return; const sanitizedConfig = this.sanitizeConfigData(configChanges); this.reporter.sendTelemetryEvent('configuration', sanitizedConfig); } private sanitizeConfigData(config: Record<string, any>): Record<string, string> { const sanitized: Record<string, string> = {}; for (const [key, value] of Object.entries(config)) { if (key.toLowerCase().includes('password') || key.toLowerCase().includes('token') || key.toLowerCase().includes('secret')) { continue; } sanitized[key] = String(value); } return sanitized; } dispose() { if (this.reporter) { this.reporter.dispose(); } } }
export function trackCommandExecution(commandId: string) { const telemetry = TelemetryReporter.getInstance(); const startTime = Date.now(); return { success: () => { const duration = Date.now() - startTime; telemetry.trackPerformance(commandId, duration, true); telemetry.trackFeatureUsage('command', { commandId }); }, error: (error: Error) => { const duration = Date.now() - startTime; telemetry.trackPerformance(commandId, duration, false); telemetry.trackError(error, `command:${commandId}`); } }; }
|