Skip to content

Commit a2c7721

Browse files
committed
Set xhr override before creating AppInsights client
If not supplied to the constructor the delegate AppInsightsCore class does not pick up the default channel (`BreezeChannelIdentifier = "AppInsightsChannelPlugin"`) plugin configuration and therefore doesn't use the supplied XHR override. This is subtle to notice when running in Node 18+ (VS Code 1.82+) since the AppInsights SDK mostly uses the `fetch` API which is available in Node and appears to work.
1 parent 3a87442 commit a2c7721

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/common/appInsightsClientFactory.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import type { ApplicationInsights } from "@microsoft/applicationinsights-web-basic";
7-
import type { IXHROverride } from "@microsoft/applicationinsights-core-js";
7+
import type { IXHROverride, IConfiguration } from "@microsoft/applicationinsights-core-js";
88
import { BreezeChannelIdentifier } from "@microsoft/applicationinsights-common";
99
import { ReplacementOption, SenderData } from "./baseTelemetryReporter";
1010
import { BaseTelemetryClient } from "./baseTelemetrySender";
@@ -15,6 +15,16 @@ export const appInsightsClientFactory = async (key: string, xhrOverride?: IXHROv
1515
let appInsightsClient: ApplicationInsights | undefined;
1616
try {
1717
const basicAISDK = await import/* webpackMode: "eager" */("@microsoft/applicationinsights-web-basic");
18+
const extensionConfig: IConfiguration["extensionConfig"] = {};
19+
if (xhrOverride) {
20+
// Configure the channel to use a XHR Request override since it's not available in node
21+
const channelConfig: IChannelConfiguration = {
22+
alwaysUseXhrOverride: true,
23+
httpXHROverride: xhrOverride
24+
};
25+
extensionConfig[BreezeChannelIdentifier] = channelConfig;
26+
}
27+
1828
appInsightsClient = new basicAISDK.ApplicationInsights({
1929
instrumentationKey: key,
2030
disableAjaxTracking: true,
@@ -24,19 +34,9 @@ export const appInsightsClientFactory = async (key: string, xhrOverride?: IXHROv
2434
disableCookiesUsage: true,
2535
autoTrackPageVisitTime: false,
2636
emitLineDelimitedJson: false,
27-
disableInstrumentationKeyValidation: true
28-
},
29-
);
30-
31-
if (xhrOverride) {
32-
appInsightsClient.config.extensionConfig = {};
33-
// Configure the channel to use a XHR Request override since it's not available in node
34-
const channelConfig: IChannelConfiguration = {
35-
alwaysUseXhrOverride: true,
36-
httpXHROverride: xhrOverride
37-
};
38-
appInsightsClient.config.extensionConfig[BreezeChannelIdentifier] = channelConfig;
39-
}
37+
disableInstrumentationKeyValidation: true,
38+
extensionConfig,
39+
});
4040
} catch (e) {
4141
return Promise.reject(e);
4242
}

0 commit comments

Comments
 (0)