From 641cd19c90e52ccb15b4cf02e6d4e3bd2430f438 Mon Sep 17 00:00:00 2001 From: Joshua Feingold Date: Fri, 1 Dec 2023 10:33:19 -0600 Subject: [PATCH] @W-14219242@: Fixing behavioral issues with telemetry. --- src/extension.ts | 9 +++----- src/lib/core-extension-service.ts | 34 ++++++++++++++++++++++++------- src/lib/telemetry.ts | 24 ---------------------- src/test/suite/extension.test.ts | 2 +- 4 files changed, 31 insertions(+), 38 deletions(-) delete mode 100644 src/lib/telemetry.ts diff --git a/src/extension.ts b/src/extension.ts index 1597065..0061e15 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -15,8 +15,7 @@ import {RuleResult} from './types'; import {DiagnosticManager} from './lib/diagnostics'; import {messages} from './lib/messages'; import {Fixer} from './lib/fixer'; -import { CoreExtensionService } from './lib/core-extension-service'; -import { TelemetryService } from './lib/telemetry'; +import { CoreExtensionService, TelemetryService } from './lib/core-extension-service'; import * as Constants from './lib/constants'; type RunInfo = { @@ -85,8 +84,7 @@ export async function activate(context: vscode.ExtensionContext): Promise { if (!CoreExtensionService.initialized) { @@ -61,11 +61,11 @@ export class CoreExtensionService { } /** - * Initializes a {@link TelemetryService} instance of the provided class, or a {@link NoOpTelemetryService} if none was provided + * Initializes a {@link CoreTelemetryService} instance of the provided class, or uses null if none was provided. * @param telemetryService * @param context */ - private static async initializeTelemetryService(telemetryService: TelemetryService | undefined, context: vscode.ExtensionContext): Promise { + private static async initializeTelemetryService(telemetryService: CoreTelemetryService | undefined, context: vscode.ExtensionContext): Promise { if (!telemetryService) { console.log(`Telemetry service not present in core dependency API. Using null instead.`); CoreExtensionService.telemetryService = null; @@ -79,7 +79,7 @@ export class CoreExtensionService { * * @returns The {@link TelemetryService} object exported by the Core Extension if available, else null. */ - public static getTelemetryService(): TelemetryService|null { + public static _getTelemetryService(): CoreTelemetryService|null { if (CoreExtensionService.initialized) { return CoreExtensionService.telemetryService; } @@ -87,6 +87,26 @@ export class CoreExtensionService { } } +export class TelemetryService { + + public static sendExtensionActivationEvent(hrStart: [number, number]): void { + CoreExtensionService._getTelemetryService()?.sendExtensionActivationEvent(hrStart); + } + + public static sendCommandEvent(key: string, data: Properties): void { + CoreExtensionService._getTelemetryService()?.sendCommandEvent(key, undefined, data); + } + + public static sendException(name: string, message: string, data?: Record): void { + const fullMessage = data ? message + JSON.stringify(data) : message; + CoreExtensionService._getTelemetryService()?.sendException(name, fullMessage); + } + + public static dispose(): void { + CoreExtensionService._getTelemetryService()?.dispose(); + } +} + interface Measurements { [key: string]: number; @@ -99,10 +119,10 @@ export interface Properties { /** * This interface is a subset of the TelemetryService interface from the salesforcedx-utils-vscode package. */ -interface TelemetryService { +interface CoreTelemetryService { extensionName: string; isTelemetryEnabled(): boolean; - getInstance(): TelemetryService; + getInstance(): CoreTelemetryService; initializeService(extensionContext: vscode.ExtensionContext): Promise; sendExtensionActivationEvent(hrstart: [number, number]): void; sendExtensionDeactivationEvent(): void; @@ -118,6 +138,6 @@ interface TelemetryService { interface CoreExtensionApi { services: { - TelemetryService: TelemetryService; + TelemetryService: CoreTelemetryService; } } diff --git a/src/lib/telemetry.ts b/src/lib/telemetry.ts deleted file mode 100644 index 2bb83c6..0000000 --- a/src/lib/telemetry.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * SPDX-License-Identifier: BSD-3-Clause - * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ -import { CoreExtensionService, Properties } from './core-extension-service'; - -/** - * Helper class for managing access to the core telemetry service. - */ -export class TelemetryService { - public static sendCommandEvent(key: string, data: Properties): void { - const coreTelemetryService = CoreExtensionService.getTelemetryService(); - coreTelemetryService.sendCommandEvent(key, undefined, data); - } - - public static sendException(name: string, message: string, data?: Record): void { - const coreTelemetryService = CoreExtensionService.getTelemetryService(); - message += data ? JSON.stringify(data) : ''; - coreTelemetryService?.sendException(name, message); - } -} - diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 851799f..79b3b96 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -11,7 +11,7 @@ import {SfCli} from '../../lib/sf-cli'; import Sinon = require('sinon'); import { _runAndDisplayPathless, _runAndDisplayDfa, _clearDiagnostics } from '../../extension'; import {messages} from '../../lib/messages'; -import {TelemetryService} from '../../lib/telemetry'; +import {TelemetryService} from '../../lib/core-extension-service'; import * as Constants from '../../lib/constants'; // You can import and use all API from the 'vscode' module