[BUG] App Insights not auto-capturing from a Web Worker #1995

This commit is contained in:
Nev Wylie 2023-02-24 15:13:29 -08:00
Родитель a905888e69
Коммит 617e201040
4 изменённых файлов: 57 добавлений и 17 удалений

12
common/config/rush/npm-shrinkwrap.json сгенерированный
Просмотреть файл

@ -2183,9 +2183,9 @@
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.4.314", "version": "1.4.315",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.314.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.315.tgz",
"integrity": "sha512-+3RmNVx9hZLlc0gW//4yep0K5SYKmIvB5DXg1Yg6varsuAHlHwTeqeygfS8DWwLCsNOWrgj+p9qgM5WYjw1lXQ==" "integrity": "sha512-ndBQYz3Eyy3rASjjQ9poMJGoAlsZ/aZnq6GBsGL4w/4sWIAwiUHVSsMuADbxa8WJw7pZ0oxLpGbtoDt4vRTdCg=="
}, },
"node_modules/encodeurl": { "node_modules/encodeurl": {
"version": "1.0.2", "version": "1.0.2",
@ -7205,9 +7205,9 @@
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow=="
}, },
"electron-to-chromium": { "electron-to-chromium": {
"version": "1.4.314", "version": "1.4.315",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.314.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.315.tgz",
"integrity": "sha512-+3RmNVx9hZLlc0gW//4yep0K5SYKmIvB5DXg1Yg6varsuAHlHwTeqeygfS8DWwLCsNOWrgj+p9qgM5WYjw1lXQ==" "integrity": "sha512-ndBQYz3Eyy3rASjjQ9poMJGoAlsZ/aZnq6GBsGL4w/4sWIAwiUHVSsMuADbxa8WJw7pZ0oxLpGbtoDt4vRTdCg=="
}, },
"encodeurl": { "encodeurl": {
"version": "1.0.2", "version": "1.0.2",

Просмотреть файл

@ -11,7 +11,7 @@ let _appInsights: ApplicationInsights;
* @param config * @param config
* @returns * @returns
*/ */
export function initApplicationInsights(config: IConfiguration) { export function initApplicationInsights(config: IConfiguration, onInitCallback: (appInsights: ApplicationInsights, port: MessagePort) => void, port: MessagePort) {
if (!_appInsights) { if (!_appInsights) {
// Make sure we have a configuration object // Make sure we have a configuration object
@ -25,7 +25,11 @@ export function initApplicationInsights(config: IConfiguration) {
}); });
_appInsights.loadAppInsights(); _appInsights.loadAppInsights();
_appInsights.trackPageView(); // Manually call trackPageView to establish the current user/session/pageview if (_appInsights.core.isInitialized()) {
// Call the callback before the trackPageView
onInitCallback(_appInsights, port);
_appInsights.trackPageView(); // Manually call trackPageView to establish the current user/session/pageview
}
return _appInsights; return _appInsights;
} }

Просмотреть файл

