-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathPostChannel.ts
1189 lines (1028 loc) · 56.2 KB
/
PostChannel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* PostManager.ts
* @author Abhilash Panwar (abpanwar); Hector Hernandez (hectorh); Nev Wylie (newylie)
* @copyright Microsoft 2018-2020
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import {
BaseTelemetryPlugin, EventLatencyValue, EventSendType, EventsDiscardedReason, IAppInsightsCore, IChannelControls, IConfigDefaults,
IExtendedConfiguration, IPlugin, IProcessTelemetryContext, IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryUnloadState,
IUnloadHook, NotificationManager, SendRequestReason, _eInternalMessageId, _throwInternal, addPageHideEventListener,
addPageShowEventListener, addPageUnloadEventListener, arrForEach, createProcessTelemetryContext, createUniqueNamespace, doPerf,
eLoggingSeverity, getWindow, isChromium, isGreaterThanZero, isNumber, mergeEvtNamespace, objForEachKey, onConfigChange, optimizeObject,
proxyFunctions, removePageHideEventListener, removePageShowEventListener, removePageUnloadEventListener, setProcessTelemetryTimings
} from "@microsoft/1ds-core-js";
import { IPromise, createPromise } from "@nevware21/ts-async";
import { ITimerHandler, objDeepFreeze } from "@nevware21/ts-utils";
import {
BE_PROFILE, EventBatchNotificationReason, IChannelConfiguration, IPostChannel, IPostTransmissionTelemetryItem, NRT_PROFILE, RT_PROFILE
} from "./DataModels";
import { EventBatch } from "./EventBatch";
import { HttpManager } from "./HttpManager";
import { STR_MSA_DEVICE_TICKET, STR_TRACE, STR_USER } from "./InternalConstants";
import { retryPolicyGetMillisToBackoffForRetry } from "./RetryPolicy";
import { ITimeoutOverrideWrapper, createTimeoutWrapper } from "./TimeoutOverrideWrapper";
const FlushCheckTimer = 0.250; // This needs to be in seconds, so this is 250ms
const MaxNumberEventPerBatch = 500;
const EventsDroppedAtOneTime = 20;
const MaxSendAttempts = 6;
const MaxSyncUnloadSendAttempts = 2; // Assuming 2 based on beforeunload and unload
const MaxBackoffCount = 4;
const MaxConnections = 2;
const MaxRequestRetriesBeforeBackoff = 1;
const MaxEventsLimitInMem = 10000;
const strEventsDiscarded = "eventsDiscarded";
let undefValue = undefined;
interface IPostChannelBatchQueue {
/**
* This is the actual queue of event batches
*/
batches: EventBatch[];
/**
* This is just a lookup map using the iKey to link to the batch in the batches queue
*/
iKeyMap: { [iKey: string]: EventBatch };
}
/**
* The default settings for the config.
* WE MUST include all defaults here to ensure that the config is created with all of the properties
* defined as dynamic.
*/
const defaultPostChannelConfig: IConfigDefaults<IChannelConfiguration> = objDeepFreeze({
eventsLimitInMem: { isVal: isGreaterThanZero, v: MaxEventsLimitInMem },
immediateEventLimit: { isVal: isGreaterThanZero, v: 500 },
autoFlushEventsLimit: { isVal: isGreaterThanZero, v: 0 },
disableAutoBatchFlushLimit: false,
httpXHROverride: { isVal: isOverrideFn, v: undefValue },
overrideInstrumentationKey: undefValue,
overrideEndpointUrl: undefValue,
disableTelemetry: false,
ignoreMc1Ms0CookieProcessing: false,
setTimeoutOverride: undefValue,
clearTimeoutOverride: undefValue,
payloadPreprocessor: undefValue,
payloadListener: undefValue,
disableEventTimings: undefValue,
valueSanitizer: undefValue,
stringifyObjects: undefValue,
enableCompoundKey: undefValue,
disableOptimizeObj: false,
// disableCacheHeader: undefValue, // See Task #7178858 - Collector requires a change to support this
transports: undefValue,
unloadTransports: undefValue,
useSendBeacon: undefValue,
disableFetchKeepAlive: undefValue,
avoidOptions: false,
xhrTimeout: undefValue,
disableXhrSync: undefValue,
alwaysUseXhrOverride: false,
maxEventRetryAttempts: { isVal: isNumber, v: MaxSendAttempts },
maxUnloadEventRetryAttempts: { isVal: isNumber, v: MaxSyncUnloadSendAttempts},
addNoResponse: undefValue
});
function isOverrideFn(httpXHROverride: any) {
return httpXHROverride && httpXHROverride.sendPOST;
}
/**
* Class that manages adding events to inbound queues and batching of events
* into requests.
* @group Classes
* @group Entrypoint
*/
export class PostChannel extends BaseTelemetryPlugin implements IChannelControls, IPostChannel {
public identifier = "PostChannel";
public priority = 1011;
public version = "#version#";
constructor() {
super();
let _postConfig: IChannelConfiguration;
let _isTeardownCalled = false;
let _flushCallbackQueue: Array<() => void> = [];
let _flushCallbackTimer: ITimerHandler;
let _paused = false;
let _immediateQueueSize = 0;
let _immediateQueueSizeLimit: number;
let _queueSize = 0;
let _queueSizeLimit: number;
let _profiles: { [profileName: string]: number[] } = {};
let _currentProfile = RT_PROFILE;
let _scheduledTimer: ITimerHandler;
let _immediateTimer: ITimerHandler;
let _currentBackoffCount;
let _timerCount;
let _httpManager: HttpManager;
let _batchQueues: { [eventLatency: number]: IPostChannelBatchQueue };
let _autoFlushEventsLimit: number | undefined;
// either MaxBatchSize * (1+ Max Connections) or _queueLimit / 6 (where 3 latency Queues [normal, realtime, cost deferred] * 2 [allow half full -- allow for retry])
let _autoFlushBatchLimit: number;
let _delayedBatchSendLatency: number;
let _delayedBatchReason: SendRequestReason;
let _optimizeObject: boolean;
let _isPageUnloadTriggered: boolean;
let _maxEventSendAttempts: number;
let _maxUnloadEventSendAttempts: number;
let _evtNamespace: string | string[];
let _timeoutWrapper: ITimeoutOverrideWrapper;
let _ignoreMc1Ms0CookieProcessing: boolean;
let _disableAutoBatchFlushLimit: boolean;
let _notificationManager: NotificationManager | undefined;
let _unloadHandlersAdded: boolean;
let _overrideInstrumentationKey: string;
let _disableTelemetry: boolean;
dynamicProto(PostChannel, this, (_self, _base) => {
_initDefaults();
// Special internal method to allow the DebugPlugin to hook embedded objects
_self["_getDbgPlgTargets"] = () => {
return [_httpManager, _postConfig];
};
_self.initialize = (theConfig: IExtendedConfiguration, core: IAppInsightsCore, extensions: IPlugin[]) => {
doPerf(core, () => "PostChannel:initialize", () => {
_base.initialize(theConfig, core, extensions);
_notificationManager = core.getNotifyMgr();
try {
_evtNamespace = mergeEvtNamespace(createUniqueNamespace(_self.identifier), core.evtNamespace && core.evtNamespace());
_self._addHook(onConfigChange(theConfig, (details) => {
let coreConfig = details.cfg;
let ctx = createProcessTelemetryContext(null, coreConfig, core);
_postConfig = ctx.getExtCfg<IChannelConfiguration>(_self.identifier, defaultPostChannelConfig);
_timeoutWrapper = createTimeoutWrapper(_postConfig.setTimeoutOverride, _postConfig.clearTimeoutOverride);
// Only try and use the optimizeObject() if this appears to be a chromium based browser and it has not been explicitly disabled
_optimizeObject = !_postConfig.disableOptimizeObj && isChromium();
_ignoreMc1Ms0CookieProcessing = _postConfig.ignoreMc1Ms0CookieProcessing;
_hookWParam(core); // _hookWParam uses _ignoreMc1Ms0CookieProcessing
_queueSizeLimit = _postConfig.eventsLimitInMem;
_immediateQueueSizeLimit = _postConfig.immediateEventLimit;
_autoFlushEventsLimit = _postConfig.autoFlushEventsLimit;
_maxEventSendAttempts = _postConfig.maxEventRetryAttempts;
_maxUnloadEventSendAttempts = _postConfig.maxUnloadEventRetryAttempts;
_disableAutoBatchFlushLimit = _postConfig.disableAutoBatchFlushLimit;
_setAutoLimits();
// Override iKey if provided in Post config if provided for during initialization
_overrideInstrumentationKey = _postConfig.overrideInstrumentationKey;
// DisableTelemetry was defined in the config provided during initialization
_disableTelemetry = !!_postConfig.disableTelemetry;
if (_unloadHandlersAdded) {
_removeUnloadHandlers();
}
let excludePageUnloadEvents = coreConfig.disablePageUnloadEvents || [];
// When running in Web browsers try to send all telemetry if page is unloaded
_unloadHandlersAdded = addPageUnloadEventListener(_handleUnloadEvents, excludePageUnloadEvents, _evtNamespace);
_unloadHandlersAdded = addPageHideEventListener(_handleUnloadEvents, excludePageUnloadEvents, _evtNamespace) || _unloadHandlersAdded;
_unloadHandlersAdded = addPageShowEventListener(_handleShowEvents, coreConfig.disablePageShowEvents, _evtNamespace) || _unloadHandlersAdded;
}));
// only initialize the manager once
_httpManager.initialize(theConfig, _self.core, _self);
} catch (e) {
// resetting the initialized state because of failure
_self.setInitialized(false);
throw e;
}
}, () => ({ theConfig, core, extensions }));
};
_self.processTelemetry = (ev: ITelemetryItem, itemCtx?: IProcessTelemetryContext): void => {
setProcessTelemetryTimings(ev, _self.identifier);
itemCtx = itemCtx || _self._getTelCtx(itemCtx);
var event = ev as IPostTransmissionTelemetryItem;
if (!_disableTelemetry && !_isTeardownCalled) {
// Override iKey if provided in Post config if provided for during initialization
if (_overrideInstrumentationKey) {
event.iKey = _overrideInstrumentationKey;
}
_addEventToQueues(event, true);
if (_isPageUnloadTriggered) {
// Unload event has been received so we need to try and flush new events
_releaseAllQueues(EventSendType.SendBeacon, SendRequestReason.Unload);
} else {
_scheduleTimer();
}
}
_self.processNext(event, itemCtx);
};
_self._doTeardown = (unloadCtx?: IProcessTelemetryUnloadContext, unloadState?: ITelemetryUnloadState) => {
_releaseAllQueues(EventSendType.SendBeacon, SendRequestReason.Unload);
_isTeardownCalled = true;
_httpManager.teardown();
_removeUnloadHandlers();
// Just register to remove all events associated with this namespace
_initDefaults();
};
function _removeUnloadHandlers() {
removePageUnloadEventListener(null, _evtNamespace);
removePageHideEventListener(null, _evtNamespace);
removePageShowEventListener(null, _evtNamespace);
}
function _hookWParam(core: IAppInsightsCore) {
var existingGetWParamMethod = core.getWParam;
core.getWParam = () => {
var wparam = 0;
if (_ignoreMc1Ms0CookieProcessing) {
wparam = wparam | 2;
}
return wparam | existingGetWParamMethod.call(core);
};
}
// Moving event handlers out from the initialize closure so that any local variables can be garbage collected
function _handleUnloadEvents(evt: any) {
let theEvt = evt || getWindow().event; // IE 8 does not pass the event
if (theEvt.type !== "beforeunload") {
// Only set the unload trigger if not beforeunload event as beforeunload can be cancelled while the other events can't
_isPageUnloadTriggered = true;
_httpManager.setUnloading(_isPageUnloadTriggered);
}
_releaseAllQueues(EventSendType.SendBeacon, SendRequestReason.Unload);
}
function _handleShowEvents(evt: any) {
// Handle the page becoming visible again
_isPageUnloadTriggered = false;
_httpManager.setUnloading(_isPageUnloadTriggered);
}
function _addEventToQueues(event: IPostTransmissionTelemetryItem, append: boolean) {
// If send attempt field is undefined we should set it to 0.
if (!event.sendAttempt) {
event.sendAttempt = 0;
}
// Add default latency
if (!event.latency) {
event.latency = EventLatencyValue.Normal;
}
// Remove extra AI properties if present
if (event.ext && event.ext[STR_TRACE]) {
delete (event.ext[STR_TRACE]);
}
if (event.ext && event.ext[STR_USER] && event.ext[STR_USER]["id"]) {
delete (event.ext[STR_USER]["id"]);
}
// v8 performance optimization for iterating over the keys
if (_optimizeObject) {
event.ext = optimizeObject(event.ext);
if (event.baseData) {
event.baseData = optimizeObject(event.baseData);
}
if (event.data) {
event.data = optimizeObject(event.data);
}
}
if (event.sync) {
// If the transmission is backed off then do not send synchronous events.
// We will convert these events to Real time latency instead.
if (_currentBackoffCount || _paused) {
event.latency = EventLatencyValue.RealTime;
event.sync = false;
} else {
// Log the event synchronously
if (_httpManager) {
// v8 performance optimization for iterating over the keys
if (_optimizeObject) {
event = optimizeObject(event);
}
_httpManager.sendSynchronousBatch(
EventBatch.create(event.iKey, [event]),
event.sync === true ? EventSendType.Synchronous : event.sync as EventSendType,
SendRequestReason.SyncEvent);
return;
}
}
}
let evtLatency = event.latency;
let queueSize = _queueSize;
let queueLimit = _queueSizeLimit;
if (evtLatency === EventLatencyValue.Immediate) {
queueSize = _immediateQueueSize;
queueLimit = _immediateQueueSizeLimit;
}
let eventDropped = false;
// Only add the event if the queue isn't full or it's a direct event (which don't add to the queue sizes)
if (queueSize < queueLimit) {
eventDropped = !_addEventToProperQueue(event, append);
} else {
let dropLatency = EventLatencyValue.Normal;
let dropNumber = EventsDroppedAtOneTime;
if (evtLatency === EventLatencyValue.Immediate) {
// Only drop other immediate events as they are not technically sharing the general queue
dropLatency = EventLatencyValue.Immediate;
dropNumber = 1;
}
// Drop old event from lower or equal latency
eventDropped = true;
if (_dropEventWithLatencyOrLess(event.iKey, event.latency, dropLatency, dropNumber)) {
eventDropped = !_addEventToProperQueue(event, append);
}
}
if (eventDropped) {
// Can't drop events from current queues because the all the slots are taken by queues that are being flushed.
_notifyEvents(strEventsDiscarded, [event], EventsDiscardedReason.QueueFull);
}
}
_self.setEventQueueLimits = (eventLimit: number, autoFlushLimit?: number) => {
_postConfig.eventsLimitInMem = _queueSizeLimit = isGreaterThanZero(eventLimit) ? eventLimit : MaxEventsLimitInMem;
_postConfig.autoFlushEventsLimit = _autoFlushEventsLimit = isGreaterThanZero(autoFlushLimit) ? autoFlushLimit : 0;
_setAutoLimits();
// We only do this check here as during normal event addition if the queue is > then events start getting dropped
let doFlush = _queueSize > eventLimit;
if (!doFlush && _autoFlushBatchLimit > 0) {
// Check the auto flush max batch size
for (let latency = EventLatencyValue.Normal; !doFlush && latency <= EventLatencyValue.RealTime; latency++) {
let batchQueue: IPostChannelBatchQueue = _batchQueues[latency];
if (batchQueue && batchQueue.batches) {
arrForEach(batchQueue.batches, (theBatch) => {
if (theBatch && theBatch.count() >= _autoFlushBatchLimit) {
// If any 1 batch is > than the limit then trigger an auto flush
doFlush = true;
}
});
}
}
}
_performAutoFlush(true, doFlush);
};
_self.pause = () => {
_clearScheduledTimer();
_paused = true;
_httpManager.pause();
};
_self.resume = () => {
_paused = false;
_httpManager.resume();
_scheduleTimer();
};
_self._loadTransmitProfiles = (profiles: { [profileName: string]: number[] }) => {
_resetTransmitProfiles();
objForEachKey(profiles, (profileName, profileValue) => {
let profLen = profileValue.length;
if (profLen >= 2) {
let directValue = (profLen > 2 ? profileValue[2] : 0);
profileValue.splice(0, profLen - 2);
// Make sure if a higher latency is set to not send then don't send lower latency
if (profileValue[1] < 0) {
profileValue[0] = -1;
}
// Make sure each latency is multiple of the latency higher then it. If not a multiple
// we round up so that it becomes a multiple.
if (profileValue[1] > 0 && profileValue[0] > 0) {
let timerMultiplier = profileValue[0] / profileValue[1];
profileValue[0] = Math.ceil(timerMultiplier) * profileValue[1];
}
// Add back the direct profile timeout
if (directValue >= 0 && profileValue[1] >= 0 && directValue > profileValue[1]) {
// Make sure if it's not disabled (< 0) then make sure it's not larger than RealTime
directValue = profileValue[1];
}
profileValue.push(directValue);
_profiles[profileName] = profileValue;
}
});
};
_self.flush = (async: boolean = true, callback?: (flushComplete?: boolean) => void, sendReason?: SendRequestReason): boolean | void | IPromise<boolean> => {
let result: IPromise<boolean>;
if (!_paused) {
sendReason = sendReason || SendRequestReason.ManualFlush;
if (async) {
if (!callback) {
result = createPromise<boolean>((resolve) => {
// Set the callback to the promise resolve callback
callback = resolve;
});
}
if (_flushCallbackTimer == null) {
// Clear the normal schedule timer as we are going to try and flush ASAP
_clearScheduledTimer();
// Move all queued events to the HttpManager so that we don't discard new events (Auto flush scenario)
_queueBatches(EventLatencyValue.Normal, EventSendType.Batched, sendReason);
_flushCallbackTimer = _createTimer(() => {
_flushCallbackTimer = null;
_flushImpl(callback, sendReason);
}, 0);
} else {
// Even if null (no callback) this will ensure after the flushImpl finishes waiting
// for a completely idle connection it will attempt to re-flush any queued events on the next cycle
_flushCallbackQueue.push(callback);
}
} else {
// Clear the normal schedule timer as we are going to try and flush ASAP
let cleared = _clearScheduledTimer();
// Now cause all queued events to be sent synchronously
_sendEventsForLatencyAndAbove(EventLatencyValue.Normal, EventSendType.Synchronous, sendReason);
callback && callback();
if (cleared) {
// restart the normal event timer if it was cleared
_scheduleTimer();
}
}
}
return result;
};
_self.setMsaAuthTicket = (ticket: string) => {
_httpManager.addHeader(STR_MSA_DEVICE_TICKET, ticket);
};
_self.hasEvents = _hasEvents;
_self._setTransmitProfile = (profileName: string) => {
if (_currentProfile !== profileName && _profiles[profileName] !== undefined) {
_clearScheduledTimer();
_currentProfile = profileName;
_scheduleTimer();
}
};
proxyFunctions(_self, () => _httpManager, [ "addResponseHandler" ]);
/**
* Batch and send events currently in the queue for the given latency.
* @param latency - Latency for which to send events.
*/
function _sendEventsForLatencyAndAbove(latency: number, sendType: EventSendType, sendReason: SendRequestReason): boolean {
let queued = _queueBatches(latency, sendType, sendReason);
// Always trigger the request as while the post channel may not have queued additional events, the httpManager may already have waiting events
_httpManager.sendQueuedRequests(sendType, sendReason);
return queued;
}
function _hasEvents(): boolean {
return _queueSize > 0;
}
/**
* Try to schedule the timer after which events will be sent. If there are
* no events to be sent, or there is already a timer scheduled, or the
* http manager doesn't have any idle connections this method is no-op.
*/
function _scheduleTimer() {
// If we had previously attempted to send requests, but the http manager didn't have any idle connections then the requests where delayed
// so try and requeue then again now
if (_delayedBatchSendLatency >= 0 && _queueBatches(_delayedBatchSendLatency, EventSendType.Batched, _delayedBatchReason)) {
_httpManager.sendQueuedRequests(EventSendType.Batched, _delayedBatchReason);
}
if (_immediateQueueSize > 0 && !_immediateTimer && !_paused) {
// During initialization _profiles enforce that the direct [2] is less than real time [1] timer value
// If the immediateTimeout is disabled the immediate events will be sent with Real Time events
let immediateTimeOut = _profiles[_currentProfile][2];
if (immediateTimeOut >= 0) {
_immediateTimer = _createTimer(() => {
_immediateTimer = null;
// Only try to send direct events
_sendEventsForLatencyAndAbove(EventLatencyValue.Immediate, EventSendType.Batched, SendRequestReason.NormalSchedule);
_scheduleTimer();
}, immediateTimeOut);
}
}
// During initialization the _profiles enforce that the normal [0] is a multiple of the real time [1] timer value
let timeOut = _profiles[_currentProfile][1];
if (!_scheduledTimer && !_flushCallbackTimer && timeOut >= 0 && !_paused) {
if (_hasEvents()) {
_scheduledTimer = _createTimer(() => {
_scheduledTimer = null;
_sendEventsForLatencyAndAbove(_timerCount === 0 ? EventLatencyValue.RealTime : EventLatencyValue.Normal, EventSendType.Batched, SendRequestReason.NormalSchedule);
// Increment the count for next cycle
_timerCount++;
_timerCount %= 2;
_scheduleTimer();
}, timeOut);
} else {
_timerCount = 0;
}
}
}
_self._backOffTransmission = () => {
if (_currentBackoffCount < MaxBackoffCount) {
_currentBackoffCount++;
_clearScheduledTimer();
_scheduleTimer();
}
};
_self._clearBackOff = () => {
if (_currentBackoffCount) {
_currentBackoffCount = 0;
_clearScheduledTimer();
_scheduleTimer();
}
};
function _initDefaults() {
_postConfig = null;
_isTeardownCalled = false;
_flushCallbackQueue = [];
_flushCallbackTimer = null;
_paused = false;
_immediateQueueSize = 0;
_immediateQueueSizeLimit = 500;
_queueSize = 0;
_queueSizeLimit = MaxEventsLimitInMem;
_profiles = {};
_currentProfile = RT_PROFILE;
_scheduledTimer = null;
_immediateTimer = null;
_currentBackoffCount = 0;
_timerCount = 0;
_batchQueues = {};
_autoFlushEventsLimit = 0;
_unloadHandlersAdded = false;
// either MaxBatchSize * (1+ Max Connections) or _queueLimit / 6 (where 3 latency Queues [normal, realtime, cost deferred] * 2 [allow half full -- allow for retry])
_autoFlushBatchLimit = 0;
_delayedBatchSendLatency = -1;
_delayedBatchReason = null;
_optimizeObject = true;
_isPageUnloadTriggered = false;
_maxEventSendAttempts = MaxSendAttempts;
_maxUnloadEventSendAttempts = MaxSyncUnloadSendAttempts;
_evtNamespace = null;
_overrideInstrumentationKey = null;
_disableTelemetry = false;
_timeoutWrapper = createTimeoutWrapper();
_httpManager = new HttpManager(MaxNumberEventPerBatch, MaxConnections, MaxRequestRetriesBeforeBackoff, {
requeue: _requeueEvents,
send: _sendingEvent,
sent: _eventsSentEvent,
drop: _eventsDropped,
rspFail: _eventsResponseFail,
oth: _otherEvent
});
_initializeProfiles();
_clearQueues();
_setAutoLimits();
}
function _createTimer(theTimerFunc: () => void, timeOut: number): ITimerHandler {
// If the transmission is backed off make the timer at least 1 sec to allow for back off.
if (timeOut === 0 && _currentBackoffCount) {
timeOut = 1;
}
let timerMultiplier = 1000;
if (_currentBackoffCount) {
timerMultiplier = retryPolicyGetMillisToBackoffForRetry(_currentBackoffCount - 1);
}
return _timeoutWrapper.set(theTimerFunc, timeOut * timerMultiplier);
}
function _clearScheduledTimer() {
if (_scheduledTimer !== null) {
_scheduledTimer.cancel();
_scheduledTimer = null;
_timerCount = 0;
return true;
}
return false;
}
// Try to send all queued events using beacons if available
function _releaseAllQueues(sendType: EventSendType, sendReason: SendRequestReason) {
_clearScheduledTimer();
// Cancel all flush callbacks
if (_flushCallbackTimer) {
_flushCallbackTimer.cancel();
_flushCallbackTimer = null;
}
if (!_paused) {
// Queue all the remaining requests to be sent. The requests will be sent using HTML5 Beacons if they are available.
_sendEventsForLatencyAndAbove(EventLatencyValue.Normal, sendType, sendReason);
}
}
/**
* Add empty queues for all latencies in the inbound queues map. This is called
* when Transmission Manager is being flushed. This ensures that new events added
* after flush are stored separately till we flush the current events.
*/
function _clearQueues() {
_batchQueues[EventLatencyValue.Immediate] = {
batches: [],
iKeyMap: {}
};
_batchQueues[EventLatencyValue.RealTime] = {
batches: [],
iKeyMap: {}
};
_batchQueues[EventLatencyValue.CostDeferred] = {
batches: [],
iKeyMap: {}
};
_batchQueues[EventLatencyValue.Normal] = {
batches: [],
iKeyMap: {}
};
}
function _getEventBatch(iKey: string, latency: number, create: boolean) {
let batchQueue: IPostChannelBatchQueue = _batchQueues[latency];
if (!batchQueue) {
latency = EventLatencyValue.Normal;
batchQueue = _batchQueues[latency];
}
let eventBatch = batchQueue.iKeyMap[iKey];
if (!eventBatch && create) {
eventBatch = EventBatch.create(iKey);
batchQueue.batches.push(eventBatch);
batchQueue.iKeyMap[iKey] = eventBatch;
}
return eventBatch;
}
function _performAutoFlush(isAsync: boolean, doFlush?: boolean) {
// Only perform the auto flush check if the httpManager has an idle connection and we are not in a backoff situation
if (_httpManager.canSendRequest() && !_currentBackoffCount) {
if (_autoFlushEventsLimit > 0 && _queueSize > _autoFlushEventsLimit) {
// Force flushing
doFlush = true;
}
if (doFlush && _flushCallbackTimer == null) {
// Auto flush the queue, adding a callback to avoid the creation of a promise
_self.flush(isAsync, () => {}, SendRequestReason.MaxQueuedEvents);
}
}
}
function _addEventToProperQueue(event: IPostTransmissionTelemetryItem, append: boolean): boolean {
// v8 performance optimization for iterating over the keys
if (_optimizeObject) {
event = optimizeObject(event);
}
const latency = event.latency;
let eventBatch = _getEventBatch(event.iKey, latency, true);
if (eventBatch.addEvent(event)) {
if (latency !== EventLatencyValue.Immediate) {
_queueSize++;
// Check for auto flushing based on total events in the queue, but not for requeued or retry events
if (append && event.sendAttempt === 0) {
// Force the flushing of the batch if the batch (specific iKey / latency combination) reaches it's auto flush limit
_performAutoFlush(!event.sync, _autoFlushBatchLimit > 0 && eventBatch.count() >= _autoFlushBatchLimit);
}
} else {
// Direct events don't need auto flushing as they are scheduled (by default) for immediate delivery
_immediateQueueSize++;
}
return true;
}
return false;
}
function _dropEventWithLatencyOrLess(iKey: string, latency: number, currentLatency: number, dropNumber: number): boolean {
while (currentLatency <= latency) {
let eventBatch = _getEventBatch(iKey, latency, true);
if (eventBatch && eventBatch.count() > 0) {
// Dropped oldest events from lowest possible latency
let droppedEvents = eventBatch.split(0, dropNumber);
let droppedCount = droppedEvents.count();
if (droppedCount > 0) {
if (currentLatency === EventLatencyValue.Immediate) {
_immediateQueueSize -= droppedCount;
} else {
_queueSize -= droppedCount;
}
_notifyBatchEvents(strEventsDiscarded, [droppedEvents], EventsDiscardedReason.QueueFull);
return true;
}
}
currentLatency++;
}
// Unable to drop any events -- lets just make sure the queue counts are correct to avoid exhaustion
_resetQueueCounts();
return false;
}
/**
* Internal helper to reset the queue counts, used as a backstop to avoid future queue exhaustion errors
* that might occur because of counting issues.
*/
function _resetQueueCounts() {
let immediateQueue = 0;
let normalQueue = 0;
for (let latency = EventLatencyValue.Normal; latency <= EventLatencyValue.Immediate; latency++) {
let batchQueue: IPostChannelBatchQueue = _batchQueues[latency];
if (batchQueue && batchQueue.batches) {
arrForEach(batchQueue.batches, (theBatch) => {
if (latency === EventLatencyValue.Immediate) {
immediateQueue += theBatch.count();
} else {
normalQueue += theBatch.count();
}
});
}
}
_queueSize = normalQueue;
_immediateQueueSize = immediateQueue;
}
function _queueBatches(latency: number, sendType: EventSendType, sendReason: SendRequestReason): boolean {
let eventsQueued = false;
let isAsync = sendType === EventSendType.Batched;
// Only queue batches (to the HttpManager) if this is a sync request or the httpManager has an idle connection
// Thus keeping the events within the PostChannel until the HttpManager has a connection available
// This is so we can drop "old" events if the queue is getting full because we can't successfully send events
if (!isAsync || _httpManager.canSendRequest()) {
doPerf(_self.core, () => "PostChannel._queueBatches", () => {
let droppedEvents = [];
let latencyToProcess = EventLatencyValue.Immediate;
while (latencyToProcess >= latency) {
let batchQueue: IPostChannelBatchQueue = _batchQueues[latencyToProcess];
if (batchQueue && batchQueue.batches && batchQueue.batches.length > 0) {
arrForEach(batchQueue.batches, (theBatch) => {
// Add the batch to the http manager to send the requests
if (!_httpManager.addBatch(theBatch)) {
// The events from this iKey are being dropped (killed)
droppedEvents = droppedEvents.concat(theBatch.events());
} else {
eventsQueued = eventsQueued || (theBatch && theBatch.count() > 0);
}
if (latencyToProcess === EventLatencyValue.Immediate) {
_immediateQueueSize -= theBatch.count();
} else {
_queueSize -= theBatch.count();
}
});
// Remove all batches from this Queue
batchQueue.batches = [];
batchQueue.iKeyMap = {};
}
latencyToProcess--;
}
if (droppedEvents.length > 0) {
_notifyEvents(strEventsDiscarded, droppedEvents, EventsDiscardedReason.KillSwitch);
}
if (eventsQueued && _delayedBatchSendLatency >= latency) {
// We have queued events at the same level as the delayed values so clear the setting
_delayedBatchSendLatency = -1;
_delayedBatchReason = SendRequestReason.Undefined;
}
}, () => ({ latency, sendType, sendReason }), !isAsync);
} else {
// remember the min latency so that we can re-trigger later
_delayedBatchSendLatency = _delayedBatchSendLatency >= 0 ? Math.min(_delayedBatchSendLatency, latency) : latency;
_delayedBatchReason = Math.max(_delayedBatchReason, sendReason);
}
return eventsQueued;
}
/**
* This is the callback method is called as part of the manual flushing process.
* @param callback
* @param sendReason
*/
function _flushImpl(callback: () => void, sendReason: SendRequestReason) {
// Add any additional queued events and cause all queued events to be sent asynchronously
_sendEventsForLatencyAndAbove(EventLatencyValue.Normal, EventSendType.Batched, sendReason);
// All events (should) have been queue -- lets just make sure the queue counts are correct to avoid queue exhaustion (previous bug #9685112)
_resetQueueCounts();
_waitForIdleManager(() => {
// Only called AFTER the httpManager does not have any outstanding requests
if (callback) {
callback();
}
if (_flushCallbackQueue.length > 0) {
_flushCallbackTimer = _createTimer(() => {
_flushCallbackTimer = null;
_flushImpl(_flushCallbackQueue.shift(), sendReason);
}, 0);
} else {
// No more flush requests
_flushCallbackTimer = null;
// Restart the normal timer schedule
_scheduleTimer();
}
});
}
function _waitForIdleManager(callback: () => void) {
if (_httpManager.isCompletelyIdle()) {
callback();
} else {
_flushCallbackTimer = _createTimer(() => {
_flushCallbackTimer = null;
_waitForIdleManager(callback);
}, FlushCheckTimer);
}
}
/**
* Resets the transmit profiles to the default profiles of Real Time, Near Real Time
* and Best Effort. This removes all the custom profiles that were loaded.
*/
function _resetTransmitProfiles() {
_clearScheduledTimer();
_initializeProfiles();
_currentProfile = RT_PROFILE;
_scheduleTimer();
}
function _initializeProfiles() {
_profiles = {};
_profiles[RT_PROFILE] = [2, 1, 0];
_profiles[NRT_PROFILE] = [6, 3, 0];
_profiles[BE_PROFILE] = [18, 9, 0];
}
/**
* The notification handler for requeue events
* @ignore
*/
function _requeueEvents(batches: EventBatch[], reason?: number) {
let droppedEvents: IPostTransmissionTelemetryItem[] = [];
let maxSendAttempts = _maxEventSendAttempts;
if (_isPageUnloadTriggered) {
// If a page unlaod has been triggered reduce the number of times we try to "retry"
maxSendAttempts = _maxUnloadEventSendAttempts;
}
arrForEach(batches, (theBatch) => {
if (theBatch && theBatch.count() > 0) {
arrForEach(theBatch.events(), (theEvent: IPostTransmissionTelemetryItem) => {
if (theEvent) {
// Check if the request being added back is for a sync event in which case mark it no longer a sync event
if (theEvent.sync) {
theEvent.latency = EventLatencyValue.Immediate;
theEvent.sync = false;
}
if (theEvent.sendAttempt < maxSendAttempts) {
// Reset the event timings
setProcessTelemetryTimings(theEvent, _self.identifier);
_addEventToQueues(theEvent, false);
} else {
droppedEvents.push(theEvent);
}
}
});
}
});
if (droppedEvents.length > 0) {
_notifyEvents(strEventsDiscarded, droppedEvents, EventsDiscardedReason.NonRetryableStatus);
}
if (_isPageUnloadTriggered) {
// Unload event has been received so we need to try and flush new events
_releaseAllQueues(EventSendType.SendBeacon, SendRequestReason.Unload);
}
}
function _callNotification(evtName: string, theArgs: any[]) {
let manager = (_notificationManager || ({} as NotificationManager));
let notifyFunc = manager[evtName];
if (notifyFunc) {
try {
notifyFunc.apply(manager, theArgs);
} catch (e) {
_throwInternal(_self.diagLog(),
eLoggingSeverity.CRITICAL,
_eInternalMessageId.NotificationException,
evtName + " notification failed: " + e);
}
}
}
function _notifyEvents(evtName: string, theEvents: IPostTransmissionTelemetryItem[], ...extraArgs) {
if (theEvents && theEvents.length > 0) {
_callNotification(evtName, [theEvents].concat(extraArgs));
}
}
function _notifyBatchEvents(evtName: string, batches: EventBatch[], ...extraArgs) {
if (batches && batches.length > 0) {
arrForEach(batches, (theBatch) => {
if (theBatch && theBatch.count() > 0) {
_callNotification(evtName, [theBatch.events()].concat(extraArgs));
}
});
}
}
/**
* The notification handler for when batches are about to be sent
* @ignore