From ff225b9fcce46941c2ef47839fcfac557c4a13ea Mon Sep 17 00:00:00 2001 From: SteVen Batten <6561887+sbatten@users.noreply.github.com> Date: Tue, 2 Mar 2021 07:59:50 -0800 Subject: [PATCH] add telemetry enablement api (#117944) --- src/vs/vscode.proposed.d.ts | 4 +++ .../api/browser/mainThreadTelemetry.ts | 34 +++++++++++++++---- .../workbench/api/common/extHost.api.impl.ts | 11 ++++++ .../api/common/extHost.common.services.ts | 2 ++ .../workbench/api/common/extHost.protocol.ts | 6 ++++ .../workbench/api/common/extHostTelemetry.ts | 31 +++++++++++++++++ 6 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/vs/workbench/api/common/extHostTelemetry.ts diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 6678eca5db0..eebd670cd4d 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -2708,6 +2708,10 @@ declare module 'vscode' { namespace env { export function openExternal(target: Uri, options?: OpenExternalOptions): Thenable; + + export const isTelemetryEnabled: boolean; + + export const onDidChangeTelemetryEnabled: Event; } //#endregion diff --git a/src/vs/workbench/api/browser/mainThreadTelemetry.ts b/src/vs/workbench/api/browser/mainThreadTelemetry.ts index 3b800af7284..99b91ad20a2 100644 --- a/src/vs/workbench/api/browser/mainThreadTelemetry.ts +++ b/src/vs/workbench/api/browser/mainThreadTelemetry.ts @@ -4,24 +4,46 @@ *--------------------------------------------------------------------------------------------*/ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; -import { MainThreadTelemetryShape, MainContext, IExtHostContext } from '../common/extHost.protocol'; +import { MainThreadTelemetryShape, MainContext, IExtHostContext, ExtHostTelemetryShape, ExtHostContext } from '../common/extHost.protocol'; import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers'; import { ClassifiedEvent, StrictPropertyCheck, GDPRClassification } from 'vs/platform/telemetry/common/gdprTypings'; +import { Disposable } from 'vs/base/common/lifecycle'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; @extHostNamedCustomer(MainContext.MainThreadTelemetry) -export class MainThreadTelemetry implements MainThreadTelemetryShape { +export class MainThreadTelemetry extends Disposable implements MainThreadTelemetryShape { + private readonly _proxy: ExtHostTelemetryShape; private static readonly _name = 'pluginHostTelemetry'; constructor( extHostContext: IExtHostContext, - @ITelemetryService private readonly _telemetryService: ITelemetryService + @ITelemetryService private readonly _telemetryService: ITelemetryService, + @IConfigurationService private readonly _configurationService: IConfigurationService, + @IEnvironmentService private readonly _environmenService: IEnvironmentService, ) { - // + super(); + + this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostTelemetry); + + if (!this._environmenService.disableTelemetry) { + this._register(this._configurationService.onDidChangeConfiguration(e => { + if (e.affectedKeys.includes('telemetry.enableTelemetry')) { + this._proxy.$onDidChangeTelemetryEnabled(this.telemetryEnabled); + } + })); + } + + this._proxy.$initializeTelemetryEnabled(this.telemetryEnabled); } - dispose(): void { - // + private get telemetryEnabled(): boolean { + if (this._environmenService.disableTelemetry) { + return false; + } + + return !!this._configurationService.getValue('telemetry.enableTelemetry'); } $publicLog(eventName: string, data: any = Object.create(null)): void { diff --git a/src/vs/workbench/api/common/extHost.api.impl.ts b/src/vs/workbench/api/common/extHost.api.impl.ts index ca370ab84b7..14841f2511c 100644 --- a/src/vs/workbench/api/common/extHost.api.impl.ts +++ b/src/vs/workbench/api/common/extHost.api.impl.ts @@ -85,6 +85,7 @@ import { ExtHostTesting } from 'vs/workbench/api/common/extHostTesting'; import { ExtHostUriOpeners } from 'vs/workbench/api/common/extHostUriOpener'; import { IExtHostSecretState } from 'vs/workbench/api/common/exHostSecretState'; import { ExtHostEditorTabs } from 'vs/workbench/api/common/extHostEditorTabs'; +import { IExtHostTelemetry } from 'vs/workbench/api/common/extHostTelemetry'; export interface IExtensionApiFactory { (extension: IExtensionDescription, registry: ExtensionDescriptionRegistry, configProvider: ExtHostConfigProvider): typeof vscode; @@ -101,6 +102,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I const extHostConsumerFileSystem = accessor.get(IExtHostConsumerFileSystem); const extensionService = accessor.get(IExtHostExtensionService); const extHostWorkspace = accessor.get(IExtHostWorkspace); + const extHostTelemetry = accessor.get(IExtHostTelemetry); const extHostConfiguration = accessor.get(IExtHostConfiguration); const uriTransformer = accessor.get(IURITransformerService); const rpcProtocol = accessor.get(IExtHostRpcService); @@ -122,6 +124,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I rpcProtocol.set(ExtHostContext.ExtHostTunnelService, extHostTunnelService); rpcProtocol.set(ExtHostContext.ExtHostWindow, extHostWindow); rpcProtocol.set(ExtHostContext.ExtHostSecretState, extHostSecretState); + rpcProtocol.set(ExtHostContext.ExtHostTelemetry, extHostTelemetry); // automatically create and register addressable instances const extHostDecorations = rpcProtocol.set(ExtHostContext.ExtHostDecorations, accessor.get(IExtHostDecorations)); @@ -292,6 +295,14 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I get shell() { return extHostTerminalService.getDefaultShell(false, configProvider); }, + get isTelemetryEnabled() { + checkProposedApiEnabled(extension); + return extHostTelemetry.getTelemetryEnabled(); + }, + get onDidChangeTelemetryEnabled(): Event { + checkProposedApiEnabled(extension); + return extHostTelemetry.onDidChangeTelemetryEnabled; + }, openExternal(uri: URI, options?: { allowContributedOpeners?: boolean | string; }) { return extHostWindow.openUri(uri, { allowTunneling: !!initData.remote.authority, diff --git a/src/vs/workbench/api/common/extHost.common.services.ts b/src/vs/workbench/api/common/extHost.common.services.ts index b1fe830b257..bf894f0cfb4 100644 --- a/src/vs/workbench/api/common/extHost.common.services.ts +++ b/src/vs/workbench/api/common/extHost.common.services.ts @@ -22,6 +22,7 @@ import { IExtHostWindow, ExtHostWindow } from 'vs/workbench/api/common/extHostWi import { IExtHostConsumerFileSystem, ExtHostConsumerFileSystem } from 'vs/workbench/api/common/extHostFileSystemConsumer'; import { IExtHostFileSystemInfo, ExtHostFileSystemInfo } from 'vs/workbench/api/common/extHostFileSystemInfo'; import { IExtHostSecretState, ExtHostSecretState } from 'vs/workbench/api/common/exHostSecretState'; +import { ExtHostTelemetry, IExtHostTelemetry } from 'vs/workbench/api/common/extHostTelemetry'; registerSingleton(IExtensionStoragePaths, ExtensionStoragePaths); registerSingleton(IExtHostApiDeprecationService, ExtHostApiDeprecationService); @@ -41,3 +42,4 @@ registerSingleton(IExtHostTunnelService, ExtHostTunnelService); registerSingleton(IExtHostWindow, ExtHostWindow); registerSingleton(IExtHostWorkspace, ExtHostWorkspace); registerSingleton(IExtHostSecretState, ExtHostSecretState); +registerSingleton(IExtHostTelemetry, ExtHostTelemetry); diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 4b12d94eb0a..7509513e300 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1536,6 +1536,11 @@ export interface ExtHostQuickOpenShape { $onDidHide(sessionId: number): void; } +export interface ExtHostTelemetryShape { + $initializeTelemetryEnabled(enabled: boolean): void; + $onDidChangeTelemetryEnabled(enabled: boolean): void; +} + export interface IShellLaunchConfigDto { name?: string; executable?: string; @@ -1962,4 +1967,5 @@ export const ExtHostContext = { ExtHostAuthentication: createMainId('ExtHostAuthentication'), ExtHostTimeline: createMainId('ExtHostTimeline'), ExtHostTesting: createMainId('ExtHostTesting'), + ExtHostTelemetry: createMainId('ExtHostTelemetry'), }; diff --git a/src/vs/workbench/api/common/extHostTelemetry.ts b/src/vs/workbench/api/common/extHostTelemetry.ts new file mode 100644 index 00000000000..2d14c450b17 --- /dev/null +++ b/src/vs/workbench/api/common/extHostTelemetry.ts @@ -0,0 +1,31 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; +import { Event, Emitter } from 'vs/base/common/event'; +import { ExtHostTelemetryShape } from 'vs/workbench/api/common/extHost.protocol'; + +export class ExtHostTelemetry implements ExtHostTelemetryShape { + private readonly _onDidChangeTelemetryEnabled = new Emitter(); + readonly onDidChangeTelemetryEnabled: Event = this._onDidChangeTelemetryEnabled.event; + + private _enabled: boolean = false; + + getTelemetryEnabled(): boolean { + return this._enabled; + } + + $initializeTelemetryEnabled(enabled: boolean): void { + this._enabled = enabled; + } + + $onDidChangeTelemetryEnabled(enabled: boolean): void { + this._enabled = enabled; + this._onDidChangeTelemetryEnabled.fire(enabled); + } +} + +export const IExtHostTelemetry = createDecorator('IExtHostTelemetry'); +export interface IExtHostTelemetry extends ExtHostTelemetry, ExtHostTelemetryShape { }