Skip to content

Commit

Permalink
Set xhr override before creating AppInsights client
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devm33 committed Feb 20, 2024
1 parent 3a87442 commit a2c7721
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/common/appInsightsClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import type { ApplicationInsights } from "@microsoft/applicationinsights-web-basic";
import type { IXHROverride } from "@microsoft/applicationinsights-core-js";
import type { IXHROverride, IConfiguration } from "@microsoft/applicationinsights-core-js";
import { BreezeChannelIdentifier } from "@microsoft/applicationinsights-common";
import { ReplacementOption, SenderData } from "./baseTelemetryReporter";
import { BaseTelemetryClient } from "./baseTelemetrySender";
Expand All @@ -15,6 +15,16 @@ export const appInsightsClientFactory = async (key: string, xhrOverride?: IXHROv
let appInsightsClient: ApplicationInsights | undefined;
try {
const basicAISDK = await import/* webpackMode: "eager" */("@microsoft/applicationinsights-web-basic");
const extensionConfig: IConfiguration["extensionConfig"] = {};
if (xhrOverride) {
// Configure the channel to use a XHR Request override since it's not available in node
const channelConfig: IChannelConfiguration = {
alwaysUseXhrOverride: true,
httpXHROverride: xhrOverride
};
extensionConfig[BreezeChannelIdentifier] = channelConfig;
}

appInsightsClient = new basicAISDK.ApplicationInsights({
instrumentationKey: key,
disableAjaxTracking: true,
Expand All @@ -24,19 +34,9 @@ export const appInsightsClientFactory = async (key: string, xhrOverride?: IXHROv
disableCookiesUsage: true,
autoTrackPageVisitTime: false,
emitLineDelimitedJson: false,
disableInstrumentationKeyValidation: true
},
);

if (xhrOverride) {
appInsightsClient.config.extensionConfig = {};
// Configure the channel to use a XHR Request override since it's not available in node
const channelConfig: IChannelConfiguration = {
alwaysUseXhrOverride: true,
httpXHROverride: xhrOverride
};
appInsightsClient.config.extensionConfig[BreezeChannelIdentifier] = channelConfig;
}
disableInstrumentationKeyValidation: true,
extensionConfig,
});
} catch (e) {
return Promise.reject(e);
}
Expand Down

0 comments on commit a2c7721

Please sign in to comment.