-
Notifications
You must be signed in to change notification settings - Fork 30.6k
/
Copy pathtelemetryService.ts
248 lines (205 loc) · 11.1 KB
/
telemetryService.ts
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
107
108
109
110
111
112
113
114
115
116
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*---------------------------------------------------------------------------------------------
* 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 { 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 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 } from './telemetryUtils.js';
export interface ITelemetryServiceConfig {
appenders: ITelemetryAppender[];
sendErrorTelemetry?: boolean;
commonProperties?: ICommonProperties;
piiPaths?: string[];
}
export class TelemetryService implements ITelemetryService {
static readonly IDLE_START_EVENT_NAME = 'UserIdleStart';
static readonly IDLE_STOP_EVENT_NAME = 'UserIdleStop';
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 } = {};
private _piiPaths: string[];
private _telemetryLevel: TelemetryLevel;
private _sendErrorTelemetry: boolean;
private readonly _disposables = new DisposableStore();
private _cleanupPatterns: RegExp[] = [];
constructor(
config: ITelemetryServiceConfig,
@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;
// 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();
}
}));
}
setExperimentProperty(name: string, value: string): void {
this._experimentProperties[name] = value;
}
private _updateTelemetryLevel(): void {
let level = getTelemetryLevel(this._configurationService);
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._disposables.dispose();
}
private _log(eventName: string, eventLevel: TelemetryLevel, data?: ITelemetryData) {
// don't send events when the user is optout
if (this._telemetryLevel < eventLevel) {
return;
}
// add experiment properties
data = mixin(data, this._experimentProperties);
// remove all PII from data
data = cleanData(data as Record<string, any>, this._cleanupPatterns);
// add common properties
data = mixin(data, this._commonProperties);
// 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.') : '';
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;
}
Registry.as<IConfigurationRegistry>(Extensions.Configuration).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']
}
}
});
// Deprecated telemetry setting
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
'id': TELEMETRY_SECTION_ID,
'order': 110,
'type': 'object',
'title': localize('telemetryConfigurationTitle', "Telemetry"),
'properties': {
[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),
'default': true,
'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']
}
}
});