@microsoft/applicationinsights-core-js 2.7.5-nightly.2202-11 → 2.7.5-nightly.2203-03

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.
Files changed (160) hide show
  1. package/browser/applicationinsights-core-js.integrity.json +9 -9
  2. package/browser/applicationinsights-core-js.js +1999 -793
  3. package/browser/applicationinsights-core-js.js.map +1 -1
  4. package/browser/applicationinsights-core-js.min.js +2 -2
  5. package/browser/applicationinsights-core-js.min.js.map +1 -1
  6. package/dist/applicationinsights-core-js.api.json +15679 -8835
  7. package/dist/applicationinsights-core-js.api.md +425 -48
  8. package/dist/applicationinsights-core-js.d.ts +784 -123
  9. package/dist/applicationinsights-core-js.js +1999 -793
  10. package/dist/applicationinsights-core-js.js.map +1 -1
  11. package/dist/applicationinsights-core-js.min.js +2 -2
  12. package/dist/applicationinsights-core-js.min.js.map +1 -1
  13. package/dist/applicationinsights-core-js.rollup.d.ts +784 -123
  14. package/dist-esm/JavaScriptSDK/AppInsightsCore.js +3 -69
  15. package/dist-esm/JavaScriptSDK/AppInsightsCore.js.map +1 -1
  16. package/dist-esm/JavaScriptSDK/BaseCore.js +496 -99
  17. package/dist-esm/JavaScriptSDK/BaseCore.js.map +1 -1
  18. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js +120 -21
  19. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js.map +1 -1
  20. package/dist-esm/JavaScriptSDK/ChannelController.js +202 -87
  21. package/dist-esm/JavaScriptSDK/ChannelController.js.map +1 -1
  22. package/dist-esm/JavaScriptSDK/Constants.js +1 -1
  23. package/dist-esm/JavaScriptSDK/CookieMgr.js +4 -4
  24. package/dist-esm/JavaScriptSDK/CookieMgr.js.map +1 -1
  25. package/dist-esm/JavaScriptSDK/CoreUtils.js +7 -148
  26. package/dist-esm/JavaScriptSDK/CoreUtils.js.map +1 -1
  27. package/dist-esm/JavaScriptSDK/DataCacheHelper.js +93 -0
  28. package/dist-esm/JavaScriptSDK/DataCacheHelper.js.map +1 -0
  29. package/dist-esm/JavaScriptSDK/DbgExtensionUtils.js +1 -1
  30. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js +25 -12
  31. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js.map +1 -1
  32. package/dist-esm/JavaScriptSDK/EnvUtils.js +13 -12
  33. package/dist-esm/JavaScriptSDK/EnvUtils.js.map +1 -1
  34. package/dist-esm/JavaScriptSDK/EventHelpers.js +477 -0
  35. package/dist-esm/JavaScriptSDK/EventHelpers.js.map +1 -0
  36. package/dist-esm/JavaScriptSDK/HelperFuncs.js +89 -86
  37. package/dist-esm/JavaScriptSDK/HelperFuncs.js.map +1 -1
  38. package/dist-esm/JavaScriptSDK/InstrumentHooks.js +3 -1
  39. package/dist-esm/JavaScriptSDK/InstrumentHooks.js.map +1 -1
  40. package/dist-esm/JavaScriptSDK/InternalConstants.js +28 -0
  41. package/dist-esm/JavaScriptSDK/InternalConstants.js.map +1 -0
  42. package/dist-esm/JavaScriptSDK/NotificationManager.js +37 -42
  43. package/dist-esm/JavaScriptSDK/NotificationManager.js.map +1 -1
  44. package/dist-esm/JavaScriptSDK/PerfManager.js +1 -1
  45. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js +430 -119
  46. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js.map +1 -1
  47. package/dist-esm/JavaScriptSDK/RandomHelper.js +30 -4
  48. package/dist-esm/JavaScriptSDK/RandomHelper.js.map +1 -1
  49. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js +60 -14
  50. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js.map +1 -1
  51. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js +81 -0
  52. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js.map +1 -0
  53. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js +33 -0
  54. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js.map +1 -0
  55. package/dist-esm/JavaScriptSDK.Enums/EventsDiscardedReason.js +1 -1
  56. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js +90 -96
  57. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js.map +1 -1
  58. package/dist-esm/JavaScriptSDK.Enums/SendRequestReason.js +1 -1
  59. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js +8 -0
  60. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js.map +1 -0
  61. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js +8 -0
  62. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js.map +1 -0
  63. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js +1 -2
  64. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js.map +1 -1
  65. package/dist-esm/JavaScriptSDK.Interfaces/IChannelControls.js +1 -1
  66. package/dist-esm/JavaScriptSDK.Interfaces/IConfiguration.js +1 -1
  67. package/dist-esm/JavaScriptSDK.Interfaces/ICookieMgr.js +1 -1
  68. package/dist-esm/JavaScriptSDK.Interfaces/IDbgExtension.js +1 -1
  69. package/dist-esm/JavaScriptSDK.Interfaces/IDiagnosticLogger.js +1 -1
  70. package/dist-esm/JavaScriptSDK.Interfaces/IInstrumentHooks.js +1 -1
  71. package/dist-esm/JavaScriptSDK.Interfaces/INotificationListener.js +1 -1
  72. package/dist-esm/JavaScriptSDK.Interfaces/INotificationManager.js +1 -1
  73. package/dist-esm/JavaScriptSDK.Interfaces/IPerfEvent.js +1 -1
  74. package/dist-esm/JavaScriptSDK.Interfaces/IPerfManager.js +1 -1
  75. package/dist-esm/JavaScriptSDK.Interfaces/IProcessTelemetryContext.js +1 -1
  76. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js +6 -0
  77. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js.map +1 -0
  78. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryItem.js +1 -1
  79. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPlugin.js +1 -1
  80. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js +1 -2
  81. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js.map +1 -1
  82. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js +8 -0
  83. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js.map +1 -0
  84. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js +8 -0
  85. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js.map +1 -0
  86. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js +6 -0
  87. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js.map +1 -0
  88. package/dist-esm/applicationinsights-core-js.js +12 -7
  89. package/dist-esm/applicationinsights-core-js.js.map +1 -1
  90. package/package.json +2 -2
  91. package/src/JavaScriptSDK/AppInsightsCore.ts +2 -101
  92. package/src/JavaScriptSDK/BaseCore.ts +696 -120
  93. package/src/JavaScriptSDK/BaseTelemetryPlugin.ts +205 -31
  94. package/src/JavaScriptSDK/ChannelController.ts +242 -103
  95. package/src/JavaScriptSDK/CookieMgr.ts +7 -5
  96. package/src/JavaScriptSDK/CoreUtils.ts +8 -172
  97. package/src/JavaScriptSDK/DataCacheHelper.ts +106 -0
  98. package/src/JavaScriptSDK/DiagnosticLogger.ts +27 -12
  99. package/src/JavaScriptSDK/EnvUtils.ts +13 -12
  100. package/src/JavaScriptSDK/EventHelpers.ts +550 -0
  101. package/src/JavaScriptSDK/HelperFuncs.ts +123 -97
  102. package/src/JavaScriptSDK/InstrumentHooks.ts +4 -2
  103. package/src/JavaScriptSDK/InternalConstants.ts +26 -0
  104. package/src/JavaScriptSDK/NotificationManager.ts +34 -37
  105. package/src/JavaScriptSDK/PerfManager.ts +4 -7
  106. package/src/JavaScriptSDK/ProcessTelemetryContext.ts +543 -134
  107. package/src/JavaScriptSDK/RandomHelper.ts +35 -3
  108. package/src/JavaScriptSDK/TelemetryHelpers.ts +83 -21
  109. package/src/JavaScriptSDK/TelemetryInitializerPlugin.ts +111 -0
  110. package/src/JavaScriptSDK/UnloadHandlerContainer.ts +46 -0
  111. package/src/JavaScriptSDK.Enums/LoggingEnums.ts +184 -87
  112. package/src/JavaScriptSDK.Enums/SendRequestReason.ts +5 -0
  113. package/src/JavaScriptSDK.Enums/TelemetryUnloadReason.ts +27 -0
  114. package/src/JavaScriptSDK.Enums/TelemetryUpdateReason.ts +27 -0
  115. package/src/JavaScriptSDK.Interfaces/IAppInsightsCore.ts +82 -2
  116. package/src/JavaScriptSDK.Interfaces/IChannelControls.ts +16 -5
  117. package/src/JavaScriptSDK.Interfaces/IInstrumentHooks.ts +16 -6
  118. package/src/JavaScriptSDK.Interfaces/IProcessTelemetryContext.ts +94 -14
  119. package/src/JavaScriptSDK.Interfaces/ITelemetryInitializers.ts +16 -0
  120. package/src/JavaScriptSDK.Interfaces/ITelemetryPlugin.ts +28 -10
  121. package/src/JavaScriptSDK.Interfaces/ITelemetryPluginChain.ts +9 -11
  122. package/src/JavaScriptSDK.Interfaces/ITelemetryUnloadState.ts +10 -0
  123. package/src/JavaScriptSDK.Interfaces/ITelemetryUpdateState.ts +34 -0
  124. package/src/JavaScriptSDK.Interfaces/IUnloadableComponent.ts +17 -0
  125. package/types/JavaScriptSDK/AppInsightsCore.d.ts +0 -21
  126. package/types/JavaScriptSDK/BaseCore.d.ts +81 -3
  127. package/types/JavaScriptSDK/BaseTelemetryPlugin.d.ts +53 -10
  128. package/types/JavaScriptSDK/ChannelController.d.ts +16 -17
  129. package/types/JavaScriptSDK/CoreUtils.d.ts +1 -48
  130. package/types/JavaScriptSDK/DataCacheHelper.d.ts +13 -0
  131. package/types/JavaScriptSDK/DiagnosticLogger.d.ts +10 -0
  132. package/types/JavaScriptSDK/EventHelpers.d.ts +154 -0
  133. package/types/JavaScriptSDK/HelperFuncs.d.ts +33 -32
  134. package/types/JavaScriptSDK/InternalConstants.d.ts +20 -0
  135. package/types/JavaScriptSDK/PerfManager.d.ts +1 -1
  136. package/types/JavaScriptSDK/ProcessTelemetryContext.d.ts +69 -6
  137. package/types/JavaScriptSDK/RandomHelper.d.ts +6 -0
  138. package/types/JavaScriptSDK/TelemetryHelpers.d.ts +23 -3
  139. package/types/JavaScriptSDK/TelemetryInitializerPlugin.d.ts +16 -0
  140. package/types/JavaScriptSDK/UnloadHandlerContainer.d.ts +11 -0
  141. package/types/JavaScriptSDK.Enums/LoggingEnums.d.ts +91 -2
  142. package/types/JavaScriptSDK.Enums/SendRequestReason.d.ts +4 -0
  143. package/types/JavaScriptSDK.Enums/TelemetryUnloadReason.d.ts +21 -0
  144. package/types/JavaScriptSDK.Enums/TelemetryUpdateReason.d.ts +20 -0
  145. package/types/JavaScriptSDK.Interfaces/IAppInsightsCore.d.ts +72 -1
  146. package/types/JavaScriptSDK.Interfaces/IChannelControls.d.ts +16 -5
  147. package/types/JavaScriptSDK.Interfaces/IInstrumentHooks.d.ts +9 -0
  148. package/types/JavaScriptSDK.Interfaces/IProcessTelemetryContext.d.ts +82 -11
  149. package/types/JavaScriptSDK.Interfaces/ITelemetryInitializers.d.ts +13 -0
  150. package/types/JavaScriptSDK.Interfaces/ITelemetryPlugin.d.ts +25 -8
  151. package/types/JavaScriptSDK.Interfaces/ITelemetryPluginChain.d.ts +9 -10
  152. package/types/JavaScriptSDK.Interfaces/ITelemetryUnloadState.d.ts +6 -0
  153. package/types/JavaScriptSDK.Interfaces/ITelemetryUpdateState.d.ts +22 -0
  154. package/types/JavaScriptSDK.Interfaces/IUnloadableComponent.d.ts +13 -0
  155. package/types/applicationinsights-core-js.d.ts +18 -9
  156. package/types/tsdoc-metadata.json +1 -1
  157. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js +0 -76
  158. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js.map +0 -1
  159. package/src/JavaScriptSDK/TelemetryPluginChain.ts +0 -120
  160. package/types/JavaScriptSDK/TelemetryPluginChain.d.ts +0 -32
