src/vs/platform/telemetry/common/telemetryService.ts
366 LOC · 272 covered · 94 uncovered · 26 ranges · 1484 concepts · 2 introducers · 685 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
agentHostTelemetryService.ts ×29
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { DisposableStore } from '../../../base/common/lifecycle.js';
import { mixin } from '../../../base/common/objects.js';
import { isWeb } from '../../../base/common/platform.js';
import { PolicyCategory } from '../../../base/common/policy.js';
import { escapeRegExpCharacters } from '../../../base/common/strings.js';
import { localize } from '../../../nls.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import { ConfigurationScope, Extensions, IConfigurationRegistry } from '../../configuration/common/configurationRegistry.js';
import { IMeteredConnectionService } from '../../meteredConnection/common/meteredConnection.js';
import product from '../../product/common/product.js';
import { IProductService } from '../../product/common/productService.js';
import { Registry } from '../../registry/common/platform.js';
import { ClassifiedEvent, IGDPRProperty, OmitMetadata, StrictPropertyCheck } from './gdprTypings.js';
import { ITelemetryData, ITelemetryService, TelemetryConfiguration, TelemetryLevel, TELEMETRY_CRASH_REPORTER_SETTING_ID, TELEMETRY_OLD_SETTING_ID, TELEMETRY_SECTION_ID, TELEMETRY_SETTING_ID, ICommonProperties } from './telemetry.js';
import { cleanData, getTelemetryLevel, ITelemetryAppender, TelemetryTrustedValue } from './telemetryUtils.js';
export interface ITelemetryServiceConfig {
appenders: ITelemetryAppender[];
sendErrorTelemetry?: boolean;
commonProperties?: ICommonProperties;
piiPaths?: string[];
/**
* If true, telemetry events will be buffered until setExperimentProperty is called
* (up to 10 seconds) to ensure experiment context is attached to all events.
*/
waitForExperimentProperties?: boolean;
/**
* If provided, telemetry events will be dropped when the connection is metered.
*/
meteredConnectionService?: IMeteredConnectionService;
}
interface IPendingEvent {
eventName: string;
eventLevel: TelemetryLevel;
data: ITelemetryData | undefined;
}
export class TelemetryService implements ITelemetryService {
static readonly IDLE_START_EVENT_NAME = 'UserIdleStart';
static readonly IDLE_STOP_EVENT_NAME = 'UserIdleStop';
private static readonly BUFFER_FLUSH_TIMEOUT = 10000; // 10 seconds
private static readonly MAX_BUFFER_SIZE = 1000;
declare readonly _serviceBrand: undefined;
readonly sessionId: string;
readonly machineId: string;
readonly sqmId: string;
readonly devDeviceId: string;
readonly firstSessionDate: string;
readonly msftInternal: boolean | undefined;
private _appenders: ITelemetryAppender[];
private _commonProperties: ICommonProperties;
private _experimentProperties: { [name: string]: string | TelemetryTrustedValue<string> } = {};
private _piiPaths: string[];
private _telemetryLevel: TelemetryLevel;
private _sendErrorTelemetry: boolean;
private readonly _meteredConnectionService: IMeteredConnectionService | undefined;
private _pendingEvents: IPendingEvent[] = [];
private _isExperimentPropertySet = false;
private _flushTimeout: ReturnType<typeof setTimeout> | undefined;
private readonly _disposables = new DisposableStore();
private _cleanupPatterns: RegExp[] = [];
constructor(
@IConfigurationService private _configurationService: IConfigurationService,
@IProductService private _productService: IProductService
) {
this._appenders = config.appenders;
this._commonProperties = config.commonProperties ?? Object.create(null);
this.sessionId = this._commonProperties['sessionID'] as string;
this.machineId = this._commonProperties['common.machineId'] as string;
this.sqmId = this._commonProperties['common.sqmId'] as string;
this.devDeviceId = this._commonProperties['common.devDeviceId'] as string;
this.firstSessionDate = this._commonProperties['common.firstSessionDate'] as string;
this.msftInternal = this._commonProperties['common.msftInternal'] as boolean | undefined;
this._piiPaths = config.piiPaths || [];
this._telemetryLevel = TelemetryLevel.USAGE;
this._sendErrorTelemetry = !!config.sendErrorTelemetry;
this._meteredConnectionService = config.meteredConnectionService;
// static cleanup pattern for: `vscode-file:///DANGEROUS/PATH/resources/app/Useful/Information`
this._cleanupPatterns = [/(vscode-)?file:\/\/.*?\/resources\/app\//gi];
for (const piiPath of this._piiPaths) {
this._cleanupPatterns.push(new RegExp(escapeRegExpCharacters(piiPath), 'gi'));
if (piiPath.indexOf('\\') >= 0) {
this._cleanupPatterns.push(new RegExp(escapeRegExpCharacters(piiPath.replace(/\\/g, '/')), 'gi'));
}
this._updateTelemetryLevel();
this._disposables.add(this._configurationService.onDidChangeConfiguration(e => {
// Check on the telemetry settings and update the state if changed
const affectsTelemetryConfig =
e.affectsConfiguration(TELEMETRY_SETTING_ID)
|| e.affectsConfiguration(TELEMETRY_OLD_SETTING_ID)
|| e.affectsConfiguration(TELEMETRY_CRASH_REPORTER_SETTING_ID);
if (affectsTelemetryConfig) {
this._updateTelemetryLevel();
}
// Buffer events until experiment properties are set (or timeout expires).
// This ensures early events include experiment context when available.
if (config.waitForExperimentProperties) {
this._flushTimeout = setTimeout(() => this._flushPendingEvents(), TelemetryService.BUFFER_FLUSH_TIMEOUT);
this._isExperimentPropertySet = true;
}
}
setExperimentProperty(name: string, value: string): void {
this._experimentProperties[name] = new TelemetryTrustedValue(value);
// On first call, flush all pending events that were buffered waiting for experiment properties
if (!this._isExperimentPropertySet) {
this._flushPendingEvents();
}
}
setCommonProperty(name: string, value: string | boolean): void {
this._commonProperties[name] = value;
}
private _flushPendingEvents(): void {
return;
}
this._isExperimentPropertySet = true;
if (this._flushTimeout !== undefined) {
clearTimeout(this._flushTimeout);
this._flushTimeout = undefined;
}
// Send all buffered events now that experiment properties are available
for (const event of this._pendingEvents) {
this._doLog(event.eventName, event.eventLevel, event.data);
}
this._pendingEvents = [];
private _updateTelemetryLevel(): void {
const collectableTelemetry = this._productService.enabledTelemetryLevels;
// Also ensure that error telemetry is respecting the product configuration for collectable telemetry
if (collectableTelemetry) {
this._sendErrorTelemetry = this.sendErrorTelemetry ? collectableTelemetry.error : false;
// Make sure the telemetry level from the service is the minimum of the config and product
const maxCollectableTelemetryLevel = collectableTelemetry.usage ? TelemetryLevel.USAGE : collectableTelemetry.error ? TelemetryLevel.ERROR : TelemetryLevel.NONE;
level = Math.min(level, maxCollectableTelemetryLevel);
}
this._telemetryLevel = level;
}
get sendErrorTelemetry(): boolean {
return this._sendErrorTelemetry;
}
get telemetryLevel(): TelemetryLevel {
return this._telemetryLevel;
}
dispose(): void {
this._flushPendingEvents();
this._disposables.dispose();
}
private _log(eventName: string, eventLevel: TelemetryLevel, data?: ITelemetryData) {
// don't send events when the user is optout
if (this._telemetryLevel < eventLevel) {
return;
}
// Don't send events when the connection is metered
if (this._meteredConnectionService?.isConnectionMetered) {
return;
}
// Buffer events until experiment properties are set (or timeout expires)
if (!this._isExperimentPropertySet) {
if (this._pendingEvents.length < TelemetryService.MAX_BUFFER_SIZE) {
this._pendingEvents.push({ eventName, eventLevel, data });
}
return;
}
this._doLog(eventName, eventLevel, data);
}
private _doLog(eventName: string, eventLevel: TelemetryLevel, data?: ITelemetryData) {
// add experiment properties
data = mixin(data, this._experimentProperties);
// remove all PII from data
data = cleanData(data, this._cleanupPatterns);
// add common properties
data = mixin(data, this._commonProperties);
// tag error-level events so the backend can identify them generically
if (eventLevel === TelemetryLevel.ERROR) {
data = { ...data, 'isError': true };
}
// Log to the appenders of sufficient level
this._appenders.forEach(a => a.log(eventName, data ?? {}));
}
publicLog(eventName: string, data?: ITelemetryData) {
this._log(eventName, TelemetryLevel.USAGE, data);
}
publicLog2<E extends ClassifiedEvent<OmitMetadata<T>> = never, T extends IGDPRProperty = never>(eventName: string, data?: StrictPropertyCheck<T, E>) {
this.publicLog(eventName, data as ITelemetryData);
}
publicLogError(errorEventName: string, data?: ITelemetryData) {
if (!this._sendErrorTelemetry) {
return;
}
// Send error event and anonymize paths
this._log(errorEventName, TelemetryLevel.ERROR, data);
}
publicLogError2<E extends ClassifiedEvent<OmitMetadata<T>> = never, T extends IGDPRProperty = never>(eventName: string, data?: StrictPropertyCheck<T, E>) {
this.publicLogError(eventName, data as ITelemetryData);
}
function getTelemetryLevelSettingDescription(): string {
const telemetryText = localize('telemetry.telemetryLevelMd', "Controls {0} telemetry, first-party extension telemetry, and participating third-party extension telemetry. Some third party extensions might not respect this setting. Consult the specific extension's documentation to be sure. Telemetry helps us better understand how {0} is performing, where improvements need to be made, and how features are being used.", product.nameLong);
const externalLinksStatement = !product.privacyStatementUrl ?
localize("telemetry.docsStatement", "Read more about the [data we collect]({0}).", 'https://aka.ms/vscode-telemetry') :
localize("telemetry.docsAndPrivacyStatement", "Read more about the [data we collect]({0}) and our [privacy statement]({1}).", 'https://aka.ms/vscode-telemetry', product.privacyStatementUrl);
const restartString = !isWeb ? localize('telemetry.restart', 'A full restart of the application is necessary for crash reporting changes to take effect.') : '';
agentHostTelemetryService.ts ×29
const crashReportsHeader = localize('telemetry.crashReports', "Crash Reports");
const errorsHeader = localize('telemetry.errors', "Error Telemetry");
const usageHeader = localize('telemetry.usage', "Usage Data");
const telemetryTableDescription = localize('telemetry.telemetryLevel.tableDescription', "The following table outlines the data sent with each setting:");
const telemetryTable = `
| | ${crashReportsHeader} | ${errorsHeader} | ${usageHeader} |
|:------|:-------------:|:---------------:|:----------:|
| all | ✓ | ✓ | ✓ |
| error | ✓ | ✓ | - |
| crash | ✓ | - | - |
| off | - | - | - |
`;
const deprecatedSettingNote = localize('telemetry.telemetryLevel.deprecated', "****Note:*** If this setting is 'off', no telemetry will be sent regardless of other telemetry settings. If this setting is set to anything except 'off' and telemetry is disabled with deprecated settings, no telemetry will be sent.*");
const telemetryDescription = `
${telemetryText} ${externalLinksStatement} ${restartString}
${telemetryTableDescription}
${telemetryTable}
${deprecatedSettingNote}
`;
return telemetryDescription;
}
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
configurationRegistry.registerConfiguration({
'id': TELEMETRY_SECTION_ID,
'order': 1,
'type': 'object',
'title': localize('telemetryConfigurationTitle', "Telemetry"),
'properties': {
[TELEMETRY_SETTING_ID]: {
'type': 'string',
'enum': [TelemetryConfiguration.ON, TelemetryConfiguration.ERROR, TelemetryConfiguration.CRASH, TelemetryConfiguration.OFF],
'enumDescriptions': [
localize('telemetry.telemetryLevel.default', "Sends usage data, errors, and crash reports."),
localize('telemetry.telemetryLevel.error', "Sends general error telemetry and crash reports."),
localize('telemetry.telemetryLevel.crash', "Sends OS level crash reports."),
localize('telemetry.telemetryLevel.off', "Disables all product telemetry.")
],
'markdownDescription': getTelemetryLevelSettingDescription(),
'default': TelemetryConfiguration.ON,
'restricted': true,
'scope': ConfigurationScope.APPLICATION,
'tags': ['usesOnlineServices', 'telemetry'],
'policy': {
name: 'TelemetryLevel',
category: PolicyCategory.Telemetry,
minimumVersion: '1.99',
localization: {
description: {
key: 'telemetry.telemetryLevel.policyDescription',
value: localize('telemetry.telemetryLevel.policyDescription', "Controls the level of telemetry."),
},
enumDescriptions: [
{
key: 'telemetry.telemetryLevel.default',
value: localize('telemetry.telemetryLevel.default', "Sends usage data, errors, and crash reports."),
},
{
key: 'telemetry.telemetryLevel.error',
value: localize('telemetry.telemetryLevel.error', "Sends general error telemetry and crash reports."),
},
{
key: 'telemetry.telemetryLevel.crash',
value: localize('telemetry.telemetryLevel.crash', "Sends OS level crash reports."),
},
{
key: 'telemetry.telemetryLevel.off',
value: localize('telemetry.telemetryLevel.off', "Disables all product telemetry."),
}
]
}
}
},
'telemetry.feedback.enabled': {
type: 'boolean',
default: true,
description: localize('telemetry.feedback.enabled', "Enable feedback mechanisms such as the issue reporter, surveys, and other feedback options."),
policy: {
name: 'EnableFeedback',
category: PolicyCategory.Telemetry,
minimumVersion: '1.99',
localization: { description: { key: 'telemetry.feedback.enabled', value: localize('telemetry.feedback.enabled', "Enable feedback mechanisms such as the issue reporter, surveys, and other feedback options.") } },
}
},
// Deprecated telemetry setting
[TELEMETRY_OLD_SETTING_ID]: {
'type': 'boolean',
'markdownDescription':
!product.privacyStatementUrl ?
localize('telemetry.enableTelemetry', "Enable diagnostic data to be collected. This helps us to better understand how {0} is performing and where improvements need to be made.", product.nameLong) :
localize('telemetry.enableTelemetryMd', "Enable diagnostic data to be collected. This helps us to better understand how {0} is performing and where improvements need to be made. [Read more]({1}) about what we collect and our privacy statement.", product.nameLong, product.privacyStatementUrl),
'restricted': true,
'markdownDeprecationMessage': localize('enableTelemetryDeprecated', "If this setting is false, no telemetry will be sent regardless of the new setting's value. Deprecated in favor of the {0} setting.", `\`#${TELEMETRY_SETTING_ID}#\``),
'scope': ConfigurationScope.APPLICATION,
'tags': ['usesOnlineServices', 'telemetry']
}
},
});