1
+ /*---------------------------------------------------------------------------------------------
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 { AppInsightsCore , IExtendedConfiguration } from "@microsoft/1ds-core-js" ;
7
+ import type { IChannelConfiguration , IXHROverride , PostChannel } from "@microsoft/1ds-post-js" ;
8
+ import type * as vscode from "vscode" ;
9
+ import type { BaseTelemetryClient } from "./baseTelemetryAppender" ;
10
+ import { AppenderData } from "./baseTelemetryReporter" ;
11
+
12
+ /**
13
+ * Configures 1DS properly and returns the core client object
14
+ * @param key The ingestion key
15
+ * @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
16
+ * @returns The AI core object
17
+ */
18
+ const getAICore = async ( key : string , vscodeAPI : typeof vscode , xhrOverride ?: IXHROverride ) : Promise < AppInsightsCore > => {
19
+ const oneDs = await import ( "@microsoft/1ds-core-js" ) ;
20
+ const postPlugin = await import ( "@microsoft/1ds-post-js" ) ;
21
+ const appInsightsCore = new oneDs . AppInsightsCore ( ) ;
22
+ const collectorChannelPlugin : PostChannel = new postPlugin . PostChannel ( ) ;
23
+ // Configure the app insights core to send to collector++ and disable logging of debug info
24
+ const coreConfig : IExtendedConfiguration = {
25
+ instrumentationKey : key ,
26
+ endpointUrl : "https://mobile.events.data.microsoft.com/OneCollector/1.0" ,
27
+ loggingLevelTelemetry : 0 ,
28
+ loggingLevelConsole : 0 ,
29
+ disableCookiesUsage : true ,
30
+ disableDbgExt : true ,
31
+ disableInstrumentationKeyValidation : true ,
32
+ channels : [ [
33
+ collectorChannelPlugin
34
+ ] ]
35
+ } ;
36
+
37
+ if ( xhrOverride ) {
38
+ coreConfig . extensionConfig = { } ;
39
+ // Configure the channel to use a XHR Request override since it's not available in node
40
+ const channelConfig : IChannelConfiguration = {
41
+ alwaysUseXhrOverride : true ,
42
+ httpXHROverride : xhrOverride
43
+ } ;
44
+ coreConfig . extensionConfig [ collectorChannelPlugin . identifier ] = channelConfig ;
45
+ }
46
+
47
+ const config = vscodeAPI . workspace . getConfiguration ( "telemetry" ) ;
48
+ const internalTesting = config . get < boolean > ( "internalTesting" ) ;
49
+
50
+ appInsightsCore . initialize ( coreConfig , [ ] ) ;
51
+
52
+ appInsightsCore . addTelemetryInitializer ( ( envelope ) => {
53
+ // Only add this flag when `telemetry.internalTesting` is enabled
54
+ if ( ! internalTesting ) {
55
+ return ;
56
+ }
57
+ envelope [ "ext" ] = envelope [ "ext" ] ?? { } ;
58
+ envelope [ "ext" ] [ "utc" ] = envelope [ "ext" ] [ "utc" ] ?? { } ;
59
+ // Sets it to be internal only based on Windows UTC flagging
60
+ envelope [ "ext" ] [ "utc" ] [ "flags" ] = 0x0000811ECD ;
61
+ } ) ;
62
+
63
+ return appInsightsCore ;
64
+ } ;
65
+
66
+ /**
67
+ * Configures and creates a telemetry client using the 1DS sdk
68
+ * @param key The ingestion key
69
+ * @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
70
+ */
71
+ export const oneDataSystemClientFactory = async ( key : string , vscodeAPI : typeof vscode , xhrOverride ?: IXHROverride ) : Promise < BaseTelemetryClient > => {
72
+ const appInsightsCore = await getAICore ( key , vscodeAPI , xhrOverride ) ;
73
+ // Shape the app insights core from 1DS into a standard format
74
+ const telemetryClient : BaseTelemetryClient = {
75
+ logEvent : ( eventName : string , data ?: AppenderData ) => {
76
+ try {
77
+ appInsightsCore ?. track ( {
78
+ name : eventName ,
79
+ baseData : { name : eventName , properties : data ?. properties , measurements : data ?. measurements }
80
+ } ) ;
81
+ } catch ( e : any ) {
82
+ throw new Error ( "Failed to log event to app insights!\n" + e . message ) ;
83
+ }
84
+ } ,
85
+ logException : ( _exception : Error , _data ?: AppenderData ) => {
86
+ throw new Error ( "1DS SDK does not support logging exceptions, please use logEvent for exception tracking" ) ;
87
+ } ,
88
+ flush : async ( ) => {
89
+ try {
90
+ appInsightsCore ?. unload ( ) ;
91
+ } catch ( e : any ) {
92
+ throw new Error ( "Failed to flush app insights!\n" + e . message ) ;
93
+ }
94
+ }
95
+ } ;
96
+ return telemetryClient ;
97
+ } ;
0 commit comments