@@ -1,155 +1,466 @@
1
1
  /*
2
- * Application Insights JavaScript SDK - Core, 2.7.5-nightly.2202-11
2
+ * Application Insights JavaScript SDK - Core, 2.7.5-nightly.2203-03
3
3
  * Copyright (c) Microsoft and contributors. All rights reserved.
4
4
  */
5
5
 
6
6
 
7
7
  "use strict";
8
- import { safeGetLogger } from "./DiagnosticLogger";
9
- import { TelemetryPluginChain } from "./TelemetryPluginChain";
10
- import { arrForEach, isFunction, isNullOrUndefined, isUndefined } from "./HelperFuncs";
8
+ import { safeGetLogger, _throwInternal } from "./DiagnosticLogger";
9
+ import { arrForEach, isArray, isFunction, isNullOrUndefined, isObject, isUndefined, objExtend, objForEachKey, objFreeze, objKeys, proxyFunctions } from "./HelperFuncs";
10
+ import { doPerf } from "./PerfManager";
11
+ import { dumpObj } from "./EnvUtils";
12
+ import { strCore, strDisabled, strEmpty, strIsInitialized, strTeardown, strUpdate } from "./InternalConstants";
13
+ import { _getPluginState } from "./TelemetryHelpers";
14
+ var strTelemetryPluginChain = "TelemetryPluginChain";
15
+ var strHasRunFlags = "_hasRun";
16
+ var strGetTelCtx = "_getTelCtx";
17
+ var _chainId = 0;
18
+ function _getNextProxyStart(proxy, config, core, startAt) {
19
+ while (proxy) {
20
+ if (proxy.getPlugin() === startAt) {
21
+ return proxy;
22
+ }
23
+ proxy = proxy.getNext();
24
+ }
25
+ // This wasn't found in the existing chain so create an isolated one with just this plugin
26
+ return createTelemetryProxyChain([startAt], config, core);
27
+ }
11
28
  /**
12
- * Creates the instance execution chain for the plugins
29
+ * @ignore
30
+ * @param telemetryChain
31
+ * @param config
32
+ * @param core
33
+ * @param startAt - Identifies the next plugin to execute, if null there is no "next" plugin and if undefined it should assume the start of the chain
34
+ * @returns
13
35
  */