@ -3,7 +3,7 @@
import { initApplicationInsights, trackPageView, unloadApplicationInsights } from "./worker-npm-init"; import { initApplicationInsights, trackPageView, unloadApplicationInsights } from "./worker-npm-init";
import { ExampleMessageType, IExampleRequest, IExampleResponse } from "./interfaces/IExampleMessage"; import { ExampleMessageType, IExampleRequest, IExampleResponse } from "./interfaces/IExampleMessage";
import { IConfiguration, INotificationListener } from "@microsoft/applicationinsights-web"; import { ApplicationInsights, IConfiguration, INotificationListener } from "@microsoft/applicationinsights-web";
import { dumpObj, objAssign } from "@nevware21/ts-utils"; import { dumpObj, objAssign } from "@nevware21/ts-utils";
/** /**
@ -12,7 +12,16 @@ import { dumpObj, objAssign } from "@nevware21/ts-utils";
* the connection string. * the connection string.
*/ */
const defaultApplicationInsightsConfig: IConfiguration = { const defaultApplicationInsightsConfig: IConfiguration = {
/**
* Telemtry logging level to instrumentation key. All logs with a severity
* level higher than the configured level will sent as telemetry data to
* the configured instrumentation key.
*
* 0: ALL iKey logging off
* 1: logs to iKey: severity >= CRITICAL
* 2: logs to iKey: severity >= WARNING
*/
loggingLevelTelemetry: 2
}; };
/** /**
@ -91,6 +100,31 @@ function notificationListener(port: MessagePort): INotificationListener {
}; };
} }
/**
* We only want to add any notification listener or telemetry initializer once
* otherwise they WILL get called multiple times during processing.
* @param appInsights
* @param port
*/
function onInitAddInitializers(appInsights: ApplicationInsights, port: MessagePort) {
// This callback is only called once, otherwise we would keep adding listeners and initializers
appInsights.core.getNotifyMgr().addNotificationListener(notificationListener(port));
// This is not normally needed, but this provides a view from the worker to the
// main page about errors that the worker is having / seeing
appInsights.addTelemetryInitializer((theEvent) => {
if (theEvent && theEvent.name && theEvent.name["startsWith"]("InternalMessageId") && theEvent.baseData) {
port.postMessage({
success: true,
message: "Internal Message: " + (theEvent.baseData?.message || "--")
});
// Drop ALL internal message from being sent to Azure Monitor portal
return false;
}
});
}
/** /**
* Initialize the SDK using the passed connection string from the request (if supplied) * Initialize the SDK using the passed connection string from the request (if supplied)
* @param request * @param request
@ -100,9 +134,8 @@ function notificationListener(port: MessagePort): INotificationListener {
function workerLoadSdk(request: IExampleRequest, port: MessagePort) { function workerLoadSdk(request: IExampleRequest, port: MessagePort) {
let theConfig = objAssign({}, defaultApplicationInsightsConfig); let theConfig = objAssign({}, defaultApplicationInsightsConfig);
theConfig.connectionString = request.connectionString; theConfig.connectionString = request.connectionString;
let appInsights = initApplicationInsights(theConfig); let appInsights = initApplicationInsights(theConfig, onInitAddInitializers, port);
if (appInsights && appInsights.core.isInitialized()) { if (appInsights && appInsights.core.isInitialized()) {
appInsights.core.getNotifyMgr().addNotificationListener(notificationListener(port));
return { return {
success: true, success: true,
message: `SDK Loaded and Initialized with - ${appInsights.config.connectionString}` message: `SDK Loaded and Initialized with - ${appInsights.config.connectionString}`

Просмотреть файл

@ -9,6 +9,7 @@ import {
IAppInsightsCore, IDiagnosticLogger, IProcessTelemetryUnloadContext, ITelemetryUnloadState, _eInternalMessageId, _throwInternal, IAppInsightsCore, IDiagnosticLogger, IProcessTelemetryUnloadContext, ITelemetryUnloadState, _eInternalMessageId, _throwInternal,
arrForEach, dumpObj, eLoggingSeverity, getDocument, getExceptionName, getLocation, isNullOrUndefined arrForEach, dumpObj, eLoggingSeverity, getDocument, getExceptionName, getLocation, isNullOrUndefined
} from "@microsoft/applicationinsights-core-js"; } from "@microsoft/applicationinsights-core-js";
import { isWebWorker } from "@nevware21/ts-utils";
import { PageViewPerformanceManager } from "./PageViewPerformanceManager"; import { PageViewPerformanceManager } from "./PageViewPerformanceManager";
/** /**
@ -99,11 +100,13 @@ export class PageViewManager {
); );
_flushChannels(true); _flushChannels(true);
// no navigation timing (IE 8, iOS Safari 8.4, Opera Mini 8 - see http://caniuse.com/#feat=nav-timing) if (!isWebWorker()) {
_throwInternal(_logger, // no navigation timing (IE 8, iOS Safari 8.4, Opera Mini 8 - see http://caniuse.com/#feat=nav-timing)
eLoggingSeverity.WARNING, _throwInternal(_logger,
_eInternalMessageId.NavigationTimingNotSupported, eLoggingSeverity.WARNING,
"trackPageView: navigation timing API used for calculation of page duration is not supported in this browser. This page view will be collected without duration and timing info."); _eInternalMessageId.NavigationTimingNotSupported,
"trackPageView: navigation timing API used for calculation of page duration is not supported in this browser. This page view will be collected without duration and timing info.");
}
return; return;
} }