-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtelemetry.ts
360 lines (310 loc) · 12 KB
/
telemetry.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use-strict';
import os from 'os';
import fs from 'fs';
import path from 'path';
import { randomUUID } from 'crypto';
import vscode from 'vscode';
import { ExtHttp, uuidAxiosRequestConfig } from 'f5-conx-core';
import { ExtensionContext } from 'vscode';
import { ext } from './extensionVariables';
export type EventType = {
command?: string;
unhandledRejection?: any;
[key: string]: any;
};
export class Telemetry {
https: ExtHttp;
ctx: ExtensionContext;
apiKey = Buffer.from([
'bW1oSlUyc0Nk',
'NjNCem5YQVh',
'EaDRreExJ',
'eWZJTW0zQXI='
].join(''), 'base64').toString();
/**
* standard vscode f5 document type param
*/
documentType = "F5 VSCode Telemetry Data";
/**
* f5 vscode teem standard doc version (initial)
*/
documentVersion = "1";
/**
* todo: setup a journal to buffer logs
*/
journal: EventType[] = [];
// endPoint = "https://us-central1-vscode-f5.cloudfunctions.net/telemetryV1";
endPoint = "https://product.apis.f5.com/ee/v1/telemetry";
/**
* teem agent ex vscode-f5/3.7.1
*/
teemAgent: string;
/**
* Microsoft Extension marketplace id, ex: F5DevCentral.vscode-f5
*/
extensionId: string;
/**
* extension version, ex: 3.7.1
*/
extensionVersion: string;
/**
* vscode code version, ex: "1.66.2"
*/
vscodeVersion: string;
osType: string;
osPlatform: string;
osRelease: string;
nodeArch: string;
product: string;
remoteName: string;
/**
* ui kind, desktop application or web browser
*/
uiKind: string;
/**
* unique identifier for the vscode instance/computer
*/
vscodeMachineId: string;
/**
* unique id for the current session. changes with every new window/editor
*/
vscodeSessionID: string;
/**
* Indicates that this is a fresh install of the application. true if within the first day of installation otherwise false.
* https://code.visualstudio.com/api/references/vscode-api#env
*/
isNewAppInstall: string;
/**
* is vscode telemetry enabled/disabled
*
* --**this is for vscode itself not any of the extensions**--
*/
isTelemetryEnabled: string;
/**
* vscodeMachineId in uuid format (truncated)
*/
instanceGUID: string;
constructor(ctx: ExtensionContext) {
this.ctx = ctx;
// uncomment to enable telemetry console logging for debugging
process.env.F5_TELEMETRY_DEBUG = 'yes';
// create external https service just for telemetry
this.https = this.createExtHttps();
this.teemAgent = ext.teemAgent;
this.vscodeVersion = vscode.version;
this.extensionId = ctx.extension.id;
this.extensionVersion = ctx.extension.packageJSON.version;
this.osType = os.type();
this.osPlatform = os.platform();
this.osRelease = (os.release() || "").replace(/^(\d+)(\.\d+)?(\.\d+)?(.*)/, "$1$2$3");
this.nodeArch = os.arch();
this.vscodeMachineId = vscode.env.machineId;
this.vscodeSessionID = vscode.env.sessionId;
this.isNewAppInstall = vscode.env.isNewAppInstall ? vscode.env.isNewAppInstall.toString() : "false";
this.isTelemetryEnabled = vscode.env.isTelemetryEnabled ? vscode.env.isTelemetryEnabled.toString() : "false";
this.product = vscode.env.appHost;
this.remoteName = this.cleanRemoteName(vscode.env.remoteName);
// mutate vscodeMachineId into digitalAssetId
this.instanceGUID = [
this.vscodeMachineId.substring(0, 8),
this.vscodeMachineId.substring(8, 12),
this.vscodeMachineId.substring(12, 16),
this.vscodeMachineId.substring(16, 20),
this.vscodeMachineId.substring(20, 32)
].join('-');
switch (vscode.env.uiKind) {
case vscode.UIKind.Web:
this.uiKind = "web";
break;
case vscode.UIKind.Desktop:
this.uiKind = "desktop";
break;
default:
this.uiKind = "unknown";
}
}
// /**
// * loads api key from file or secret
// */
// async init() {
// const keyFileName = 'F5_TEEM';
// const keyFileNamePath = path.join(this.ctx.extensionPath, keyFileName);
// // console.log(`Looking for ${keyFileName} file at`, keyFileNamePath);
// await fs.promises.readFile(keyFileNamePath)
// .then(key => {
// this.ctx.secrets.store(keyFileName, key.toString());
// // console.log(`${keyFileName} FILE FOUND AND KEY STORED AS SECRET:`, key.toString());
// })
// .then(() => {
// // console.log(`Deleting ${keyFileName} FILE`);
// fs.unlinkSync(keyFileNamePath);
// })
// .catch( async e => {
// // console.log(`${keyFileName} FILE NOT FOUND`, e.message);
// const str = [
// 'bW1oSlUyc0Nk',
// 'NjNCem5YQVh',
// 'EaDRreExJ',
// 'eWZJTW0zQXI='
// ].join('');
// await this.ctx.secrets.store(keyFileName, Buffer.from(str, 'base64').toString());
// });
// // set the api key
// this.apiKey = await this.ctx.secrets.get(keyFileName);
// // console.log(`---${this.apiKey}---`);
// return;
// }
private createExtHttps() {
// create external https service just for telemetry
const eHttps = new ExtHttp();
// listen and log the requests/responses
eHttps
.events
.on('log-http-request', config => {
if (process.env.F5_TELEMETRY_DEBUG) {
const headers = Object.entries(config.headers).filter(([key, val]) => typeof val === 'string' );
console.log(`f5-telemetry-request`, {
method: config.method,
url: config.url,
uuid: config.uuid,
headers,
body: config.data
});
}
})
.on('log-http-response', resp => {
if (process.env.F5_TELEMETRY_DEBUG) {
console.log(`f5-telemetry-response`, {
status: resp.status,
statusText: resp.statusText,
uuid: resp.config.uuid,
headers: resp.headers,
request: {
baseURL: resp.config.baseURL,
url: resp.config.url,
method: resp.request.method,
headers: resp.config.headers,
timings: resp.request.timings
},
data: resp.data
});
}
});
return eHttps;
}
telemetryBase() {
return {
documentType: this.documentType,
documentVersion: this.documentVersion,
digitalAssetId: this.instanceGUID,
digitalAssetName: this.extensionId,
digitalAssetVersion: this.extensionVersion,
vscodeVersion: this.vscodeVersion,
extensionId: this.extensionId,
extensionVersion: this.extensionVersion,
osType: this.osType,
osPlatform: this.osPlatform,
osRelease: this.osRelease,
nodeArch: this.nodeArch,
vscodeMachineId: this.vscodeMachineId,
vscodeSessionID: this.vscodeSessionID,
isNewAppInstall: this.isNewAppInstall,
product: this.product,
remoteName: this.remoteName,
};
}
/**
*
* Capture vscode telemetry event
*
* @param event
* @returns
*/
async capture(event: EventType): Promise<void> {
if (!this.apiKey) {
return;
}
// append event to journal with unique id and timestamp
this.journal.push(Object.assign(event, {
id: randomUUID(),
eventTime: Date.now()
}));
// kick off send process with every new event
this.send();
// return to continue processing
return;
}
/**
*
* attempt to send captured telemetry events in journal
*
* @returns
*/
private async send(): Promise<void> {
const teemEnvLabel = ext.teemEnv;
const teemEnv = process.env[teemEnvLabel];
if (teemEnv === 'true' && this.apiKey) {
// combine base telemetry details with
const reqOpts = {
method: 'POST',
url: this.endPoint,
headers: {
"F5-ApiKey": this.apiKey,
"F5-DigitalAssetId": this.instanceGUID,
"F5-TraceId": randomUUID(),
},
data: Object.assign(
this.telemetryBase(),
{
observationStartTime: new Date().toISOString(),
observationEndTime: new Date().toISOString(),
epochTime: Date.now(),
telemtryId: randomUUID(),
telemetryRecords: this.journal
}
)
};
// console.log(JSON.stringify(reqOpts));
return this.https.makeRequest(reqOpts as uuidAxiosRequestConfig)
.then(resp => {
console.debug(`telemtry resp: ${resp.statusText}-${resp.status}`);
// clear journal on successful send
this.journal.length = 0;
})
.catch(err => {
console.debug(`telemtry error`, err);
});
}
}
// https://github.com/microsoft/vscode-extension-telemetry/blob/4408adad49f6da5816c28467d90aec15773773a9/src/common/baseTelemetryReporter.ts#L139
// __GDPR__COMMON__ "common.os" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.nodeArch" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.platformversion" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.extname" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.extversion" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.vscodemachineid" : { "endPoint": "MacAddressHash", "classification": "EndUserPseudonymizedInformation", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.vscodesessionid" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.vscodeversion" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.uikind" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.remotename" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.isnewappinstall" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
// __GDPR__COMMON__ "common.product" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
/**
* Given a remoteName ensures it is in the list of valid ones
* @param remoteName The remotename
* @returns The "cleaned" one
*/
private cleanRemoteName(remoteName?: string): string {
if (!remoteName) {
return "none";
}
let ret = "other";
// Allowed remote authorities
["ssh-remote", "dev-container", "attached-container", "wsl", "codespaces"].forEach((res: string) => {
if (remoteName!.indexOf(`${res}`) === 0) {
ret = res;
}
});
return ret;
}
}