@@ -11,13 +11,14 @@ import {
1111 IAutoExceptionTelemetry , IChannelControls , IConfig , IConfigDefaults , IConfiguration , ICookieMgr , ICustomProperties , IDependencyTelemetry ,
1212 IDiagnosticLogger , IDistributedTraceContext , IDynamicConfigHandler , IEventTelemetry , IExceptionTelemetry , ILoadedPlugin ,
1313 IMetricTelemetry , INotificationManager , IOTelApi , IOTelSpanOptions , IPageViewPerformanceTelemetry , IPageViewTelemetry , IPlugin ,
14- IReadableSpan , IRequestHeaders , ISdkStatsNotifCbk , ISpanScope , ITelemetryContext as Common_ITelemetryContext , ITelemetryInitializerHandler , ITelemetryItem ,
15- ITelemetryPlugin , ITelemetryUnloadState , IThrottleInterval , IThrottleLimit , IThrottleMgrConfig , ITraceApi , ITraceProvider ,
16- ITraceTelemetry , IUnloadHook , OTelTimeInput , PropertiesPluginIdentifier , ThrottleMgr , UnloadHandler , WatcherFunction ,
17- _eInternalMessageId , _throwInternal , addPageHideEventListener , addPageUnloadEventListener , cfgDfMerge , cfgDfValidate ,
18- createDynamicConfig , createOTelApi , createProcessTelemetryContext , createSdkStatsNotifCbk , createTraceProvider , createUniqueNamespace , doPerf , eLoggingSeverity ,
19- hasDocument , hasWindow , isArray , isFeatureEnabled , isFunction , isNullOrUndefined , isReactNative , isString , mergeEvtNamespace ,
20- onConfigChange , parseConnectionString , proxyAssign , proxyFunctions , removePageHideEventListener , removePageUnloadEventListener , useSpan
14+ IReadableSpan , IRequestHeaders , ISdkStatsNotifCbk , ISpanScope , ITelemetryContext as Common_ITelemetryContext ,
15+ ITelemetryInitializerHandler , ITelemetryItem , ITelemetryPlugin , ITelemetryUnloadState , IThrottleInterval , IThrottleLimit ,
16+ IThrottleMgrConfig , ITraceApi , ITraceProvider , ITraceTelemetry , IUnloadHook , OTelTimeInput , PropertiesPluginIdentifier , ThrottleMgr ,
17+ UnloadHandler , WatcherFunction , _eInternalMessageId , _throwInternal , addPageHideEventListener , addPageUnloadEventListener , cfgDfMerge ,
18+ cfgDfValidate , createDynamicConfig , createOTelApi , createProcessTelemetryContext , createSdkStatsNotifCbk , createTraceProvider ,
19+ createUniqueNamespace , doPerf , eLoggingSeverity , hasDocument , hasWindow , isArray , isFeatureEnabled , isFunction , isNullOrUndefined ,
20+ isReactNative , isString , mergeEvtNamespace , onConfigChange , parseConnectionString , proxyAssign , proxyFunctions ,
21+ removePageHideEventListener , removePageUnloadEventListener , useSpan
2122} from "@microsoft/applicationinsights-core-js" ;
2223import {
2324 AjaxPlugin as DependenciesPlugin , DependencyInitializerFunction , DependencyListenerFunction , IDependencyInitializerHandler ,
@@ -398,10 +399,15 @@ export class AppInsightsSku implements IApplicationInsights<IConfiguration & ICo
398399 // Register SDK Stats notification listener (on by default)
399400 if ( isFeatureEnabled ( SDK_STATS , _config , true ) ) {
400401 _sdkStatsListener = createSdkStatsNotifCbk ( {
401- trk : function ( item : ITelemetryItem ) { _core . track ( item ) ; } ,
402+ trk : function ( item : ITelemetryItem ) {
403+ _core . track ( item ) ;
404+ } ,
402405 lang : "JavaScript" ,
403406 ver : SDK_STATS_VERSION ,
404- int : SDK_STATS_FLUSH_INTERVAL
407+ int : SDK_STATS_FLUSH_INTERVAL ,
408+ fnFlush : function ( ) {
409+ _self . onunloadFlush ( false ) ;
410+ }
405411 } ) ;
406412 _core . addNotificationListener ( _sdkStatsListener ) ;
407413 }
@@ -589,13 +595,6 @@ export class AppInsightsSku implements IApplicationInsights<IConfiguration & ICo
589595 if ( ! unloadDone ) {
590596 unloadDone = true ;
591597
592- // Flush and remove SDK Stats listener before unload
593- if ( _sdkStatsListener && _core ) {
594- _sdkStatsListener . flush ( ) ;
595- _core . removeNotificationListener ( _sdkStatsListener ) ;
596- _sdkStatsListener = null ;
597- }
598-
599598 // Reset OTel API to clean up all trace state before unloading core
600599 if ( _core ) {
601600 // Clear the trace provider to stop any active spans
@@ -619,6 +618,15 @@ export class AppInsightsSku implements IApplicationInsights<IConfiguration & ICo
619618
620619 _removePageEventHandlers ( ) ;
621620
621+ // Unload SDK Stats listener BEFORE core.unload() tears down the pipeline.
622+ // unload() calls core.track() to enqueue the accumulated metrics,
623+ // then calls fnFlush to flush the channel so they actually get sent.
624+ if ( _sdkStatsListener && _core ) {
625+ _sdkStatsListener . unload ( ) ;
626+ _core . removeNotificationListener ( _sdkStatsListener ) ;
627+ _sdkStatsListener = null ;
628+ }
629+
622630 _core . unload && _core . unload ( isAsync , _unloadCallback , cbTimeout ) ;
623631
624632 return result ;
0 commit comments