Skip to content

Commit

Permalink
[Master] Add ability to disable the pollInternalLogs via config and c…
Browse files Browse the repository at this point in the history
…hange to stop using setInterval #2055
  • Loading branch information
MSNev committed Apr 20, 2023
1 parent 388f676 commit 14979d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class PageViewManager {
pageViewPerformanceManager: PageViewPerformanceManager) {

dynamicProto(PageViewManager, this, (_self) => {
let intervalHandle: any = null;
let queueTimer: any = null;
let itemQueue: Array<() => boolean> = [];
let pageViewPerformanceSent: boolean = false;
let _logger: IDiagnosticLogger;
Expand All @@ -63,11 +63,10 @@ export class PageViewManager {
}
}

function _addQueue(cb:() => boolean) {
itemQueue.push(cb);

if (!intervalHandle) {
intervalHandle = setInterval((() => {
function _startTimer() {
if (!queueTimer) {
queueTimer = setTimeout((() => {
queueTimer = null;
let allItems = itemQueue.slice(0);
let doFlush = false;
itemQueue = [];
Expand All @@ -80,9 +79,8 @@ export class PageViewManager {
}
});

if (itemQueue.length === 0) {
clearInterval(intervalHandle);
intervalHandle = null;
if (itemQueue.length > 0) {
_startTimer();
}

if (doFlush) {
Expand All @@ -93,6 +91,12 @@ export class PageViewManager {
}
}

function _addQueue(cb:() => boolean) {
itemQueue.push(cb);

_startTimer();
}

_self.trackPageView = (pageView: IPageViewTelemetry, customProperties?: { [key: string]: any }) => {
let name = pageView.name;
if (isNullOrUndefined(name) || typeof name !== "string") {
Expand Down Expand Up @@ -227,9 +231,9 @@ export class PageViewManager {
};

_self.teardown = (unloadCtx?: IProcessTelemetryUnloadContext, unloadState?: ITelemetryUnloadState) => {
if (intervalHandle) {
clearInterval(intervalHandle);
intervalHandle = null;
if (queueTimer) {
clearInterval(queueTimer);
queueTimer = null;

let allItems = itemQueue.slice(0);
let doFlush = false;
Expand Down
54 changes: 40 additions & 14 deletions shared/AppInsightsCore/src/JavaScriptSDK/BaseCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export class BaseCore implements IAppInsightsCore {
* Internal log poller
*/
let _internalLogPoller: number = 0;
let _forceStopInternalLogPoller = false;

dynamicProto(BaseCore, this, (_self) => {

Expand Down Expand Up @@ -336,17 +337,32 @@ export class BaseCore implements IAppInsightsCore {
*/
_self.pollInternalLogs = (eventName?: string): number => {
_internalLogsEventName = eventName || null;

let interval: number = getCfgValue(_config.diagnosticLogInterval);
if (!interval || !(interval > 0)) {
interval = 10000;
}
_forceStopInternalLogPoller = false;
if(_internalLogPoller) {
clearInterval(_internalLogPoller);
_internalLogPoller = null;
}

return _startInternalLogTimer(true);
}

function _startInternalLogTimer(alwaysStart?: boolean) {
if (!_internalLogPoller && !_forceStopInternalLogPoller) {
let shouldStart = alwaysStart || (_self.logger && _self.logger.queue.length > 0);
if (shouldStart) {
let interval: number = getCfgValue(_config.diagnosticLogInterval);
if (!interval || !(interval > 0)) {
interval = 10000;
}

// Keeping as an interval timer for backward compatibility as it returns the result
_internalLogPoller = setInterval(() => {
clearInterval(_internalLogPoller);
_internalLogPoller = 0;
_flushInternalLogs();
}, interval) as any;
}
}
_internalLogPoller = setInterval(() => {
_flushInternalLogs();
}, interval) as any;

return _internalLogPoller;
}
Expand All @@ -355,6 +371,7 @@ export class BaseCore implements IAppInsightsCore {
* Stop polling log messages from logger.queue
*/
_self.stopPollingInternalLogs = (): void => {
_forceStopInternalLogPoller = true;
if(_internalLogPoller) {
clearInterval(_internalLogPoller);
_internalLogPoller = 0;
Expand Down Expand Up @@ -403,6 +420,8 @@ export class BaseCore implements IAppInsightsCore {
processUnloadCtx.processNext(unloadState);
}

_flushInternalLogs();

if (!_flushChannels(isAsync, _doUnload, SendRequestReason.SdkUnload, cbTimeout)) {
_doUnload(false);
}
Expand Down Expand Up @@ -510,7 +529,10 @@ export class BaseCore implements IAppInsightsCore {
}

function _createTelCtx(): IProcessTelemetryContext {
return createProcessTelemetryContext(_getPluginChain(), _config, _self);
let theCtx = createProcessTelemetryContext(_getPluginChain(), _config, _self);
theCtx.onComplete(_startInternalLogTimer);

return theCtx;
}

// Initialize or Re-initialize the plugins
Expand Down Expand Up @@ -556,7 +578,8 @@ export class BaseCore implements IAppInsightsCore {
// Initialize the controls
_channelControl.initialize(_config, _self, allExtensions);

initializePlugins(_createTelCtx(), allExtensions);
let initCtx = _createTelCtx();
initializePlugins(initCtx, allExtensions);

// Now reset the extensions to just those being managed by Basecore
_self._extensions = objFreeze(sortPlugins(_coreExtensions || [])).slice();
Expand Down Expand Up @@ -674,6 +697,7 @@ export class BaseCore implements IAppInsightsCore {
}

removeComplete && removeComplete(removed);
_startInternalLogTimer();
});

unloadCtx.processNext(unloadState);
Expand All @@ -683,8 +707,10 @@ export class BaseCore implements IAppInsightsCore {
}

function _flushInternalLogs() {
let queue: _InternalLogMessage[] = _self.logger ? _self.logger.queue : [];
if (queue) {
if (_self.logger && _self.logger.queue) {
let queue: _InternalLogMessage[] = _self.logger.queue.slice(0);
_self.logger.queue.length = 0;

arrForEach(queue, (logMessage: _InternalLogMessage) => {
const item: ITelemetryItem = {
name: _internalLogsEventName ? _internalLogsEventName : "InternalMessageId: " + logMessage.messageId,
Expand All @@ -695,8 +721,6 @@ export class BaseCore implements IAppInsightsCore {
};
_self.track(item);
});

queue.length = 0;
}
}

Expand Down Expand Up @@ -745,6 +769,7 @@ export class BaseCore implements IAppInsightsCore {

function _doUpdate(updateState: ITelemetryUpdateState): void {
let updateCtx = createProcessTelemetryUpdateContext(_getPluginChain(), _self);
updateCtx.onComplete(_startInternalLogTimer);

if (!_self._updateHook || _self._updateHook(updateCtx, updateState) !== true) {
updateCtx.processNext(updateState);
Expand All @@ -756,6 +781,7 @@ export class BaseCore implements IAppInsightsCore {
if (logger) {
// there should always be a logger
_throwInternal(logger, eLoggingSeverity.WARNING, _eInternalMessageId.PluginException, message);
_startInternalLogTimer();
} else {
throwError(message);
}
Expand Down

0 comments on commit 14979d3

Please sign in to comment.