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

160 LOC · 121 covered · 39 uncovered · 17 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 { IExtendedConfiguration, IExtendedTelemetryItem, ITelemetryItem, ITelemetryUnloadState } from '@microsoft/1ds-core-js';
7 > import type { IChannelConfiguration, IXHROverride, PostChannel } from '@microsoft/1ds-post-js';
8 > import { importAMDNodeModule } from '../../../amdX.js';
9 > import { onUnexpectedError } from '../../../base/common/errors.js';
10 > import { mixin } from '../../../base/common/objects.js';
11 > import { isWeb } from '../../../base/common/platform.js';
12 > import { ITelemetryAppender, validateTelemetryData } from './telemetryUtils.js';
13 >
14 > // Interface type which is a subset of @microsoft/1ds-core-js AppInsightsCore.
15 > // Allows us to more easily build mock objects for testing as the interface is quite large and we only need a few properties.
16 > export interface IAppInsightsCore {
17 > pluginVersionString: string;
18 > track(item: ITelemetryItem | IExtendedTelemetryItem): void;
19 > unload(isAsync: boolean, unloadComplete: (unloadState: ITelemetryUnloadState) => void): void;
20 > }
21 >
22 > const endpointUrl = 'https://mobile.events.data.microsoft.com/OneCollector/1.0';
23 > const endpointHealthUrl = 'https://mobile.events.data.microsoft.com/ping';
24 >
25 > async function getClient(instrumentationKey: string, addInternalFlag?: boolean, xhrOverride?: IXHROverride): Promise<IAppInsightsCore> { 1dsAppender.ts ×11
26 > // eslint-disable-next-line local/code-amd-node-module
27 > const oneDs = isWeb ? await importAMDNodeModule<typeof import('@microsoft/1ds-core-js')>('@microsoft/1ds-core-js', 'bundle/ms.core.min.js') : await import('@microsoft/1ds-core-js');
28 > // eslint-disable-next-line local/code-amd-node-module
29 > const postPlugin = isWeb ? await importAMDNodeModule<typeof import('@microsoft/1ds-post-js')>('@microsoft/1ds-post-js', 'bundle/ms.post.min.js') : await import('@microsoft/1ds-post-js');
30 >
31 > const appInsightsCore = new oneDs.AppInsightsCore();
32 > const collectorChannelPlugin: PostChannel = new postPlugin.PostChannel();
33 > // Configure the app insights core to send to collector++ and disable logging of debug info
34 > const coreConfig: IExtendedConfiguration = {
35 > instrumentationKey,
36 > endpointUrl,
37 > loggingLevelTelemetry: 0,
38 > loggingLevelConsole: 0,
39 > disableCookiesUsage: true,
40 > disableDbgExt: true,
41 > disableInstrumentationKeyValidation: true,
42 > channels: [[
43 > collectorChannelPlugin
44 > ]]
45 > };
46 >
47 > if (xhrOverride) {
48 > coreConfig.extensionConfig = {};
49 > // Configure the channel to use a XHR Request override since it's not available in node
50 > const channelConfig: IChannelConfiguration = {
51 > alwaysUseXhrOverride: true,
52 > ignoreMc1Ms0CookieProcessing: true,
53 > httpXHROverride: xhrOverride
54 > };
55 > coreConfig.extensionConfig[collectorChannelPlugin.identifier] = channelConfig;
56 > }
57 >
58 > appInsightsCore.initialize(coreConfig, []);
59 >
60 > appInsightsCore.addTelemetryInitializer((envelope) => {
61 // Opt the user out of 1DS data sharing
62 envelope['ext'] = envelope['ext'] ?? {};
63 envelope['ext']['web'] = envelope['ext']['web'] ?? {};
64 envelope['ext']['web']['consentDetails'] = '{"GPC_DataSharingOptIn":false}';
65
66 if (addInternalFlag) {
67 envelope['ext']['utc'] = envelope['ext']['utc'] ?? {};
68 // Sets it to be internal only based on Windows UTC flagging
69 envelope['ext']['utc']['flags'] = 0x0000811ECD;
70 }
72 >
73 > return appInsightsCore;
74 > }
76 > // TODO @lramos15 maybe make more in line with src/vs/platform/telemetry/browser/appInsightsAppender.ts with caching support
77 > export abstract class AbstractOneDataSystemAppender implements ITelemetryAppender {
78 >
79 > protected _aiCoreOrKey: IAppInsightsCore | string | undefined;
80 > private _asyncAiCore: Promise<IAppInsightsCore> | null;
81 > protected readonly endPointUrl = endpointUrl;
82 > protected readonly endPointHealthUrl = endpointHealthUrl;
83 >
84 > constructor(
85 > private readonly _isInternalTelemetry: boolean, 1dsAppender.ts ×11
86 > private _eventPrefix: string,
87 > private _defaultData: { [key: string]: unknown } | null,
88 > iKeyOrClientFactory: string | (() => IAppInsightsCore), // allow factory function for testing
89 > private _xhrOverride?: IXHROverride
90 > ) {
91 > if (!this._defaultData) {
92 > this._defaultData = {};
93 > }
94 >
95 > if (typeof iKeyOrClientFactory === 'function') {
96 this._aiCoreOrKey = iKeyOrClientFactory();
97 > } else { 1dsAppender.ts ×11
98 > this._aiCoreOrKey = iKeyOrClientFactory;
99 > }
100 > this._asyncAiCore = null;
101 > }
103 > private _withAIClient(callback: (aiCore: IAppInsightsCore) => void): void {
104 > if (!this._aiCoreOrKey) { 1dsAppender.ts ×11
105 return;
106 }
108 > if (typeof this._aiCoreOrKey !== 'string') {
109 callback(this._aiCoreOrKey);
110 return;
111 }
113 > if (!this._asyncAiCore) {
114 > this._asyncAiCore = getClient(this._aiCoreOrKey, this._isInternalTelemetry, this._xhrOverride);
115 > }
116 >
117 > this._asyncAiCore.then(
118 > (aiClient) => {
119 > callback(aiClient);
120 > },
121 > (err) => {
122 onUnexpectedError(err);
123 console.error(err);
124 }
126 > }
128 > log(eventName: string, data?: unknown): void {
129 if (!this._aiCoreOrKey) {
130 return;
131 }
132 data = mixin(data, this._defaultData);
133 const validatedData = validateTelemetryData(data);
134 const name = this._eventPrefix + '/' + eventName;
135
136 try {
137 this._withAIClient((aiClient) => {
138 aiClient.pluginVersionString = validatedData?.properties.version ?? 'Unknown';
139 aiClient.track({
140 name,
141 baseData: { name, properties: validatedData?.properties, measurements: validatedData?.measurements }
142 });
143 });
144 } catch { }
145 }
147 > flush(): Promise<void> {
148 > if (this._aiCoreOrKey) { 1dsAppender.ts ×11
149 > return new Promise(resolve => {
150 > this._withAIClient((aiClient) => {
151 > aiClient.unload(true, () => {
152 this._aiCoreOrKey = undefined;
153 resolve(undefined);
155 > });
156 > });
157 > }
158 return Promise.resolve(undefined);