telemetryService.ts 6.0 KB
Newer Older
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
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { ITelemetryService, ITelemetryInfo, ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService, combinedAppender, LogAppender, ITelemetryAppender, validateTelemetryData } from 'vs/platform/telemetry/common/telemetryUtils';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { Disposable } from 'vs/base/common/lifecycle';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ILogService } from 'vs/platform/log/common/log';
import { TelemetryService as BaseTelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ClassifiedEvent, StrictPropertyCheck, GDPRClassification } from 'vs/platform/telemetry/common/gdprTypings';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { resolveWorkbenchCommonProperties } from 'vs/platform/telemetry/browser/workbenchCommonProperties';
import { IProductService } from 'vs/platform/product/common/product';

interface IConfig {
	instrumentationKey?: string;
	endpointUrl?: string;
	emitLineDelimitedJson?: boolean;
	accountId?: string;
	sessionRenewalMs?: number;
	sessionExpirationMs?: number;
	maxBatchSizeInBytes?: number;
	maxBatchInterval?: number;
	enableDebug?: boolean;
	disableExceptionTracking?: boolean;
	disableTelemetry?: boolean;
	verboseLogging?: boolean;
	diagnosticLogInterval?: number;
	samplingPercentage?: number;
	autoTrackPageVisitTime?: boolean;
	disableAjaxTracking?: boolean;
	overridePageViewDuration?: boolean;
	maxAjaxCallsPerView?: number;
	disableDataLossAnalysis?: boolean;
	disableCorrelationHeaders?: boolean;
	correlationHeaderExcludedDomains?: string[];
	disableFlushOnBeforeUnload?: boolean;
	enableSessionStorageBuffer?: boolean;
	isCookieUseDisabled?: boolean;
	cookieDomain?: string;
	isRetryDisabled?: boolean;
	url?: string;
	isStorageUseDisabled?: boolean;
	isBeaconApiDisabled?: boolean;
	sdkExtension?: string;
	isBrowserLinkTrackingEnabled?: boolean;
	appId?: string;
	enableCorsCorrelation?: boolean;
}

declare class Microsoft {
	public static ApplicationInsights: {
		Initialization: {
			new(init: { config: IConfig }): AppInsights;
		}
	};
}

declare interface IAppInsightsClient {
	config: IConfig;

	/** Log a user action or other occurrence. */
	trackEvent: (name: string, properties?: { [key: string]: string }, measurements?: { [key: string]: number }) => void;

	/** Immediately send all queued telemetry. Synchronous. */
	flush(): void;
}

interface AppInsights {
	loadAppInsights: () => IAppInsightsClient;
}

export class WebTelemetryAppender implements ITelemetryAppender {
	private _aiClient?: IAppInsightsClient;

	constructor(aiKey: string, private _logService: ILogService) {
		const initConfig = {
			config: {
				instrumentationKey: aiKey,
				endpointUrl: 'https://vortex.data.microsoft.com/collect/v1',
				emitLineDelimitedJson: true,
				autoTrackPageVisitTime: false,
				disableExceptionTracking: true,
				disableAjaxTracking: true
			}
		};

		const appInsights = new Microsoft.ApplicationInsights.Initialization(initConfig);
		this._aiClient = appInsights.loadAppInsights();
	}

	log(eventName: string, data: any): void {
		if (!this._aiClient) {
			return;
		}

		data = validateTelemetryData(data);
		this._logService.trace(`telemetry/${eventName}`, data);

		this._aiClient.trackEvent('monacoworkbench/' + eventName, data.properties, data.measurements);
	}

107
	flush(): Promise<void> {
108 109 110 111 112 113 114 115
		if (this._aiClient) {
			return new Promise(resolve => {
				this._aiClient!.flush();
				this._aiClient = undefined;
				resolve(undefined);
			});
		}

116
		return Promise.resolve();
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}
}

export class TelemetryService extends Disposable implements ITelemetryService {

	_serviceBrand: any;

	private impl: ITelemetryService;

	constructor(
		@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
		@ILogService logService: ILogService,
		@IConfigurationService configurationService: IConfigurationService,
		@IStorageService storageService: IStorageService,
		@IProductService productService: IProductService
	) {
		super();

		const aiKey = productService.aiConfig && productService.aiConfig.asimovKey;
		if (!environmentService.isExtensionDevelopment && !environmentService.args['disable-telemetry'] && !!productService.enableTelemetry && !!aiKey) {
			const config: ITelemetryServiceConfig = {
				appender: combinedAppender(new WebTelemetryAppender(aiKey, logService), new LogAppender(logService)),
				commonProperties: resolveWorkbenchCommonProperties(storageService, productService.commit, productService.version, environmentService.configuration.machineId, environmentService.configuration.remoteAuthority),
				piiPaths: [environmentService.appRoot]
			};

			this.impl = this._register(new BaseTelemetryService(config, configurationService));
		} else {
			this.impl = NullTelemetryService;
		}
	}

	setEnabled(value: boolean): void {
		return this.impl.setEnabled(value);
	}

	get isOptedIn(): boolean {
		return this.impl.isOptedIn;
	}

	publicLog(eventName: string, data?: ITelemetryData, anonymizeFilePaths?: boolean): Promise<void> {
		return this.impl.publicLog(eventName, data, anonymizeFilePaths);
	}

	publicLog2<E extends ClassifiedEvent<T> = never, T extends GDPRClassification<T> = never>(eventName: string, data?: StrictPropertyCheck<T, E>, anonymizeFilePaths?: boolean) {
		return this.publicLog(eventName, data as ITelemetryData, anonymizeFilePaths);
	}

	getTelemetryInfo(): Promise<ITelemetryInfo> {
		return this.impl.getTelemetryInfo();
	}
}

registerSingleton(ITelemetryService, TelemetryService);