-
Notifications
You must be signed in to change notification settings - Fork 48
/
1dsClientFactory.ts
122 lines (114 loc) · 4.39 KB
/
1dsClientFactory.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { AppInsightsCore, IExtendedConfiguration } from "@microsoft/1ds-core-js";
import type { IChannelConfiguration, IXHROverride, PostChannel } from "@microsoft/1ds-post-js";
import type * as vscode from "vscode";
import type { BaseTelemetryClient } from "./baseTelemetrySender";
import { SenderData } from "./baseTelemetryReporter";
/**
* Configures 1DS properly and returns the core client object
* @param key The ingestion key
* @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
* @returns The AI core object
*/
const getAICore = async (key: string, vscodeAPI: typeof vscode, xhrOverride?: IXHROverride): Promise<AppInsightsCore> => {
const oneDs = await import(/* webpackMode: "eager" */ "@microsoft/1ds-core-js");
const postPlugin = await import(/* webpackMode: "eager" */ "@microsoft/1ds-post-js");
const appInsightsCore = new oneDs.AppInsightsCore();
const collectorChannelPlugin: PostChannel = new postPlugin.PostChannel();
// Configure the app insights core to send to collector++ and disable logging of debug info
const coreConfig: IExtendedConfiguration = {
instrumentationKey: key,
endpointUrl: "https://mobile.events.data.microsoft.com/OneCollector/1.0",
loggingLevelTelemetry: 0,
loggingLevelConsole: 0,
disableCookiesUsage: true,
disableDbgExt: true,
disableInstrumentationKeyValidation: true,
channels: [[
collectorChannelPlugin
]]
};
if (xhrOverride) {
coreConfig.extensionConfig = {};
// Configure the channel to use a XHR Request override since it's not available in node
const channelConfig: IChannelConfiguration = {
alwaysUseXhrOverride: true,
httpXHROverride: xhrOverride
};
coreConfig.extensionConfig[collectorChannelPlugin.identifier] = channelConfig;
}
const config = vscodeAPI.workspace.getConfiguration("telemetry");
const internalTesting = config.get<boolean>("internalTesting");
appInsightsCore.initialize(coreConfig, []);
appInsightsCore.addTelemetryInitializer((envelope) => {
// Only add this flag when `telemetry.internalTesting` is enabled
if (!internalTesting) {
return;
}
envelope["ext"] = envelope["ext"] ?? {};
envelope["ext"]["utc"] = envelope["ext"]["utc"] ?? {};
// Sets it to be internal only based on Windows UTC flagging
envelope["ext"]["utc"]["flags"] = 0x0000811ECD;
});
return appInsightsCore;
};
/**
* Configures and creates a telemetry client using the 1DS sdk
* @param key The ingestion key
* @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
*/
export const oneDataSystemClientFactory = async (key: string, vscodeAPI: typeof vscode, xhrOverride?: IXHROverride): Promise<BaseTelemetryClient> => {
let appInsightsCore: AppInsightsCore | undefined = await getAICore(key, vscodeAPI, xhrOverride);
const flushOneDS = async () => {
try {
const flushPromise = new Promise<void>((resolve, reject) => {
if (!appInsightsCore) {
resolve();
return;
}
appInsightsCore.flush(true, (completedFlush) => {
if (!completedFlush) {
reject("Failed to flush app 1DS!");
return;
}
});
});
return flushPromise;
} catch (e: any) {
throw new Error("Failed to flush 1DS!\n" + e.message);
}
};
// Shape the app insights core from 1DS into a standard format
const telemetryClient: BaseTelemetryClient = {
logEvent: (eventName: string, data?: SenderData) => {
try {
appInsightsCore?.track({
name: eventName,
baseData: { name: eventName, properties: data?.properties, measurements: data?.measurements }
});
} catch (e: any) {
throw new Error("Failed to log event to app insights!\n" + e.message);
}
},
flush: flushOneDS,
dispose: async () => {
await flushOneDS();
const disposePromise = new Promise<void>((resolve) => {
if (!appInsightsCore) {
resolve();
return;
}
appInsightsCore.unload(true, () => {
resolve();
appInsightsCore = undefined;
return;
});
});
return disposePromise;
}
};
return telemetryClient;
};