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