@microsoft/applicationinsights-dependencies-js 3.0.0-beta.2212-13 → 3.0.0-beta.2212-15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ajax.ts CHANGED
@@ -271,7 +271,6 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
271
271
  public static identifier: string = "AjaxDependencyPlugin";
272
272
 
273
273
  public identifier: string = AjaxMonitor.identifier;
274
-
275
274
  priority: number = 120;
276
275
 
277
276
  constructor() {
@@ -304,6 +303,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
304
303
  let _ajaxPerfLookupDelay: number;
305
304
  let _distributedTracingMode: eDistributedTracingModes;
306
305
  let _appId: string;
306
+ let _polyfillInitialized: boolean;
307
307
 
308
308
  dynamicProto(AjaxMonitor, this, (_self, _base) => {
309
309
  let _addHook = _base._addHook;
@@ -460,6 +460,7 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
460
460
  let location = getLocation();
461
461
  _fetchInitialized = false; // fetch monitoring initialized
462
462
  _xhrInitialized = false; // XHR monitoring initialized
463
+ _polyfillInitialized = false; // polyfill monitoring initialized
463
464
  _currentWindowHost = location && location.host && location.host.toLowerCase();
464
465
  _extensionConfig = null;
465
466
  _enableRequestHeaderTracking = false;
@@ -500,7 +501,6 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
500
501
  _enableAjaxErrorStatusText = _extensionConfig.enableAjaxErrorStatusText;
501
502
  _enableAjaxPerfTracking = _extensionConfig.enableAjaxPerfTracking;
502
503
  _maxAjaxCallsPerView = _extensionConfig.maxAjaxCallsPerView;
503
- _enableResponseHeaderTracking = _extensionConfig.enableResponseHeaderTracking;
504
504
  _excludeRequestFromAutoTrackingPatterns = [].concat(_extensionConfig.excludeRequestFromAutoTrackingPatterns || [], _extensionConfig.addIntEndpoints !== false ? _internalExcludeEndpoints : []);
505
505
  _addRequestContext = _extensionConfig.addRequestContext;
506
506
 
@@ -517,7 +517,6 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
517
517
  }
518
518
 
519
519
  _disableAjaxTracking = !!_extensionConfig.disableAjaxTracking;
520
- _disableFetchTracking = !!_extensionConfig.disableFetchTracking;
521
520
  _maxAjaxPerfLookupAttempts = _extensionConfig.maxAjaxPerfLookupAttempts;
522
521
  _ajaxPerfLookupDelay = _extensionConfig.ajaxPerfLookupDelay;
523
522
  _ignoreHeaders = _extensionConfig.ignoreHeaders;
@@ -555,83 +554,89 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
555
554
 
556
555
  let global = getGlobal();
557
556
  let isPolyfill = (fetch as any).polyfill;
558
- if (!_disableFetchTracking && !_fetchInitialized) {
559
- _addHook(InstrumentFunc(global, strFetch, {
560
- ns: _evtNamespace,
561
- // Add request hook
562
- req: (callDetails: IInstrumentCallDetails, input, init) => {
563
- let fetchData: ajaxRecord;
564
- if (!_disableFetchTracking && _fetchInitialized &&
565
- !_isDisabledRequest(null, input, init) &&
566
- // If we have a polyfil and XHR instrumented then let XHR report otherwise we get duplicates
567
- !(isPolyfill && _xhrInitialized)) {
568
- let ctx = callDetails.ctx();
569
- fetchData = _createFetchRecord(input, init);
570
- let newInit = _self.includeCorrelationHeaders(fetchData, input, init);
571
- if (newInit !== init) {
572
- callDetails.set(1, newInit);
557
+ _self._addHook(onConfigChange(_extensionConfig, () => {
558
+ _disableFetchTracking = !!_extensionConfig.disableFetchTracking;
559
+ _enableResponseHeaderTracking = _extensionConfig.enableResponseHeaderTracking;
560
+
561
+ if (!_disableFetchTracking && !_fetchInitialized) {
562
+ _addHook(InstrumentFunc(global, strFetch, {
563
+ ns: _evtNamespace,
564
+ // Add request hook
565
+ req: (callDetails: IInstrumentCallDetails, input, init) => {
566
+ let fetchData: ajaxRecord;
567
+ if (!_disableFetchTracking && _fetchInitialized &&
568
+ !_isDisabledRequest(null, input, init) &&
569
+ // If we have a polyfil and XHR instrumented then let XHR report otherwise we get duplicates
570
+ !(isPolyfill && _xhrInitialized)) {
571
+ let ctx = callDetails.ctx();
572
+ fetchData = _createFetchRecord(input, init);
573
+ let newInit = _self.includeCorrelationHeaders(fetchData, input, init);
574
+ if (newInit !== init) {
575
+ callDetails.set(1, newInit);
576
+ }
577
+ ctx.data = fetchData;
573
578
  }
574
- ctx.data = fetchData;
575
- }
576
- },
577
- rsp: (callDetails: IInstrumentCallDetails, input) => {
578
- if (!_disableFetchTracking) {
579
- let fetchData = callDetails.ctx().data;
580
- if (fetchData) {
581
- // Replace the result with the new promise from this code
582
- callDetails.rslt = callDetails.rslt.then((response: any) => {
583
- _reportFetchMetrics(callDetails, (response||{}).status, input, response, fetchData, () => {
584
- let ajaxResponse:IAjaxRecordResponse = {
585
- statusText: response.statusText,
586
- headerMap: null,
587
- correlationContext: _getFetchCorrelationContext(response)
588
- };
589
-
590
- if (_enableResponseHeaderTracking) {
591
- const responseHeaderMap = {};
592
- response.headers.forEach((value: string, name: string) => { // @skip-minify
593
- if (_canIncludeHeaders(name)) {
594
- responseHeaderMap[name] = value;
595
- }
596
- });
597
-
598
- ajaxResponse.headerMap = responseHeaderMap;
599
- }
600
-
601
- return ajaxResponse;
602
- });
603
-
604
- return response;
605
- })
606
- .catch((reason: any) => {
607
- _reportFetchMetrics(callDetails, 0, input, null, fetchData, null, { error: reason.message });
608
- throw reason;
609
- });
579
+ },
580
+ rsp: (callDetails: IInstrumentCallDetails, input) => {
581
+ if (!_disableFetchTracking) {
582
+ let fetchData = callDetails.ctx().data;
583
+ if (fetchData) {
584
+ // Replace the result with the new promise from this code
585
+ callDetails.rslt = callDetails.rslt.then((response: any) => {
586
+ _reportFetchMetrics(callDetails, (response||{}).status, input, response, fetchData, () => {
587
+ let ajaxResponse:IAjaxRecordResponse = {
588
+ statusText: response.statusText,
589
+ headerMap: null,
590
+ correlationContext: _getFetchCorrelationContext(response)
591
+ };
592
+
593
+ if (_enableResponseHeaderTracking) {
594
+ const responseHeaderMap = {};
595
+ response.headers.forEach((value: string, name: string) => { // @skip-minify
596
+ if (_canIncludeHeaders(name)) {
597
+ responseHeaderMap[name] = value;
598
+ }
599
+ });
600
+
601
+ ajaxResponse.headerMap = responseHeaderMap;
602
+ }
603
+
604
+ return ajaxResponse;
605
+ });
606
+
607
+ return response;
608
+ })
609
+ .catch((reason: any) => {
610
+ _reportFetchMetrics(callDetails, 0, input, null, fetchData, null, { error: reason.message });
611
+ throw reason;
612
+ });
613
+ }
610
614
  }
615
+ },
616
+ // Create an error callback to report any hook errors
617
+ hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxOpen,
618
+ "Failed to monitor Window.fetch" + ERROR_POSTFIX)
619
+ }));
620
+
621
+ _fetchInitialized = true;
622
+ } else if (isPolyfill && !_polyfillInitialized) {
623
+ // If fetch is a polyfill we need to capture the request to ensure that we correctly track
624
+ // disabled request URLS (i.e. internal urls) to ensure we don't end up in a constant loop
625
+ // of reporting ourselves, for example React Native uses a polyfill for fetch
626
+ // Note: Polyfill implementations that don't support the "polyfill" tag are not supported
627
+ // the workaround is to add a polyfill property to your fetch implementation before initializing
628
+ // App Insights
629
+ _addHook(InstrumentFunc(global, strFetch, {
630
+ ns: _evtNamespace,
631
+ req: (callDetails: IInstrumentCallDetails, input, init) => {
632
+ // Just call so that we record any disabled URL
633
+ _isDisabledRequest(null, input, init);
611
634
  }
612
- },
613
- // Create an error callback to report any hook errors
614
- hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxOpen,
615
- "Failed to monitor Window.fetch" + ERROR_POSTFIX)
616
- }));
617
-
618
- _fetchInitialized = true;
619
- } else if (isPolyfill) {
620
- // If fetch is a polyfill we need to capture the request to ensure that we correctly track
621
- // disabled request URLS (i.e. internal urls) to ensure we don't end up in a constant loop
622
- // of reporting ourselves, for example React Native uses a polyfill for fetch
623
- // Note: Polyfill implementations that don't support the "poyyfill" tag are not supported
624
- // the workaround is to add a polyfill property to your fetch implementation before initializing
625
- // App Insights
626
- _addHook(InstrumentFunc(global, strFetch, {
627
- ns: _evtNamespace,
628
- req: (callDetails: IInstrumentCallDetails, input, init) => {
629
- // Just call so that we record any disabled URL
630
- _isDisabledRequest(null, input, init);
631
- }
632
- }));
633
- }
634
-
635
+ }));
636
+ _polyfillInitialized = true;
637
+ }
638
+ }));
639
+
635
640
  if (isPolyfill) {
636
641
  // retag the instrumented fetch with the same polyfill settings this is mostly for testing
637
642
  // But also supports multiple App Insights usages
@@ -644,82 +649,91 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu
644
649
  }
645
650
 
646
651
  function _instrumentXhr():void {
647
- if (_supportsAjaxMonitoring(_self) && !_disableAjaxTracking && !_xhrInitialized) {
648
- // Instrument open
649
- _hookProto(XMLHttpRequest, "open", {
650
- ns: _evtNamespace,
651
- req: (args:IInstrumentCallDetails, method:string, url:string, async?:boolean) => {
652
- if (!_disableAjaxTracking) {
653
- let xhr = args.inst as XMLHttpRequestInstrumented;
654
- let ajaxData = xhr[strAjaxData];
655
- if (!_isDisabledRequest(xhr, url) && _isMonitoredXhrInstance(xhr, true)) {
656
- if (!ajaxData || !ajaxData.xhrMonitoringState.openDone) {
657
- // Only create a single ajaxData (even when multiple AI instances are running)
658
- _openHandler(xhr, method, url, async);
652
+ if (!_supportsAjaxMonitoring(_self)) {
653
+ return;
654
+ }
655
+ _self._addHook(onConfigChange(_extensionConfig, () => {
656
+ _disableAjaxTracking = !!_extensionConfig.disableAjaxTracking;
657
+ _enableRequestHeaderTracking = _extensionConfig.enableRequestHeaderTracking;
658
+
659
+ if (!_disableAjaxTracking && !_xhrInitialized) {
660
+ // Instrument open
661
+ _hookProto(XMLHttpRequest, "open", {
662
+ ns: _evtNamespace,
663
+ req: (args:IInstrumentCallDetails, method:string, url:string, async?:boolean) => {
664
+ if (!_disableAjaxTracking) {
665
+ let xhr = args.inst as XMLHttpRequestInstrumented;
666
+ let ajaxData = xhr[strAjaxData];
667
+ if (!_isDisabledRequest(xhr, url) && _isMonitoredXhrInstance(xhr, true)) {
668
+ if (!ajaxData || !ajaxData.xhrMonitoringState.openDone) {
669
+ // Only create a single ajaxData (even when multiple AI instances are running)
670
+ _openHandler(xhr, method, url, async);
671
+ }
672
+
673
+ // always attach to the on ready state change (required for handling multiple instances)
674
+ _attachToOnReadyStateChange(xhr);
659
675
  }
660
-
661
- // always attach to the on ready state change (required for handling multiple instances)
662
- _attachToOnReadyStateChange(xhr);
663
676
  }
664
- }
665
- },
666
- hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxOpen,
667
- ERROR_HEADER + ".open" + ERROR_POSTFIX)
668
- });
669
-
670
- // Instrument send
671
- _hookProto(XMLHttpRequest, "send", {
672
- ns: _evtNamespace,
673
- req: (args:IInstrumentCallDetails, context?: Document | BodyInit | null) => {
674
- if (!_disableAjaxTracking) {
675
- let xhr = args.inst as XMLHttpRequestInstrumented;
676
- let ajaxData = xhr[strAjaxData];
677
- if (_isMonitoredXhrInstance(xhr) && !ajaxData.xhrMonitoringState.sendDone) {
678
- _createMarkId("xhr", ajaxData);
679
- ajaxData.requestSentTime = dateTimeUtilsNow();
680
- _self.includeCorrelationHeaders(ajaxData, undefined, undefined, xhr);
681
- ajaxData.xhrMonitoringState.sendDone = true;
677
+ },
678
+ hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxOpen,
679
+ ERROR_HEADER + ".open" + ERROR_POSTFIX)
680
+ });
681
+
682
+ // Instrument send
683
+ _hookProto(XMLHttpRequest, "send", {
684
+ ns: _evtNamespace,
685
+ req: (args:IInstrumentCallDetails, context?: Document | BodyInit | null) => {
686
+ if (!_disableAjaxTracking) {
687
+ let xhr = args.inst as XMLHttpRequestInstrumented;
688
+ let ajaxData = xhr[strAjaxData];
689
+ if (_isMonitoredXhrInstance(xhr) && !ajaxData.xhrMonitoringState.sendDone) {
690
+ _createMarkId("xhr", ajaxData);
691
+ ajaxData.requestSentTime = dateTimeUtilsNow();
692
+ _self.includeCorrelationHeaders(ajaxData, undefined, undefined, xhr);
693
+ ajaxData.xhrMonitoringState.sendDone = true;
694
+ }
682
695
  }
683
- }
684
- },
685
- hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxSend,
686
- ERROR_HEADER + ERROR_POSTFIX)
687
- });
688
-
689
- // Instrument abort
690
- _hookProto(XMLHttpRequest, "abort", {
691
- ns: _evtNamespace,
692
- req: (args:IInstrumentCallDetails) => {
693
- if (!_disableAjaxTracking) {
694
- let xhr = args.inst as XMLHttpRequestInstrumented;
695
- let ajaxData = xhr[strAjaxData];
696
- if (_isMonitoredXhrInstance(xhr) && !ajaxData.xhrMonitoringState.abortDone) {
697
- ajaxData.aborted = 1;
698
- ajaxData.xhrMonitoringState.abortDone = true;
696
+ },
697
+ hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxSend,
698
+ ERROR_HEADER + ERROR_POSTFIX)
699
+ });
700
+
701
+ // Instrument abort
702
+ _hookProto(XMLHttpRequest, "abort", {
703
+ ns: _evtNamespace,
704
+ req: (args:IInstrumentCallDetails) => {
705
+ if (!_disableAjaxTracking) {
706
+ let xhr = args.inst as XMLHttpRequestInstrumented;
707
+ let ajaxData = xhr[strAjaxData];
708
+ if (_isMonitoredXhrInstance(xhr) && !ajaxData.xhrMonitoringState.abortDone) {
709
+ ajaxData.aborted = 1;
710
+ ajaxData.xhrMonitoringState.abortDone = true;
711
+ }
699
712
  }
700
- }
701
- },
702
- hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxAbort,
703
- ERROR_HEADER + ".abort" + ERROR_POSTFIX)
704
- });
705
-
706
- // Instrument setRequestHeader
707
- _hookProto(XMLHttpRequest, "setRequestHeader", {
708
- ns: _evtNamespace,
709
- req: (args: IInstrumentCallDetails, header: string, value: string) => {
710
- if (!_disableAjaxTracking && _enableRequestHeaderTracking) {
711
- let xhr = args.inst as XMLHttpRequestInstrumented;
712
- if (_isMonitoredXhrInstance(xhr) && _canIncludeHeaders(header)) {
713
- xhr[strAjaxData].requestHeaders[header] = value;
713
+ },
714
+ hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxAbort,
715
+ ERROR_HEADER + ".abort" + ERROR_POSTFIX)
716
+ });
717
+
718
+ // Instrument setRequestHeader
719
+ _hookProto(XMLHttpRequest, "setRequestHeader", {
720
+ ns: _evtNamespace,
721
+ req: (args: IInstrumentCallDetails, header: string, value: string) => {
722
+ if (!_disableAjaxTracking && _enableRequestHeaderTracking) {
723
+ let xhr = args.inst as XMLHttpRequestInstrumented;
724
+ if (_isMonitoredXhrInstance(xhr) && _canIncludeHeaders(header)) {
725
+ xhr[strAjaxData].requestHeaders[header] = value;
726
+ }
714
727
  }
715
- }
716
- },
717
- hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxSetRequestHeader,
718
- ERROR_HEADER + ".setRequestHeader" + ERROR_POSTFIX)
719
- });
720
-
721
- _xhrInitialized = true;
722
- }
728
+ },
729
+ hkErr: _createErrorCallbackFunc(_self, _eInternalMessageId.FailedMonitorAjaxSetRequestHeader,
730
+ ERROR_HEADER + ".setRequestHeader" + ERROR_POSTFIX)
731
+ });
732
+
733
+ _xhrInitialized = true;
734
+ }
735
+ }));
736
+
723
737
  }
724
738
 
725
739
  function _isDisabledRequest(xhr?: XMLHttpRequestInstrumented, request?: Request | string, init?: RequestInit) {
@@ -7,6 +7,7 @@ export declare const _DYN_TRACE_FLAGS = "traceFlags";
7
7
  export declare const _DYN_CONTEXT = "context";
8
8
  export declare const _DYN_TRACE_ID0 = "traceId";
9
9
  export declare const _DYN_SPAN_ID1 = "spanId";
10
+ export declare const _DYN__ADD_HOOK = "_addHook";
10
11
  export declare const _DYN_CORE = "core";
11
12
  export declare const _DYN_INCLUDE_CORRELATION_2 = "includeCorrelationHeaders";
12
13
  export declare const _DYN_GET_ABSOLUTE_URL = "getAbsoluteUrl";
@@ -20,12 +21,12 @@ export declare const _DYN_ENABLE_REQUEST_HEADE4 = "enableRequestHeaderTracking";
20
21
  export declare const _DYN_ENABLE_AJAX_ERROR_ST5 = "enableAjaxErrorStatusText";
21
22
  export declare const _DYN_ENABLE_AJAX_PERF_TRA6 = "enableAjaxPerfTracking";
22
23
  export declare const _DYN_MAX_AJAX_CALLS_PER_V7 = "maxAjaxCallsPerView";
23
- export declare const _DYN_ENABLE_RESPONSE_HEAD8 = "enableResponseHeaderTracking";
24
- export declare const _DYN_EXCLUDE_REQUEST_FROM9 = "excludeRequestFromAutoTrackingPatterns";
24
+ export declare const _DYN_EXCLUDE_REQUEST_FROM8 = "excludeRequestFromAutoTrackingPatterns";
25
25
  export declare const _DYN_ADD_REQUEST_CONTEXT = "addRequestContext";
26
- export declare const _DYN_DISABLE_AJAX_TRACKIN10 = "disableAjaxTracking";
26
+ export declare const _DYN_DISABLE_AJAX_TRACKIN9 = "disableAjaxTracking";
27
+ export declare const _DYN_AJAX_PERF_LOOKUP_DEL10 = "ajaxPerfLookupDelay";
27
28
  export declare const _DYN_DISABLE_FETCH_TRACKI11 = "disableFetchTracking";
28
- export declare const _DYN_AJAX_PERF_LOOKUP_DEL12 = "ajaxPerfLookupDelay";
29
+ export declare const _DYN_ENABLE_RESPONSE_HEAD12 = "enableResponseHeaderTracking";
29
30
  export declare const _DYN_STATUS = "status";
30
31
  export declare const _DYN_STATUS_TEXT = "statusText";
31
32
  export declare const _DYN_HEADER_MAP = "headerMap";