-
Notifications
You must be signed in to change notification settings - Fork 39.7k
Expand file tree
/
Copy pathloggingActions.ts
More file actions
690 lines (644 loc) · 29.2 KB
/
loggingActions.ts
File metadata and controls
690 lines (644 loc) · 29.2 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dns from 'dns';
import * as http from 'http';
import * as https from 'https';
import * as os from 'os';
import * as path from 'path';
import * as tls from 'tls';
import * as util from 'util';
import * as vscode from 'vscode';
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { ICAPIClientService } from '../../../platform/endpoint/common/capiClient';
import { IEnvService, isScenarioAutomation } from '../../../platform/env/common/envService';
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
import { collectErrorMessages, collectSingleLineErrorMessage, ILogService, sanitizeNetworkErrorForTelemetry } from '../../../platform/log/common/logService';
import { outputChannel } from '../../../platform/log/vscode/outputChannelLogTarget';
import { FetchEvent, IFetcherService } from '../../../platform/networking/common/fetcherService';
import { IFetcher, userAgentLibraryHeader } from '../../../platform/networking/common/networking';
import { NodeFetcher } from '../../../platform/networking/node/nodeFetcher';
import { NodeFetchFetcher } from '../../../platform/networking/node/nodeFetchFetcher';
import { ElectronFetcher } from '../../../platform/networking/vscode-node/electronFetcher';
import { getShadowedConfig } from '../../../platform/networking/vscode-node/fetcherServiceImpl';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { shuffle } from '../../../util/vs/base/common/arrays';
import { timeout } from '../../../util/vs/base/common/async';
import { Disposable, MutableDisposable } from '../../../util/vs/base/common/lifecycle';
import { generateUuid } from '../../../util/vs/base/common/uuid';
import { IInstantiationService, ServicesAccessor } from '../../../util/vs/platform/instantiation/common/instantiation';
import { EXTENSION_ID } from '../../common/constants';
interface ProxyAgentLog {
trace(message: string, ...args: any[]): void;
debug(message: string, ...args: any[]): void;
info(message: string, ...args: any[]): void;
warn(message: string, ...args: any[]): void;
error(message: string | Error, ...args: any[]): void;
}
interface ProxyAgentParams {
log: ProxyAgentLog;
loadSystemCertificatesFromNode: () => boolean | undefined;
}
interface ProxyAgent {
loadSystemCertificates?(params: ProxyAgentParams): Promise<string[]>;
resolveProxyURL?(url: string): Promise<string | undefined>;
}
export class LoggingActionsContrib {
constructor(
@IVSCodeExtensionContext private readonly _context: IVSCodeExtensionContext,
@IEnvService private envService: IEnvService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IExperimentationService private readonly experimentationService: IExperimentationService,
@IAuthenticationService private readonly authService: IAuthenticationService,
@ICAPIClientService private readonly capiClientService: ICAPIClientService,
@IFetcherService private readonly fetcherService: IFetcherService,
@ILogService private logService: ILogService,
) {
const collectDiagnostics = async () => {
const document = await vscode.workspace.openTextDocument({ language: 'markdown' });
const editor = await vscode.window.showTextDocument(document);
const electronConfig = getShadowedConfig<boolean>(this.configurationService, this.experimentationService, ConfigKey.Shared.DebugUseElectronFetcher, ConfigKey.TeamInternal.DebugExpUseElectronFetcher);
const nodeConfig = getShadowedConfig<boolean>(this.configurationService, this.experimentationService, ConfigKey.Shared.DebugUseNodeFetcher, ConfigKey.TeamInternal.DebugExpUseNodeFetcher);
const nodeFetchConfig = getShadowedConfig<boolean>(this.configurationService, this.experimentationService, ConfigKey.Shared.DebugUseNodeFetchFetcher, ConfigKey.TeamInternal.DebugExpUseNodeFetchFetcher);
const ext = vscode.extensions.getExtension(EXTENSION_ID);
const product = require(path.join(vscode.env.appRoot, 'product.json'));
await appendText(editor, `## GitHub Copilot Chat
- Extension: ${this.envService.getVersion()} (${this.envService.getBuildType()})
- VS Code: ${vscode.version} (${product.commit || 'out-of-source'})
- OS: ${os.platform()} ${os.release()} ${os.arch()}${vscode.env.remoteName ? `
- Remote Name: ${vscode.env.remoteName}` : ''}${vscode.env.remoteName && ext ? `
- Extension Kind: ${vscode.ExtensionKind[ext.extensionKind]}` : ''}
- GitHub Account: ${this.authService.anyGitHubSession?.account.label || 'Signed Out'}
## Network
User Settings:
\`\`\`json${getNetworkSettings()}
"github.copilot.advanced.debug.useElectronFetcher": ${electronConfig},
"github.copilot.advanced.debug.useNodeFetcher": ${nodeConfig},
"github.copilot.advanced.debug.useNodeFetchFetcher": ${nodeFetchConfig}
\`\`\`${getProxyEnvVariables()}
`);
const proxyAgent = loadVSCodeModule<ProxyAgent>('@vscode/proxy-agent');
const loadSystemCertificatesFromNode = this.configurationService.getNonExtensionConfig<boolean>('http.systemCertificatesNode');
const osCertificates = proxyAgent?.loadSystemCertificates ? await loadSystemCertificates(proxyAgent.loadSystemCertificates, loadSystemCertificatesFromNode, this.logService) : undefined;
const urls = [
this.capiClientService.dotcomAPIURL,
this.capiClientService.capiPingURL,
this.capiClientService.proxyBaseURL + '/_ping',
];
const isGHEnterprise = this.capiClientService.dotcomAPIURL !== 'https://api.github.com';
const timeoutSeconds = 10;
const electronFetcher = ElectronFetcher.create(this.envService);
const electronCurrent = !!electronFetcher && electronConfig;
const nodeCurrent = !electronCurrent && nodeConfig;
const nodeFetchCurrent = !electronCurrent && !nodeCurrent && nodeFetchConfig;
const nodeCurrentFallback = !electronCurrent && !nodeFetchCurrent;
const activeFetcher = this.fetcherService.getUserAgentLibrary();
const nodeFetcher = new NodeFetcher(this.envService);
const fetchers = {
['Electron fetch']: {
fetcher: electronFetcher,
current: electronCurrent,
},
['Node.js https']: {
fetcher: nodeFetcher,
current: nodeCurrent || nodeCurrentFallback,
},
['Node.js fetch']: {
fetcher: new NodeFetchFetcher(this.envService),
current: nodeFetchCurrent,
},
};
const dnsLookup = util.promisify(dns.lookup);
for (const url of urls) {
const authHeaders = await this.getAuthHeaders(isGHEnterprise, url);
const host = new URL(url).hostname;
await appendText(editor, `\nConnecting to ${url}:\n`);
for (const family of [4, 6]) {
await appendText(editor, `- DNS ipv${family} Lookup: `);
const start = Date.now();
try {
const dnsResult = await Promise.race([dnsLookup(host, { family }), timeout(timeoutSeconds * 1000)]);
if (dnsResult) {
await appendText(editor, `${dnsResult.address} (${Date.now() - start} ms)\n`);
} else {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${err?.message}\n`);
}
}
let probeProxyURL: string | undefined;
if (proxyAgent?.resolveProxyURL) {
await appendText(editor, `- Proxy URL: `);
const start = Date.now();
try {
const proxyURL = await Promise.race([proxyAgent.resolveProxyURL(url), timeoutAfter(timeoutSeconds * 1000)]);
if (proxyURL === 'timeout') {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
} else {
await appendText(editor, `${proxyURL || 'None'} (${Date.now() - start} ms)\n`);
probeProxyURL = proxyURL;
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${err?.message}\n`);
}
}
if (proxyAgent?.loadSystemCertificates && probeProxyURL?.startsWith('https:')) {
const tlsOrig: typeof tls | undefined = (tls as any).__vscodeOriginal;
if (tlsOrig) {
await appendText(editor, `- Proxy TLS: `);
if (!osCertificates) {
await appendText(editor, `(failed to load system certificates) `);
}
const start = Date.now();
try {
const result = await Promise.race([tlsConnect(tlsOrig, probeProxyURL, [...tls.rootCertificates, ...(osCertificates || [])]), timeout(timeoutSeconds * 1000)]);
if (result) {
await appendText(editor, `${result} (${Date.now() - start} ms)\n`);
} else {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${err?.message}\n`);
}
}
}
if (probeProxyURL) {
const httpx: typeof https | typeof http | undefined = probeProxyURL.startsWith('https:') ? (https as any).__vscodeOriginal : (http as any).__vscodeOriginal;
if (httpx) {
await appendText(editor, `- Proxy Connection: `);
const start = Date.now();
try {
const result = await Promise.race([proxyConnect(httpx, probeProxyURL, url), timeout(timeoutSeconds * 1000)]);
if (result) {
const headers = Object.keys(result.headers).map(header => `\n ${header}: ${result.headers[header]}`);
const text = `${result.statusCode} ${result.statusMessage}${headers.join('')}`;
await appendText(editor, `${text} (${Date.now() - start} ms)\n`);
} else {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${err?.message}\n`);
}
}
}
for (const [name, fetcher] of Object.entries(fetchers)) {
await appendText(editor, `- ${name}${fetcher.current ? ' (configured)' : fetcher.fetcher?.getUserAgentLibrary() === activeFetcher ? ' (active)' : ''}: `);
if (fetcher.fetcher) {
const start = Date.now();
try {
const response = await Promise.race([fetcher.fetcher.fetch(url, { headers: authHeaders, callSite: 'diagnostics-fetcher-probe' }), timeout(timeoutSeconds * 1000)]);
if (response) {
await appendText(editor, `HTTP ${response.status} (${Date.now() - start} ms)\n`);
} else {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${collectErrorMessages(err)}\n`);
}
} else {
await appendText(editor, 'Unavailable\n');
}
}
}
const currentFetcher = Object.values(fetchers).find(fetcher => fetcher.current)?.fetcher || nodeFetcher;
const secondaryUrls = [
{ url: 'https://mobile.events.data.microsoft.com', fetcher: currentFetcher },
{ url: 'https://dc.services.visualstudio.com', fetcher: currentFetcher },
{ url: 'https://copilot-telemetry.githubusercontent.com/_ping', fetcher: nodeFetcher },
{ url: vscode.Uri.parse(this.capiClientService.copilotTelemetryURL).with({ path: '/_ping' }).toString(), fetcher: nodeFetcher },
{ url: 'https://default.exp-tas.com', fetcher: nodeFetcher },
];
await appendText(editor, `\n`);
for (const { url, fetcher } of secondaryUrls) {
const authHeaders = await this.getAuthHeaders(isGHEnterprise, url);
await appendText(editor, `Connecting to ${url}: `);
const start = Date.now();
try {
const response = await Promise.race([fetcher.fetch(url, { headers: authHeaders, callSite: 'diagnostics-secondary-probe' }), timeout(timeoutSeconds * 1000)]);
if (response) {
await appendText(editor, `HTTP ${response.status} (${Date.now() - start} ms)\n`);
} else {
await appendText(editor, `timed out after ${timeoutSeconds} seconds\n`);
}
} catch (err) {
await appendText(editor, `Error (${Date.now() - start} ms): ${collectErrorMessages(err)}\n`);
}
}
await appendText(editor, `\nNumber of system certificates: ${osCertificates?.length ?? 'failed to load'}\n`);
await appendText(editor, `
## Documentation
In corporate networks: [Troubleshooting firewall settings for GitHub Copilot](https://docs.github.com/en/copilot/troubleshooting-github-copilot/troubleshooting-firewall-settings-for-github-copilot).`);
return document.getText();
};
this._context.subscriptions.push(vscode.commands.registerCommand('github.copilot.debug.collectDiagnostics', collectDiagnostics));
// Internal command is not declared in package.json so it can be used from the welcome views while the extension is being activated.
this._context.subscriptions.push(vscode.commands.registerCommand('github.copilot.debug.collectDiagnostics.internal', collectDiagnostics));
this._context.subscriptions.push(vscode.commands.registerCommand('github.copilot.debug.showOutputChannel.internal', () => outputChannel.show()));
this._context.subscriptions.push(new NetworkStatus(this.fetcherService, this.configurationService, this.experimentationService));
}
private async getAuthHeaders(isGHEnterprise: boolean, url: string) {
const authHeaders: Record<string, string> = {};
if (isGHEnterprise) {
let token = '';
if (url === this.capiClientService.dotcomAPIURL) {
token = this.authService.anyGitHubSession?.accessToken || '';
} else {
try {
token = (await this.authService.getCopilotToken()).token;
} catch (_err) {
// Ignore error
token = '';
}
}
authHeaders['Authorization'] = `Bearer ${token}`;
}
return authHeaders;
}
}
async function appendText(editor: vscode.TextEditor, string: string) {
await editor.edit(builder => {
builder.insert(editor.document.lineAt(editor.document.lineCount - 1).range.end, string);
});
}
function timeoutAfter(ms: number) {
return new Promise<'timeout'>(resolve => setTimeout(() => resolve('timeout'), ms));
}
function loadVSCodeModule<T>(moduleName: string): T | undefined {
const appRoot = vscode.env.appRoot;
try {
return require(`${appRoot}/node_modules.asar/${moduleName}`);
} catch (err) {
// Not in ASAR.
}
try {
return require(`${appRoot}/node_modules/${moduleName}`);
} catch (err) {
// Not available.
}
return undefined;
}
async function loadSystemCertificates(load: NonNullable<ProxyAgent['loadSystemCertificates']>, loadSystemCertificatesFromNode: boolean | undefined, logService: ILogService): Promise<(string | Buffer)[] | undefined> {
try {
const certificates = await load({
log: {
trace(message: string, ..._args: any[]) {
logService.trace(message);
},
debug(message: string, ..._args: any[]) {
logService.debug(message);
},
info(message: string, ..._args: any[]) {
logService.info(message);
},
warn(message: string, ..._args: any[]) {
logService.warn(message);
},
error(message: string | Error, ..._args: any[]) {
logService.error(typeof message === 'string' ? message : String(message));
},
} satisfies ProxyAgentLog,
loadSystemCertificatesFromNode: () => loadSystemCertificatesFromNode,
});
return Array.isArray(certificates) ? certificates : undefined;
} catch (err) {
logService.error(err);
return undefined;
}
}
async function tlsConnect(tlsOrig: typeof tls, proxyURL: string, ca: (string | Buffer)[]) {
return new Promise<string>((resolve, reject) => {
const proxyUrlObj = new URL(proxyURL);
const socket = tlsOrig.connect({
host: proxyUrlObj.hostname,
port: parseInt(proxyUrlObj.port, 10),
servername: proxyUrlObj.hostname,
ca,
}, () => {
socket.end();
resolve('Succeeded');
});
socket.on('error', reject);
});
}
async function proxyConnect(httpx: typeof https | typeof http, proxyUrl: string, targetUrl: string, sanitize = false) {
return new Promise<{ statusCode: number | undefined; statusMessage: string | undefined; headers: Record<string, string | string[]> }>((resolve, reject) => {
const proxyUrlObj = new URL(proxyUrl);
const targetUrlObj = new URL(targetUrl);
const targetHost = `${targetUrlObj.hostname}:${targetUrlObj.port || (targetUrlObj.protocol === 'https:' ? 443 : 80)}`;
const options = {
method: 'CONNECT',
host: proxyUrlObj.hostname,
port: proxyUrlObj.port,
path: targetHost,
headers: {
Host: targetHost,
},
rejectUnauthorized: false,
};
const req = httpx.request(options);
req.on('connect', (res, socket, head) => {
const headers = ['proxy-authenticate', 'proxy-agent', 'server', 'via'].reduce((acc, header) => {
const value = res.headers[header];
if (value) {
const doSanitize = sanitize && !['proxy-agent', 'server'].includes(header);
acc[header] = doSanitize ? Array.isArray(value) ? value.map(sanitizeValue) : sanitizeValue(value) : value;
}
return acc;
}, {} as Record<string, string | string[]>);
socket.end();
resolve({ statusCode: res.statusCode, statusMessage: res.statusMessage, headers });
});
req.on('error', reject);
req.end();
});
}
const networkSettingsIds = [
'http.proxy',
'http.noProxy',
'http.proxyAuthorization',
'http.proxyStrictSSL',
'http.proxySupport',
'http.electronFetch',
'http.fetchAdditionalSupport',
'http.proxyKerberosServicePrincipal',
'http.systemCertificates',
'http.systemCertificatesNode',
'http.experimental.systemCertificatesV2',
'http.useLocalProxyConfiguration',
];
const alwaysShowSettingsIds = [
'http.systemCertificatesNode',
];
function getNetworkSettings() {
const configuration = vscode.workspace.getConfiguration();
return networkSettingsIds.map(key => {
const i = configuration.inspect(key);
const v = configuration.get(key, i?.defaultValue);
if (alwaysShowSettingsIds.includes(key) || v !== i?.defaultValue && !(Array.isArray(v) && Array.isArray(i?.defaultValue) && v.length === 0 && i?.defaultValue.length === 0)) {
return `\n "${key}": ${JSON.stringify(v)},`;
}
return '';
}).join('');
}
function getProxyEnvVariables() {
const res = [];
const envVars = ['http_proxy', 'https_proxy', 'ftp_proxy', 'all_proxy', 'no_proxy'];
for (const env in process.env) {
if (envVars.includes(env.toLowerCase())) {
res.push(`\n- ${env}=${process.env[env]}`);
}
}
return res.length ? `\n\nEnvironment Variables:${res.join('')}` : '';
}
export class FetcherTelemetryContribution {
constructor(
@IInstantiationService instantiationService: IInstantiationService,
) {
instantiationService.invokeFunction(collectFetcherTelemetry);
}
}
function collectFetcherTelemetry(accessor: ServicesAccessor): void {
const extensionContext = accessor.get(IVSCodeExtensionContext);
const envService = accessor.get(IEnvService);
const logService = accessor.get(ILogService);
const configurationService = accessor.get(IConfigurationService);
const expService = accessor.get(IExperimentationService);
if (!vscode.env.isTelemetryEnabled || extensionContext.extensionMode !== vscode.ExtensionMode.Production || isScenarioAutomation) {
return;
}
if (!configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.DebugCollectFetcherTelemetry, expService)) {
return;
}
const now = Date.now();
const previous = extensionContext.globalState.get<number>('lastCollectFetcherTelemetryTime', 0);
if (now - previous < 5 * 60 * 1000) {
logService.debug(`Send fetcher telemetry: Skipped.`);
return;
}
(async () => {
await extensionContext.globalState.update('lastCollectFetcherTelemetryTime', now);
logService.debug(`Send fetcher telemetry: Exclude other windows.`);
const windowUUID = generateUuid();
await extensionContext.globalState.update('lastCollectFetcherTelemetryUUID', windowUUID);
await timeout(5000);
if (extensionContext.globalState.get<string>('lastCollectFetcherTelemetryUUID') !== windowUUID) {
logService.debug(`Send fetcher telemetry: Other window won.`);
return;
}
logService.debug(`Send fetcher telemetry: This window won.`);
const fetchers = [
ElectronFetcher.create(envService),
new NodeFetchFetcher(envService),
new NodeFetcher(envService),
].filter(fetcher => fetcher) as IFetcher[];
// Randomize to offset any order dependency in telemetry.
shuffle(fetchers);
// First loop: probe each fetcher with an empty body to collect connectivity results.
const probeResults: Record<string, string> = {};
for (const fetcher of fetchers) {
const library = fetcher.getUserAgentLibrary();
const key = library.replace(/-/g, '');
const requestStartTime = Date.now();
try {
const response = await sendRawTelemetry(fetcher, envService, extensionContext, 'GitHub.copilot-chat/fetcherTelemetryProbe', {});
probeResults[key] = `Status: ${response.status}`;
logService.debug(`Fetcher telemetry probe: ${library} ${probeResults[key]} (${Date.now() - requestStartTime}ms)`);
} catch (e) {
probeResults[key] = `Error: ${sanitizeNetworkErrorForTelemetry(collectSingleLineErrorMessage(e, true))}`;
logService.debug(`Fetcher telemetry probe: ${library} ${probeResults[key]} (${Date.now() - requestStartTime}ms)`);
}
}
// Second loop: send the actual telemetry event including probe results.
const requestGroupId = generateUuid();
const extensionKind = extensionContext.extension.extensionKind === vscode.ExtensionKind.UI ? 'local' : 'remote';
for (const fetcher of fetchers) {
const requestStartTime = Date.now();
try {
/* __GDPR__
"fetcherTelemetry" : {
"owner": "chrmarti",
"comment": "Telemetry event to test connectivity of different fetcher implementations.",
"requestGroupId": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "Id to group requests from the same run." },
"clientLibrary": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "The fetcher library used for this request." },
"extensionKind": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "Whether the extension runs locally or remotely." },
"remoteName": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "The remote name, if any." },
"electronfetch": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "Probe result for the electron-fetch fetcher." },
"nodefetch": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "Probe result for the node-fetch fetcher." },
"nodehttp": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "comment": "Probe result for the node-http fetcher." }
}
*/
const properties: Record<string, string> = {
requestGroupId,
clientLibrary: fetcher.getUserAgentLibrary(),
extensionKind,
remoteName: vscode.env.remoteName ?? 'none',
...probeResults,
};
const response = await sendRawTelemetry(fetcher, envService, extensionContext, 'GitHub.copilot-chat/fetcherTelemetry', properties);
logService.debug(`Fetcher telemetry: Succeeded in ${Date.now() - requestStartTime}ms using ${fetcher.getUserAgentLibrary()} with status ${response.status} (${response.statusText}).`);
} catch (e) {
logService.debug(`Fetcher telemetry: Failed in ${Date.now() - requestStartTime}ms using ${fetcher.getUserAgentLibrary()}.`);
}
}
})().catch(err => {
logService.error(err);
});
}
async function sendRawTelemetry(fetcher: IFetcher, envService: IEnvService, extensionContext: IVSCodeExtensionContext, eventName: string, properties: Record<string, string>) {
const url = 'https://mobile.events.data.microsoft.com/OneCollector/1.0?cors=true&content-type=application/x-json-stream';
const product = require(path.join(vscode.env.appRoot, 'product.json'));
const vscodeCommitHash: string = product.commit || '';
const ariaKey = (extensionContext.extension.packageJSON as { ariaKey?: string }).ariaKey ?? '';
const iKey = `o:${ariaKey.split('-')[0]}`;
const sdkVer = '1DS-Web-JS-4.3.10';
const eventTime = new Date(Date.now() - 10).toISOString();
const event = {
name: eventName,
time: eventTime,
ver: '4.0',
iKey,
ext: {
sdk: { ver: sdkVer },
web: { consentDetails: '{"GPC_DataSharingOptIn":false}' },
},
data: {
baseData: {
name: eventName,
properties: {
...properties,
'abexp.assignmentcontext': '',
'common.os': os.platform(),
'common.nodeArch': os.arch(),
'common.platformversion': os.release(),
'common.telemetryclientversion': '1.5.0',
'common.extname': EXTENSION_ID,
'common.extversion': envService.getVersion(),
'common.vscodemachineid': envService.machineId,
'common.vscodesessionid': envService.sessionId,
'common.vscodecommithash': vscodeCommitHash,
'common.sqmid': '',
'common.devDeviceId': envService.devDeviceId,
'common.vscodeversion': envService.vscodeVersion,
'common.vscodereleasedate': product.date || 'unknown',
'common.isnewappinstall': vscode.env.isNewAppInstall,
'common.product': envService.uiKind,
'common.uikind': envService.uiKind,
'common.remotename': envService.remoteName ?? 'none',
'version': 'PostChannel=4.3.10',
},
},
},
};
const body = JSON.stringify(event);
const headers: Record<string, string> = {
'Client-Id': 'NO_AUTH',
'client-version': sdkVer,
'apikey': ariaKey,
'upload-time': String(Date.now()),
'time-delta-to-apply-millis': 'use-collector-delta',
'cache-control': 'no-cache, no-store',
'content-type': 'application/x-json-stream',
'User-Agent': `GitHubCopilotChat/${envService.getVersion()}`,
[userAgentLibraryHeader]: fetcher.getUserAgentLibrary(),
};
if (fetcher.getUserAgentLibrary() === NodeFetcher.ID) {
headers['content-length'] = String(Buffer.byteLength(body));
}
const response = await fetcher.fetch(url, {
method: 'POST',
headers,
body,
callSite: 'diagnostics-telemetry-probe',
});
await response.text();
return response;
}
const ids_paths = /(^|\b)[\p{L}\p{Nd}]+((=""?[^"]+""?)|(([.:=/"_-]+[\p{L}\p{Nd}]+)+))(\b|$)/giu;
export function sanitizeValue(input: string | undefined): string {
return (input || '').replace(ids_paths, (m) => maskByClass(m));
}
function maskByClass(s: string): string {
if (/^net::[A-Z_]+$/.test(s) || ['dev-container', 'attached-container', 'k8s-container', 'ssh-remote'].includes(s)) {
return s;
}
return s.replace(/\p{Lu}|\p{Ll}|\p{Nd}/gu, (ch) => {
if (/\p{Lu}/u.test(ch)) {
return 'A';
}
if (/\p{Ll}/u.test(ch)) {
return 'a';
}
return '0';
});
}
class NetworkStatus extends Disposable {
private readonly _statusBarItem: vscode.StatusBarItem;
private readonly _events: FetchEvent[] = [];
private readonly _fetchSubscription = this._register(new MutableDisposable());
constructor(private readonly _fetcherService: IFetcherService, private readonly _configurationService: IConfigurationService, private readonly _experimentationService: IExperimentationService) {
super();
this._statusBarItem = this._register(vscode.window.createStatusBarItem('copilot.networkStatus', vscode.StatusBarAlignment.Right, -1000));
this._statusBarItem.name = 'Copilot Network Status';
this._register(_configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(ConfigKey.TeamInternal.DebugShowNetworkStatus.fullyQualifiedId)) {
this._update();
}
}));
this._update();
}
private _isEnabled(): boolean {
return this._configurationService.getExperimentBasedConfig(ConfigKey.TeamInternal.DebugShowNetworkStatus, this._experimentationService);
}
private _onEvent(event: FetchEvent): void {
this._events.push(event);
const cutoff = Date.now() - 5 * 60 * 1000;
while (this._events.length > 0 && this._events[0].timestamp < cutoff) {
this._events.shift();
}
this._update();
}
private _update(): void {
const enabled = this._isEnabled();
if (enabled && !this._fetchSubscription.value) {
this._fetchSubscription.value = this._fetcherService.onDidFetch(e => this._onEvent(e));
} else if (!enabled) {
this._fetchSubscription.value = undefined;
this._events.length = 0;
this._statusBarItem.hide();
return;
}
const latestById = new Map<string, FetchEvent>();
for (const e of this._events) {
latestById.set(e.internalId, e);
}
const latest = [...latestById.values()];
const errors = latest.filter(e => e.outcome === 'error');
this._statusBarItem.text = `Copilot Network: ${errors.length} errors / ${latest.length} total`;
const byHostname = new Map<string, { total: number; errors: number; cancellations: number }>();
for (const e of latest) {
let entry = byHostname.get(e.hostname);
if (!entry) {
entry = { total: 0, errors: 0, cancellations: 0 };
byHostname.set(e.hostname, entry);
}
entry.total++;
if (e.outcome === 'error') {
entry.errors++;
} else if (e.outcome === 'cancel') {
entry.cancellations++;
}
}
const tooltip = new vscode.MarkdownString();
tooltip.appendMarkdown(`| Hostname | Errors | Cancellations | Total |\n`);
tooltip.appendMarkdown(`|:--|--:|--:|--:|\n`);
for (const [hostname, { total, errors, cancellations }] of [...byHostname].sort((a, b) => b[1].total - a[1].total)) {
tooltip.appendMarkdown(`| ${hostname} | ${errors} | ${cancellations} | ${total} |\n`);
}
tooltip.appendMarkdown(`\n**${errors.length}** of **${latest.length}** network requests failed in the last 5 minutes`);
this._statusBarItem.tooltip = tooltip;
this._statusBarItem.show();
}
}