import * as vscode from "vscode" ; import TelemetryReporter from "vscode-extension-telemetry" ; import getPackageInfo from "./getPackageInfo" ; export default class TelemetryAI { private static telemetryReporter : TelemetryReporter ; private static enableTelemetry : boolean | undefined ; constructor ( vscodeContext ) { TelemetryAI . telemetryReporter = this . createTelemetryReporter ( vscodeContext ) ; TelemetryAI . enableTelemetry = vscode . workspace . getConfiguration ( ) . get ( "telemetry.enableTelemetry" ) ; if ( TelemetryAI . enableTelemetry === undefined ) { TelemetryAI . enableTelemetry = true ; } } public getExtensionName ( context ) { const { extensionName } = getPackageInfo ( context ) ; return extensionName ; } public getExtensionVersionNumber ( context ) { const { extensionVersion } = getPackageInfo ( context ) ; return extensionVersion ; } public sendTelemetryIfEnabled ( eventName , properties ? , measurements ? ) { if ( TelemetryAI . enableTelemetry ) { TelemetryAI . telemetryReporter . sendTelemetryEvent ( eventName , properties , measurements ) ; } } public trackFeatureUsage ( eventName , eventProperties ? ) { this . sendTelemetryIfEnabled ( eventName , eventProperties ) ; } public runWithLatencyMeasure ( functionToRun , eventName ) { const numberOfNanosecondsInSecond = 1000000000 ; const startTime = Number ( process . hrtime . bigint ( ) ) ; functionToRun ( ) ; const latency = Number ( process . hrtime . bigint ( ) ) - startTime ; const measurement = { duration : latency / numberOfNanosecondsInSecond , } ; this . sendTelemetryIfEnabled ( eventName , { } , measurement ) ; } private createTelemetryReporter ( context ) { const { extensionName , extensionVersion , instrumentationKey , } = getPackageInfo ( context ) ; const reporter = new TelemetryReporter ( extensionName , extensionVersion , instrumentationKey ) ; context . subscriptions . push ( reporter ) ; return reporter ; } }