src/vs/platform/telemetry/node/1dsAppender.ts

117 LOC · 52 covered · 65 uncovered · 7 ranges · 1493 concepts · 2 introducers · 688 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.

1 > /*--------------------------------------------------------------------------------------------- agentHostMicrosoftTelemetry.ts ×7
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import type { IPayloadData, IXHROverride } from '@microsoft/1ds-post-js';
7 > import { streamToBuffer } from '../../../base/common/buffer.js';
8 > import { CancellationToken } from '../../../base/common/cancellation.js';
9 > import { IRequestOptions } from '../../../base/parts/request/common/request.js';
10 > import { IRequestService, NO_FETCH_TELEMETRY } from '../../request/common/request.js';
11 > import { AbstractOneDataSystemAppender, IAppInsightsCore } from '../common/1dsAppender.js';
12 >
13 > type OnCompleteFunc = (status: number, headers: { [headerName: string]: string }, response?: string) => void;
14 >
15 > interface IResponseData {
16 > headers: { [headerName: string]: string };
17 > statusCode: number;
18 > responseData: string;
19 > }
20 >
21 > /**
22 > * Completes a request to submit telemetry to the server utilizing the request service
23 > * @param options The options which will be used to make the request
24 > * @param requestService The request service
25 > * @returns An object containing the headers, statusCode, and responseData
26 > */
27 async function makeTelemetryRequest(options: IRequestOptions, requestService: IRequestService): Promise<IResponseData> {
28 const response = await requestService.request(options, CancellationToken.None);
29 const responseData = (await streamToBuffer(response.stream)).toString();
30 const statusCode = response.res.statusCode ?? 200;
31 const headers = response.res.headers as Record<string, string>;
32 return {
33 headers,
34 statusCode,
35 responseData
36 };
37 }
39 > /**
40 > * Complete a request to submit telemetry to the server utilizing the https module. Only used when the request service is not available
41 > * @param options The options which will be used to make the request
42 > * @returns An object containing the headers, statusCode, and responseData
43 > */
44 async function makeLegacyTelemetryRequest(options: IRequestOptions): Promise<IResponseData> {
45 const https = await import('https'); // Lazy due to https://github.com/nodejs/node/issues/59686
46 const httpsOptions = {
47 method: options.type,
48 headers: options.headers
49 };
50 const responsePromise = new Promise<IResponseData>((resolve, reject) => {
51 const req = https.request(options.url ?? '', httpsOptions, res => {
52 res.on('data', function (responseData) {
53 resolve({
54 headers: res.headers as Record<string, string>,
55 statusCode: res.statusCode ?? 200,
56 responseData: responseData.toString()
57 });
58 });
59 // On response with error send status of 0 and a blank response to oncomplete so we can retry events
60 res.on('error', function (err) {
61 reject(err);
62 });
63 });
64 req.write(options.data, (err) => {
65 if (err) {
66 reject(err);
67 }
68 });
69 req.end();
70 });
71 return responsePromise;
72 }
74 async function sendPostAsync(requestService: IRequestService | undefined, payload: IPayloadData, oncomplete: OnCompleteFunc) {
75 const telemetryRequestData = typeof payload.data === 'string' ? payload.data : new TextDecoder().decode(payload.data);
76 const requestOptions: IRequestOptions = {
77 type: 'POST',
78 headers: {
79 ...payload.headers,
80 'Content-Type': 'application/json',
81 'Content-Length': Buffer.byteLength(payload.data).toString()
82 },
83 url: payload.urlString,
84 data: telemetryRequestData,
85 callSite: NO_FETCH_TELEMETRY
86 };
87
88 try {
89 const responseData = requestService ? await makeTelemetryRequest(requestOptions, requestService) : await makeLegacyTelemetryRequest(requestOptions);
90 oncomplete(responseData.statusCode, responseData.headers, responseData.responseData);
91 } catch {
92 // If it errors out, send status of 0 and a blank response to oncomplete so we can retry events
93 oncomplete(0, {});
94 }
95 }
97 >
98 > export class OneDataSystemAppender extends AbstractOneDataSystemAppender {
99 >
100 > constructor(
101 > requestService: IRequestService | undefined, 1dsAppender.ts ×11
102 > isInternalTelemetry: boolean,
103 > eventPrefix: string,
104 > defaultData: { [key: string]: unknown } | null,
105 > iKeyOrClientFactory: string | (() => IAppInsightsCore), // allow factory function for testing
106 > ) {
107 > // Override the way events get sent since node doesn't have XHTMLRequest
108 > const customHttpXHROverride: IXHROverride = {
109 > sendPOST: (payload: IPayloadData, oncomplete: OnCompleteFunc) => {
110 // Fire off the async request without awaiting it
111 sendPostAsync(requestService, payload, oncomplete);
112 }
114 >
115 > super(isInternalTelemetry, eventPrefix, defaultData, iKeyOrClientFactory, customHttpXHROverride);
116 > }