14
- function _createProxyChain(plugins, itemCtx) {
15
- var proxies = [];
16
- if (plugins && plugins.length > 0) {
17
- // Create the proxies and wire up the next plugin chain
18
- var lastProxy = null;
19
- for (var idx = 0; idx < plugins.length; idx++) {
20
- var thePlugin = plugins[idx];
21
- if (thePlugin && isFunction(thePlugin.processTelemetry)) {
22
- // Only add plugins that are processors
23
- var newProxy = new TelemetryPluginChain(thePlugin, itemCtx);
24
- proxies.push(newProxy);
25
- if (lastProxy) {
26
- // Set this new proxy as the next for the previous one
27
- lastProxy.setNext(newProxy);
36
+ function _createInternalContext(telemetryChain, config, core, startAt) {
37
+ // We have a special case where we want to start execution from this specific plugin
38
+ // or we simply reuse the existing telemetry plugin chain (normal execution case)
39
+ var _nextProxy = null; // By Default set as no next plugin
40
+ var _onComplete = [];
41
+ if (startAt !== null) {
42
+ // There is no next element (null) vs not defined (undefined) so use the full chain
43
+ _nextProxy = startAt ? _getNextProxyStart(telemetryChain, config, core, startAt) : telemetryChain;
44
+ }
45
+ var context = {
46
+ _next: _moveNext,
47
+ ctx: {
48
+ core: function () {
49
+ return core;
50
+ },
51
+ diagLog: function () {
52
+ return safeGetLogger(core, config);
53
+ },
54
+ getCfg: function () {
55
+ return config;
56
+ },
57
+ getExtCfg: _getExtCfg,
58
+ getConfig: _getConfig,
59
+ hasNext: function () {
60
+ return !!_nextProxy;
61
+ },
62
+ getNext: function () {
63
+ return _nextProxy;
64
+ },
65
+ setNext: function (nextPlugin) {
66
+ _nextProxy = nextPlugin;
67
+ },
68
+ iterate: _iterateChain,
69
+ onComplete: _addOnComplete
70
+ }
71
+ };
72
+ function _addOnComplete(onComplete, that) {
73
+ var args = [];
74
+ for (var _i = 2; _i < arguments.length; _i++) {
75
+ args[_i - 2] = arguments[_i];
76
+ }
77
+ if (onComplete) {
78
+ _onComplete.push({
79
+ func: onComplete,
80
+ self: !isUndefined(that) ? that : context.ctx,
81
+ args: args
82
+ });
83
+ }
84
+ }
85
+ function _moveNext() {
86
+ var nextProxy = _nextProxy;
87
+ // Automatically move to the next plugin
88
+ _nextProxy = nextProxy ? nextProxy.getNext() : null;
89
+ if (!nextProxy) {
90
+ var onComplete = _onComplete;
91
+ if (onComplete && onComplete.length > 0) {
92
+ arrForEach(onComplete, function (completeDetails) {
93
+ try {
94
+ completeDetails.func.call(completeDetails.self, completeDetails.args);
95
+ }
96
+ catch (e) {
97
+ _throwInternal(core.logger, 2 /* WARNING */, 73 /* PluginException */, "Unexpected Exception during onComplete - " + dumpObj(e));
98
+ }
99
+ });
100
+ _onComplete = [];
101
+ }
102
+ }
103
+ return nextProxy;
104
+ }
105
+ function _getExtCfg(identifier, defaultValue, mergeDefault) {
106
+ if (defaultValue === void 0) { defaultValue = {}; }
107
+ if (mergeDefault === void 0) { mergeDefault = 0 /* None */; }
108
+ var theConfig;
109
+ if (config) {
110
+ var extConfig = config.extensionConfig;
111
+ if (extConfig && identifier) {
112
+ theConfig = extConfig[identifier];
113
+ }
114
+ }
115
+ if (!theConfig) {
116
+ // Just use the defaults
117
+ theConfig = defaultValue;
118
+ }
119
+ else if (isObject(defaultValue)) {
120
+ if (mergeDefault !== 0 /* None */) {
121
+ // Merge the defaults and configured values
122
+ var newConfig_1 = objExtend(true, defaultValue, theConfig);
123
+ if (config && mergeDefault === 2 /* MergeDefaultFromRootOrDefault */) {
124
+ // Enumerate over the defaultValues and if not already populate attempt to
125
+ // find a value from the root config
126
+ objForEachKey(defaultValue, function (field) {
127
+ // for each unspecified field, set the default value
128
+ if (isNullOrUndefined(newConfig_1[field])) {
129
+ var cfgValue = config[field];
130
+ if (!isNullOrUndefined(cfgValue)) {
131
+ newConfig_1[field] = cfgValue;
132
+ }
133
+ }
134
+ });
28
135
  }
29
- lastProxy = newProxy;
30
136
  }
31
137
  }
138
+ return theConfig;
32
139
  }
33
- return proxies.length > 0 ? proxies[0] : null;
34
- }
35
- function _copyProxyChain(proxy, itemCtx, startAt) {
36
- var plugins = [];
37
- var add = startAt ? false : true;
38
- if (proxy) {
39
- while (proxy) {
40
- var thePlugin = proxy.getPlugin();
41
- if (add || thePlugin === startAt) {
42
- add = true;
43
- plugins.push(thePlugin);
140
+ function _getConfig(identifier, field, defaultValue) {
141
+ if (defaultValue === void 0) { defaultValue = false; }
142
+ var theValue;
143
+ var extConfig = _getExtCfg(identifier, null);
144
+ if (extConfig && !isNullOrUndefined(extConfig[field])) {
145
+ theValue = extConfig[field];
146
+ }
147
+ else if (config && !isNullOrUndefined(config[field])) {
148
+ theValue = config[field];
149
+ }
150
+ return !isNullOrUndefined(theValue) ? theValue : defaultValue;
151
+ }
152
+ function _iterateChain(cb) {
153
+ // Keep processing until we reach the end of the chain
154
+ var nextPlugin;
155
+ while (!!(nextPlugin = context._next())) {
156
+ var plugin = nextPlugin.getPlugin();
157
+ if (plugin) {
158
+ // callback with the current on
159
+ cb(plugin);
44
160
  }
45
- proxy = proxy.getNext();
46
161
  }
47
162
  }
48
- if (!add) {
49
- plugins.push(startAt);
163
+ return context;
164
+ }
165
+ /**
166
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain
167
+ * @param plugins - The plugin instances that will be executed
168
+ * @param config - The current config
169
+ * @param core - The current core instance
170
+ * @param startAt - Identifies the next plugin to execute, if null there is no "next" plugin and if undefined it should assume the start of the chain
171
+ */
172
+ export function createProcessTelemetryContext(telemetryChain, config, core, startAt) {
173
+ var internalContext = _createInternalContext(telemetryChain, config, core, startAt);
174
+ var context = internalContext.ctx;
175
+ function _processNext(env) {
176
+ var nextPlugin = internalContext._next();
177
+ // Run the next plugin which will call "processNext()"
178
+ nextPlugin && nextPlugin.processTelemetry(env, context);
179
+ return !nextPlugin;
50
180
  }
51
- return _createProxyChain(plugins, itemCtx);
181
+ function _createNew(plugins, startAt) {
182
+ if (plugins === void 0) { plugins = null; }
183
+ if (isArray(plugins)) {
184
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
185
+ }
186
+ return createProcessTelemetryContext(plugins || context.getNext(), config, core, startAt);
187
+ }
188
+ context.processNext = _processNext;
189
+ context.createNew = _createNew;
190
+ return context;
52
191
  }
53
- function _copyPluginChain(srcPlugins, itemCtx, startAt) {
54
- var plugins = srcPlugins;
55
- var add = false;
56
- if (startAt && srcPlugins) {
57
- plugins = [];
58
- arrForEach(srcPlugins, function (thePlugin) {
59
- if (add || thePlugin === startAt) {
60
- add = true;
61
- plugins.push(thePlugin);
192
+ /**
193
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain for handling the unloading of the chain
194
+ * @param plugins - The plugin instances that will be executed
195
+ * @param config - The current config
196
+ * @param core - The current core instance
197
+ * @param startAt - Identifies the next plugin to execute, if null there is no "next" plugin and if undefined it should assume the start of the chain
198
+ */
199
+ export function createProcessTelemetryUnloadContext(telemetryChain, config, core, startAt) {
200
+ var internalContext = _createInternalContext(telemetryChain, config, core, startAt);
201
+ var context = internalContext.ctx;
202
+ function _processNext(unloadState) {
203
+ var nextPlugin = internalContext._next();
204
+ nextPlugin && nextPlugin.unload(context, unloadState);
205
+ return !nextPlugin;
206
+ }
207
+ function _createNew(plugins, startAt) {
208
+ if (plugins === void 0) { plugins = null; }
209
+ if (isArray(plugins)) {
210
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
211
+ }
212
+ return createProcessTelemetryUnloadContext(plugins || context.getNext(), config, core, startAt);
213
+ }
214
+ context.processNext = _processNext;
215
+ context.createNew = _createNew;
216
+ return context;
217
+ }
218
+ /**
219
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain for updating the configuration
220
+ * @param plugins - The plugin instances that will be executed
221
+ * @param config - The current config
222
+ * @param core - The current core instance
223
+ * @param startAt - Identifies the next plugin to execute, if null there is no "next" plugin and if undefined it should assume the start of the chain
224
+ */
225
+ export function createProcessTelemetryUpdateContext(telemetryChain, config, core, startAt) {
226
+ var internalContext = _createInternalContext(telemetryChain, config, core, startAt);
227
+ var context = internalContext.ctx;
228
+ function _processNext(updateState) {
229
+ return context.iterate(function (plugin) {
230
+ if (isFunction(plugin[strUpdate])) {
231
+ plugin[strUpdate](context, updateState);
62
232
  }
63
233
  });
64
234
  }
65
- if (startAt && !add) {
66
- if (!plugins) {
67
- plugins = [];
235
+ function _createNew(plugins, startAt) {
236
+ if (plugins === void 0) { plugins = null; }
237
+ if (isArray(plugins)) {
238
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
68
239
  }
69
- plugins.push(startAt);
240
+ return createProcessTelemetryUpdateContext(plugins || context.getNext(), config, core, startAt);
70
241
  }
71
- return _createProxyChain(plugins, itemCtx);
242
+ context.processNext = _processNext;
243
+ context.createNew = _createNew;
244
+ return context;
72
245
  }
73
- var ProcessTelemetryContext = /** @class */ (function () {
74
- /**
75
- * Creates a new Telemetry Item context with the current config, core and plugin execution chain
76
- * @param plugins - The plugin instances that will be executed
77
- * @param config - The current config
78
- * @param core - The current core instance
79
- */
80
- function ProcessTelemetryContext(plugins, config, core, startAt) {
81
- var _self = this;
82
- var _nextProxy = null; // Null == No next plugin
83
- // There is no next element (null) vs not defined (undefined)
84
- if (startAt !== null) {
85
- if (plugins && isFunction(plugins.getPlugin)) {
86
- // We have a proxy chain object
87
- _nextProxy = _copyProxyChain(plugins, _self, startAt || plugins.getPlugin());
246
+ /**
247
+ * Creates an execution chain from the array of plugins
248
+ * @param plugins - The array of plugins that will be executed in this order
249
+ * @param defItemCtx - The default execution context to use when no telemetry context is passed to processTelemetry(), this
250
+ * should be for legacy plugins only. Currently, only used for passing the current core instance and to provide better error
251
+ * reporting (hasRun) when errors occur.
252
+ */
253
+ export function createTelemetryProxyChain(plugins, config, core, startAt) {
254
+ var firstProxy = null;
255
+ var add = startAt ? false : true;
256
+ if (isArray(plugins) && plugins.length > 0) {
257
+ // Create the proxies and wire up the next plugin chain
258
+ var lastProxy_1 = null;
259
+ arrForEach(plugins, function (thePlugin) {
260
+ if (!add && startAt === thePlugin) {
261
+ add = true;
88
262
  }
89
- else {
90
- // We just have an array
91
- if (startAt) {
92
- _nextProxy = _copyPluginChain(plugins, _self, startAt);
263
+ if (add && thePlugin && isFunction(thePlugin.processTelemetry)) {
264
+ // Only add plugins that are processors
265
+ var newProxy = createTelemetryPluginProxy(thePlugin, config, core);
266
+ if (!firstProxy) {
267
+ firstProxy = newProxy;
93
268
  }
94
- else if (isUndefined(startAt)) {
95
- // Undefined means copy the existing chain
96
- _nextProxy = _createProxyChain(plugins, _self);
269
+ if (lastProxy_1) {
270
+ // Set this new proxy as the next for the previous one
271
+ lastProxy_1._setNext(newProxy);
97
272
  }
273
+ lastProxy_1 = newProxy;
98
274
  }
275
+ });
276
+ }
277
+ if (startAt && !firstProxy) {
278
+ // Special case where the "startAt" was not in the original list of plugins
279
+ return createTelemetryProxyChain([startAt], config, core);
280
+ }
281
+ return firstProxy;
282
+ }
283
+ /**
284
+ * Create the processing telemetry proxy instance, the proxy is used to abstract the current plugin to allow monitoring and
285
+ * execution plugins while passing around the dynamic execution state (IProcessTelemetryContext), the proxy instance no longer
286
+ * contains any execution state and can be reused between requests (this was not the case for 2.7.2 and earlier with the
287
+ * TelemetryPluginChain class).
288
+ * @param plugin - The plugin instance to proxy
289
+ * @param config - The default execution context to use when no telemetry context is passed to processTelemetry(), this
290
+ * should be for legacy plugins only. Currently, only used for passing the current core instance and to provide better error
291
+ * reporting (hasRun) when errors occur.
292
+ * @returns
293
+ */
294
+ export function createTelemetryPluginProxy(plugin, config, core) {
295
+ var nextProxy = null;
296
+ var hasProcessTelemetry = isFunction(plugin.processTelemetry);
297
+ var hasSetNext = isFunction(plugin.setNextPlugin);
298
+ var chainId;
299
+ if (plugin) {
300
+ chainId = plugin.identifier + "-" + plugin.priority + "-" + _chainId++;
301
+ }
302
+ else {
303
+ chainId = "Unknown-0-" + _chainId++;
304
+ }
305
+ var proxyChain = {
306
+ getPlugin: function () {
307
+ return plugin;
308
+ },
309
+ getNext: function () {
310
+ return nextProxy;
311
+ },
312
+ processTelemetry: _processTelemetry,
313
+ unload: _unloadPlugin,
314
+ update: _updatePlugin,
315
+ _id: chainId,
316
+ _setNext: function (nextPlugin) {
317
+ nextProxy = nextPlugin;
99
318
  }
100
- _self.core = function () {
101
- return core;
102
- };
103
- _self.diagLog = function () {
104
- return safeGetLogger(core, config);
105
- };
106
- _self.getCfg = function () {
107
- return config;
108
- };
109
- _self.getExtCfg = function (identifier, defaultValue) {
110
- if (defaultValue === void 0) { defaultValue = {}; }
111
- var theConfig;
112
- if (config) {
113
- var extConfig = config.extensionConfig;
114
- if (extConfig && identifier) {
115
- theConfig = extConfig[identifier];
319
+ };
320
+ function _getTelCtx() {
321
+ var itemCtx;
322
+ // Looks like a plugin didn't pass the (optional) context, so create a new one
323
+ if (plugin && isFunction(plugin[strGetTelCtx])) {
324
+ // This plugin extends from the BaseTelemetryPlugin so lets use it
325
+ itemCtx = plugin[strGetTelCtx]();
326
+ }
327
+ if (!itemCtx) {
328
+ // Create a temporary one
329
+ itemCtx = createProcessTelemetryContext(proxyChain, config, core);
330
+ }
331
+ return itemCtx;
332
+ }
333
+ function _processChain(itemCtx, processPluginFn, name, details, isAsync) {
334
+ var hasRun = false;
335
+ var identifier = plugin ? plugin.identifier : strTelemetryPluginChain;
336
+ var hasRunContext = itemCtx[strHasRunFlags];
337
+ if (!hasRunContext) {
338
+ // Assign and populate
339
+ hasRunContext = itemCtx[strHasRunFlags] = {};
340
+ }
341
+ // Ensure that we keep the context in sync
342
+ itemCtx.setNext(nextProxy);
343
+ if (plugin) {
344
+ doPerf(itemCtx[strCore](), function () { return identifier + ":" + name; }, function () {
345
+ // Mark this component as having run
346
+ hasRunContext[chainId] = true;
347
+ try {
348
+ // Set a flag on the next plugin so we know if it was attempted to be executed
349
+ var nextId = nextProxy ? nextProxy._id : strEmpty;
350
+ if (nextId) {
351
+ hasRunContext[nextId] = false;
352
+ }
353
+ hasRun = processPluginFn(itemCtx);
354
+ }
355
+ catch (error) {
356
+ var hasNextRun = nextProxy ? hasRunContext[nextProxy._id] : true;
357
+ if (hasNextRun) {
358
+ // The next plugin after us has already run so set this one as complete
359
+ hasRun = true;
360
+ }
361
+ if (!nextProxy || !hasNextRun) {
362
+ // Either we have no next plugin or the current one did not attempt to call the next plugin
363
+ // Which means the current one is the root of the failure so log/report this failure
364
+ _throwInternal(itemCtx.diagLog(), 1 /* CRITICAL */, 73 /* PluginException */, "Plugin [" + plugin.identifier + "] failed during " + name + " - " + dumpObj(error) + ", run flags: " + dumpObj(hasRunContext));
365
+ }
116
366
  }
367
+ }, details, isAsync);
368
+ }
369
+ return hasRun;
370
+ }
371
+ function _processTelemetry(env, itemCtx) {
372
+ itemCtx = itemCtx || _getTelCtx();
373
+ function _callProcessTelemetry(itemCtx) {
374
+ if (!plugin || !hasProcessTelemetry) {
375
+ return false;
117
376
  }
118
- return (theConfig ? theConfig : defaultValue);
119
- };
120
- _self.getConfig = function (identifier, field, defaultValue) {
121
- if (defaultValue === void 0) { defaultValue = false; }
122
- var theValue;
123
- var extConfig = _self.getExtCfg(identifier, null);
124
- if (extConfig && !isNullOrUndefined(extConfig[field])) {
125
- theValue = extConfig[field];
377
+ var pluginState = _getPluginState(plugin);
378
+ if (pluginState[strTeardown] || pluginState[strDisabled]) {
379
+ return false;
126
380
  }
127
- else if (config && !isNullOrUndefined(config[field])) {
128
- theValue = config[field];
381
+ // Ensure that we keep the context in sync (for processNext()), just in case a plugin
382
+ // doesn't calls processTelemetry() instead of itemContext.processNext() or some
383
+ // other form of error occurred
384
+ if (hasSetNext) {
385
+ // Backward compatibility setting the next plugin on the instance
386
+ plugin.setNextPlugin(nextProxy);
129
387
  }
130
- return !isNullOrUndefined(theValue) ? theValue : defaultValue;
131
- };
132
- _self.hasNext = function () {
133
- return _nextProxy != null;
134
- };
135
- _self.getNext = function () {
136
- return _nextProxy;
137
- };
138
- _self.setNext = function (nextPlugin) {
139
- _nextProxy = nextPlugin;
140
- };
141
- _self.processNext = function (env) {
142
- var nextPlugin = _nextProxy;
143
- if (nextPlugin) {
144
- // Automatically move to the next plugin
145
- _nextProxy = nextPlugin.getNext();
146
- nextPlugin.processTelemetry(env, _self);
388
+ plugin.processTelemetry(env, itemCtx);
389
+ // Process Telemetry is expected to call itemCtx.processNext() or nextPlugin.processTelemetry()
390
+ return true;
391
+ }
392
+ if (!_processChain(itemCtx, _callProcessTelemetry, "processTelemetry", function () { return ({ item: env }); }, !(env.sync))) {
393
+ // The underlying plugin is either not defined, not enabled or does not have a processTelemetry implementation
394
+ // so we still want the next plugin to be executed.
395
+ itemCtx.processNext(env);
396
+ }
397
+ }
398
+ function _unloadPlugin(unloadCtx, unloadState) {
399
+ function _callTeardown() {
400
+ // Setting default of hasRun as false so the proxyProcessFn() is called as teardown() doesn't have to exist or call unloadNext().
401
+ var hasRun = false;
402
+ if (plugin) {
403
+ var pluginState = _getPluginState(plugin);
404
+ var pluginCore = plugin[strCore] || pluginState.core;
405
+ // Only teardown the plugin if it was initialized by the current core (i.e. It's not a shared plugin)
406
+ if (plugin && (!pluginCore || pluginCore === unloadCtx[strCore]()) && !pluginState[strTeardown]) {
407
+ // Handle plugins that don't extend from the BaseTelemetryPlugin
408
+ pluginState[strCore] = null;
409
+ pluginState[strTeardown] = true;
410
+ pluginState[strIsInitialized] = false;
411
+ if (plugin[strTeardown] && plugin[strTeardown](unloadCtx, unloadState) === true) {
412
+ // plugin told us that it was going to (or has) call unloadCtx.processNext()
413
+ hasRun = true;
414
+ }
415
+ }
147
416
  }
148
- };
149
- _self.createNew = function (plugins, startAt) {
150
- if (plugins === void 0) { plugins = null; }
151
- return new ProcessTelemetryContext(plugins || _nextProxy, config, core, startAt);
152
- };
417
+ return hasRun;
418
+ }
419
+ if (!_processChain(unloadCtx, _callTeardown, "unload", function () { }, unloadState.isAsync)) {
420
+ // Only called if we hasRun was not true
421
+ unloadCtx.processNext(unloadState);
422
+ }
423
+ }
424
+ function _updatePlugin(updateCtx, updateState) {
425
+ function _callUpdate() {
426
+ // Setting default of hasRun as false so the proxyProcessFn() is called as teardown() doesn't have to exist or call unloadNext().
427
+ var hasRun = false;
428
+ if (plugin) {
429
+ var pluginState = _getPluginState(plugin);
430
+ var pluginCore = plugin[strCore] || pluginState.core;
431
+ // Only update the plugin if it was initialized by the current core (i.e. It's not a shared plugin)
432
+ if (plugin && (!pluginCore || pluginCore === updateCtx[strCore]()) && !pluginState[strTeardown]) {
433
+ if (plugin[strUpdate] && plugin[strUpdate](updateCtx, updateState) === true) {
434
+ // plugin told us that it was going to (or has) call unloadCtx.processNext()
435
+ hasRun = true;
436
+ }
437
+ }
438
+ }
439
+ return hasRun;
440
+ }
441
+ if (!_processChain(updateCtx, _callUpdate, "update", function () { }, false)) {
442
+ // Only called if we hasRun was not true
443
+ updateCtx.processNext(updateState);
444
+ }
445
+ }
446
+ return objFreeze(proxyChain);
447
+ }
448
+ /**
449
+ * This class will be removed!
450
+ * @deprecated use createProcessTelemetryContext() instead
451
+ */
452
+ var ProcessTelemetryContext = /** @class */ (function () {
453
+ /**
454
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain
455
+ * @param plugins - The plugin instances that will be executed
456
+ * @param config - The current config
457
+ * @param core - The current core instance
458
+ */
459
+ function ProcessTelemetryContext(pluginChain, config, core, startAt) {
460
+ var _self = this;
461
+ var context = createProcessTelemetryContext(pluginChain, config, core, startAt);
462
+ // Proxy all functions of the context to this object
463
+ proxyFunctions(_self, context, objKeys(context));
153
464
  }
154
465
  return ProcessTelemetryContext;
155
466
  }());