@microsoft/applicationinsights-core-js 2.8.0-nightly.2202-06 → 2.8.0-nightly.2204-04

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 +16363 -9167
  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
@@ -3,90 +3,557 @@
3
3
  "use strict";
4
4
 
5
5
  import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore";
6
- import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
7
6
  import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
8
7
  import { ITelemetryItem } from "../JavaScriptSDK.Interfaces/ITelemetryItem";
9
8
  import { IPlugin, ITelemetryPlugin } from "../JavaScriptSDK.Interfaces/ITelemetryPlugin";
10
- import { IProcessTelemetryContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
9
+ import { GetExtCfgMergeType, IBaseProcessingContext, IProcessTelemetryContext, IProcessTelemetryUnloadContext, IProcessTelemetryUpdateContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
11
10
  import { ITelemetryPluginChain } from "../JavaScriptSDK.Interfaces/ITelemetryPluginChain";
12
- import { safeGetLogger } from "./DiagnosticLogger";
13
- import { TelemetryPluginChain } from "./TelemetryPluginChain";
14
- import { arrForEach, isFunction, isNullOrUndefined, isUndefined } from "./HelperFuncs";
11
+ import { safeGetLogger, _throwInternal } from "./DiagnosticLogger";
12
+ import { arrForEach, isArray, isFunction, isNullOrUndefined, isObject, isUndefined, objExtend, objForEachKey, objFreeze, objKeys, proxyFunctions } from "./HelperFuncs";
13
+ import { doPerf } from "./PerfManager";
14
+ import { eLoggingSeverity, _eInternalMessageId } from "../JavaScriptSDK.Enums/LoggingEnums";
15
+ import { dumpObj } from "./EnvUtils";
16
+ import { strCore, strDisabled, strEmpty, strIsInitialized, strTeardown, strUpdate } from "./InternalConstants";
17
+ import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
18
+ import { ITelemetryUnloadState } from "../JavaScriptSDK.Interfaces/ITelemetryUnloadState";
19
+ import { ITelemetryUpdateState } from "../JavaScriptSDK.Interfaces/ITelemetryUpdateState";
20
+ import { _getPluginState } from "./TelemetryHelpers";
21
+
22
+ const strTelemetryPluginChain = "TelemetryPluginChain";
23
+ const strHasRunFlags = "_hasRun";
24
+ const strGetTelCtx = "_getTelCtx";
25
+
26
+ let _chainId = 0;
27
+
28
+ interface OnCompleteCallback {
29
+ func: () => void;
30
+ self: any; // This for the function
31
+ args: any[]; // Additional arguments for the function
32
+ }
33
+
34
+ interface IInternalTelemetryPluginChain extends ITelemetryPluginChain {
35
+ _id: string;
36
+ _setNext: (nextPlugin: IInternalTelemetryPluginChain) => void;
37
+ }
38
+
39
+ interface IInternalContext<T extends IBaseProcessingContext> {
40
+ _next: () => ITelemetryPluginChain,
41
+
42
+ // The public context that will be exposed
43
+ ctx: T
44
+ }
45
+
46
+ function _getNextProxyStart(proxy: ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt: IPlugin): ITelemetryPluginChain {
47
+ while (proxy) {
48
+ if (proxy.getPlugin() === startAt) {
49
+ return proxy;
50
+ }
51
+
52
+ proxy = proxy.getNext();
53
+ }
54
+
55
+ // This wasn't found in the existing chain so create an isolated one with just this plugin
56
+ return createTelemetryProxyChain([startAt], config, core);
57
+ }
15
58
 
16
59
  /**
17
- * Creates the instance execution chain for the plugins
60
+ * @ignore
61
+ * @param telemetryChain
62
+ * @param config
63
+ * @param core
64
+ * @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
65
+ * @returns
18
66
  */
19
- function _createProxyChain(plugins:IPlugin[], itemCtx:IProcessTelemetryContext) {
20
- let proxies:ITelemetryPluginChain[] = [];
21
-
22
- if (plugins && plugins.length > 0) {
67
+ function _createInternalContext<T extends IBaseProcessingContext>(telemetryChain: ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt?: IPlugin): IInternalContext<T> {
68
+ // We have a special case where we want to start execution from this specific plugin
69
+ // or we simply reuse the existing telemetry plugin chain (normal execution case)
70
+ let _nextProxy: ITelemetryPluginChain | null = null; // By Default set as no next plugin
71
+ let _onComplete: OnCompleteCallback[] = [];
72
+
73
+ if (startAt !== null) {
74
+ // There is no next element (null) vs not defined (undefined) so use the full chain
75
+ _nextProxy = startAt ? _getNextProxyStart(telemetryChain, config, core, startAt) : telemetryChain;
76
+ }
77
+
78
+ let context: IInternalContext<T> = {
79
+ _next: _moveNext,
80
+ ctx: {
81
+ core: () => {
82
+ return core
83
+ },
84
+ diagLog: () => {
85
+ return safeGetLogger(core, config);
86
+ },
87
+ getCfg: () => {
88
+ return config;
89
+ },
90
+ getExtCfg: _getExtCfg,
91
+ getConfig: _getConfig,
92
+ hasNext: () => {
93
+ return !!_nextProxy;
94
+ },
95
+ getNext: () => {
96
+ return _nextProxy;
97
+ },
98
+ setNext: (nextPlugin:ITelemetryPluginChain) => {
99
+ _nextProxy = nextPlugin;
100
+ },
101
+ iterate: _iterateChain,
102
+ onComplete: _addOnComplete
103
+ } as T
104
+ };
105
+
106
+ function _addOnComplete(onComplete: () => void, that?: any, ...args: any[]) {
107
+ if (onComplete) {
108
+ _onComplete.push({
109
+ func: onComplete,
110
+ self: !isUndefined(that) ? that : context.ctx,
111
+ args: args
112
+ });
113
+ }
114
+ }
115
+
116
+ function _moveNext() {
117
+ let nextProxy = _nextProxy;
118
+
119
+ // Automatically move to the next plugin
120
+ _nextProxy = nextProxy ? nextProxy.getNext() : null;
121
+
122
+ if (!nextProxy) {
123
+ let onComplete = _onComplete;
124
+ if (onComplete && onComplete.length > 0) {
125
+ arrForEach(onComplete, (completeDetails) => {
126
+ try {
127
+ completeDetails.func.call(completeDetails.self, completeDetails.args);
128
+ } catch (e) {
129
+ _throwInternal(
130
+ core.logger,
131
+ eLoggingSeverity.WARNING,
132
+ _eInternalMessageId.PluginException,
133
+ "Unexpected Exception during onComplete - " + dumpObj(e));
134
+ }
135
+ });
136
+
137
+ _onComplete = [];
138
+ }
139
+ }
140
+
141
+ return nextProxy;
142
+ }
143
+
144
+ function _getExtCfg<T>(identifier: string, defaultValue: T|any = {}, mergeDefault: GetExtCfgMergeType = GetExtCfgMergeType.None) {
145
+ let theConfig: T;
146
+ if (config) {
147
+ let extConfig = config.extensionConfig;
148
+ if (extConfig && identifier) {
149
+ theConfig = extConfig[identifier];
150
+ }
151
+ }
152
+
153
+ if (!theConfig) {
154
+ // Just use the defaults
155
+ theConfig = defaultValue as T;
156
+ } else if (isObject(defaultValue)) {
157
+ if (mergeDefault !== GetExtCfgMergeType.None) {
158
+ // Merge the defaults and configured values
159
+ let newConfig = objExtend(true, defaultValue, theConfig);
160
+
161
+ if (config && mergeDefault === GetExtCfgMergeType.MergeDefaultFromRootOrDefault) {
162
+ // Enumerate over the defaultValues and if not already populate attempt to
163
+ // find a value from the root config
164
+ objForEachKey(defaultValue, (field) => {
165
+ // for each unspecified field, set the default value
166
+ if (isNullOrUndefined(newConfig[field])) {
167
+ let cfgValue = config[field];
168
+ if (!isNullOrUndefined(cfgValue)) {
169
+ newConfig[field] = cfgValue;
170
+ }
171
+ }
172
+ });
173
+ }
174
+ }
175
+ }
176
+
177
+ return theConfig;
178
+ }
179
+
180
+ function _getConfig(identifier:string, field: string, defaultValue: number | string | boolean | string[] | RegExp[] | Function = false) {
181
+ let theValue;
182
+ let extConfig = _getExtCfg(identifier, null);
183
+ if (extConfig && !isNullOrUndefined(extConfig[field])) {
184
+ theValue = extConfig[field];
185
+ } else if (config && !isNullOrUndefined(config[field])) {
186
+ theValue = config[field];
187
+ }
188
+
189
+ return !isNullOrUndefined(theValue) ? theValue : defaultValue;
190
+ }
191
+
192
+ function _iterateChain<T extends ITelemetryPlugin = ITelemetryPlugin>(cb: (plugin: T) => void) {
193
+ // Keep processing until we reach the end of the chain
194
+ let nextPlugin: ITelemetryPluginChain;
195
+ while(!!(nextPlugin = context._next())) {
196
+ let plugin = nextPlugin.getPlugin();
197
+ if (plugin) {
198
+ // callback with the current on
199
+ cb(plugin as T);
200
+ }
201
+ }
202
+ }
203
+
204
+ return context;
205
+ }
206
+
207
+ /**
208
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain
209
+ * @param plugins - The plugin instances that will be executed
210
+ * @param config - The current config
211
+ * @param core - The current core instance
212
+ * @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
213
+ */
214
+ export function createProcessTelemetryContext(telemetryChain: ITelemetryPluginChain | null, config: IConfiguration, core:IAppInsightsCore, startAt?: IPlugin): IProcessTelemetryContext {
215
+ let internalContext: IInternalContext<IProcessTelemetryContext> = _createInternalContext<IProcessTelemetryContext>(telemetryChain, config, core, startAt);
216
+ let context = internalContext.ctx;
217
+
218
+ function _processNext(env: ITelemetryItem) {
219
+ let nextPlugin: ITelemetryPluginChain = internalContext._next();
220
+ // Run the next plugin which will call "processNext()"
221
+ nextPlugin && nextPlugin.processTelemetry(env, context);
222
+
223
+ return !nextPlugin;
224
+ }
225
+
226
+ function _createNew(plugins: IPlugin[] | ITelemetryPluginChain | null = null, startAt?: IPlugin) {
227
+ if (isArray(plugins)) {
228
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
229
+ }
230
+
231
+ return createProcessTelemetryContext(plugins || context.getNext(), config, core, startAt);
232
+ }
233
+
234
+ context.processNext = _processNext;
235
+ context.createNew = _createNew;
236
+
237
+ return context;
238
+ }
239
+
240
+ /**
241
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain for handling the unloading of the chain
242
+ * @param plugins - The plugin instances that will be executed
243
+ * @param config - The current config
244
+ * @param core - The current core instance
245
+ * @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
246
+ */
247
+ export function createProcessTelemetryUnloadContext(telemetryChain: ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt?: IPlugin): IProcessTelemetryUnloadContext {
248
+ let internalContext: IInternalContext<IProcessTelemetryUnloadContext> = _createInternalContext<IProcessTelemetryUnloadContext>(telemetryChain, config, core, startAt);
249
+ let context = internalContext.ctx;
250
+
251
+ function _processNext(unloadState: ITelemetryUnloadState) {
252
+ let nextPlugin: ITelemetryPluginChain = internalContext._next();
253
+ nextPlugin && nextPlugin.unload(context, unloadState);
254
+
255
+ return !nextPlugin;
256
+ }
257
+
258
+ function _createNew(plugins: IPlugin[] | ITelemetryPluginChain = null, startAt?: IPlugin): IProcessTelemetryUnloadContext {
259
+ if (isArray(plugins)) {
260
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
261
+ }
262
+
263
+ return createProcessTelemetryUnloadContext(plugins || context.getNext(), config, core, startAt);
264
+ }
265
+
266
+ context.processNext = _processNext;
267
+ context.createNew = _createNew
268
+
269
+ return context;
270
+ }
271
+
272
+ /**
273
+ * Creates a new Telemetry Item context with the current config, core and plugin execution chain for updating the configuration
274
+ * @param plugins - The plugin instances that will be executed
275
+ * @param config - The current config
276
+ * @param core - The current core instance
277
+ * @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
278
+ */
279
+ export function createProcessTelemetryUpdateContext(telemetryChain: ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt?: IPlugin): IProcessTelemetryUpdateContext {
280
+ let internalContext: IInternalContext<IProcessTelemetryUpdateContext> = _createInternalContext<IProcessTelemetryUpdateContext>(telemetryChain, config, core, startAt);
281
+ let context = internalContext.ctx;
282
+
283
+ function _processNext(updateState: ITelemetryUpdateState) {
284
+ return context.iterate((plugin) => {
285
+ if (isFunction(plugin[strUpdate])) {
286
+ plugin[strUpdate](context, updateState);
287
+ }
288
+ });
289
+ }
290
+
291
+ function _createNew(plugins: IPlugin[] | ITelemetryPluginChain = null, startAt?: IPlugin) {
292
+ if (isArray(plugins)) {
293
+ plugins = createTelemetryProxyChain(plugins, config, core, startAt);
294
+ }
295
+
296
+ return createProcessTelemetryUpdateContext(plugins || context.getNext(), config, core, startAt);
297
+ }
298
+
299
+ context.processNext = _processNext;
300
+ context.createNew = _createNew;
301
+
302
+ return context;
303
+ }
304
+
305
+ /**
306
+ * Creates an execution chain from the array of plugins
307
+ * @param plugins - The array of plugins that will be executed in this order
308
+ * @param defItemCtx - The default execution context to use when no telemetry context is passed to processTelemetry(), this
309
+ * should be for legacy plugins only. Currently, only used for passing the current core instance and to provide better error
310
+ * reporting (hasRun) when errors occur.
311
+ */
312
+ export function createTelemetryProxyChain(plugins: IPlugin[], config: IConfiguration, core: IAppInsightsCore, startAt?: IPlugin): ITelemetryPluginChain {
313
+ let firstProxy: ITelemetryPluginChain = null;
314
+ let add = startAt ? false : true;
315
+
316
+ if (isArray(plugins) && plugins.length > 0) {
23
317
  // Create the proxies and wire up the next plugin chain
24
- let lastProxy:TelemetryPluginChain = null;
25
- for (let idx = 0; idx < plugins.length; idx++) {
26
- let thePlugin = plugins[idx] as ITelemetryPlugin;
27
- if (thePlugin && isFunction(thePlugin.processTelemetry)) {
318
+ let lastProxy: IInternalTelemetryPluginChain = null;
319
+ arrForEach(plugins, (thePlugin: ITelemetryPlugin) => {
320
+ if (!add && startAt === thePlugin) {
321
+ add = true;
322
+ }
323
+
324
+ if (add && thePlugin && isFunction(thePlugin.processTelemetry)) {
28
325
  // Only add plugins that are processors
29
- let newProxy = new TelemetryPluginChain(thePlugin, itemCtx);
30
- proxies.push(newProxy);
326
+ let newProxy = createTelemetryPluginProxy(thePlugin, config, core);
327
+ if (!firstProxy) {
328
+ firstProxy = newProxy;
329
+ }
330
+
31
331
  if (lastProxy) {
32
332
  // Set this new proxy as the next for the previous one
33
- lastProxy.setNext(newProxy);
333
+ lastProxy._setNext(newProxy as IInternalTelemetryPluginChain);
34
334
  }
35
335
 
36
- lastProxy = newProxy;
336
+ lastProxy = newProxy as IInternalTelemetryPluginChain;
37
337
  }
38
- }
338
+ });
39
339
  }
40
340
 
41
- return proxies.length > 0 ? proxies[0] : null;
341
+ if (startAt && !firstProxy) {
342
+ // Special case where the "startAt" was not in the original list of plugins
343
+ return createTelemetryProxyChain([startAt], config, core);
344
+ }
345
+
346
+ return firstProxy;
42
347
  }
43
348
 
44
- function _copyProxyChain(proxy:ITelemetryPluginChain, itemCtx:IProcessTelemetryContext, startAt:IPlugin) {
45
- let plugins:IPlugin[] = [];
46
- let add = startAt ? false : true;
349
+ /**
350
+ * Create the processing telemetry proxy instance, the proxy is used to abstract the current plugin to allow monitoring and
351
+ * execution plugins while passing around the dynamic execution state (IProcessTelemetryContext), the proxy instance no longer
352
+ * contains any execution state and can be reused between requests (this was not the case for 2.7.2 and earlier with the
353
+ * TelemetryPluginChain class).
354
+ * @param plugin - The plugin instance to proxy
355
+ * @param config - The default execution context to use when no telemetry context is passed to processTelemetry(), this
356
+ * should be for legacy plugins only. Currently, only used for passing the current core instance and to provide better error
357
+ * reporting (hasRun) when errors occur.
358
+ * @returns
359
+ */
360
+ export function createTelemetryPluginProxy(plugin: ITelemetryPlugin, config: IConfiguration, core: IAppInsightsCore): ITelemetryPluginChain {
361
+ let nextProxy: IInternalTelemetryPluginChain = null;
362
+ let hasProcessTelemetry = isFunction(plugin.processTelemetry);
363
+ let hasSetNext = isFunction(plugin.setNextPlugin);
364
+ let chainId: string;
365
+ if (plugin) {
366
+ chainId = plugin.identifier + "-" + plugin.priority + "-" + _chainId++;
367
+ } else {
368
+ chainId = "Unknown-0-" + _chainId++;
369
+ }
370
+ let proxyChain: IInternalTelemetryPluginChain = {
371
+ getPlugin: () => {
372
+ return plugin;
373
+ },
374
+ getNext: () => {
375
+ return nextProxy;
376
+ },
377
+ processTelemetry: _processTelemetry,
378
+ unload: _unloadPlugin,
379
+ update: _updatePlugin,
380
+ _id: chainId,
381
+ _setNext: (nextPlugin: IInternalTelemetryPluginChain) => {
382
+ nextProxy = nextPlugin;
383
+ }
384
+ };
47
385
 
48
- if (proxy) {
49
- while (proxy) {
50
- let thePlugin = proxy.getPlugin();
51
- if (add || thePlugin === startAt) {
52
- add = true;
53
- plugins.push(thePlugin);
54
- }
55
- proxy = proxy.getNext();
386
+ function _getTelCtx() {
387
+ let itemCtx: IProcessTelemetryContext;
388
+
389
+ // Looks like a plugin didn't pass the (optional) context, so create a new one
390
+ if (plugin && isFunction(plugin[strGetTelCtx])) {
391
+ // This plugin extends from the BaseTelemetryPlugin so lets use it
392
+ itemCtx = plugin[strGetTelCtx]();
393
+ }
394
+
395
+ if (!itemCtx) {
396
+ // Create a temporary one
397
+ itemCtx = createProcessTelemetryContext(proxyChain, config, core);
56
398
  }
399
+
400
+ return itemCtx;
57
401
  }
58
402
 
59
- if (!add) {
60
- plugins.push(startAt);
403
+ function _processChain<T extends IBaseProcessingContext>(
404
+ itemCtx: T,
405
+ processPluginFn: (itemCtx: T) => boolean,
406
+ name: string,
407
+ details: () => any,
408
+ isAsync: boolean) {
409
+
410
+ let hasRun = false;
411
+ let identifier = plugin ? plugin.identifier : strTelemetryPluginChain;
412
+ let hasRunContext = itemCtx[strHasRunFlags];
413
+ if (!hasRunContext) {
414
+ // Assign and populate
415
+ hasRunContext = itemCtx[strHasRunFlags] = {};
416
+ }
417
+
418
+ // Ensure that we keep the context in sync
419
+ itemCtx.setNext(nextProxy);
420
+
421
+ if (plugin) {
422
+ doPerf(itemCtx[strCore](), () => identifier + ":" + name, () => {
423
+ // Mark this component as having run
424
+ hasRunContext[chainId] = true;
425
+
426
+ try {
427
+ // Set a flag on the next plugin so we know if it was attempted to be executed
428
+ let nextId = nextProxy ? nextProxy._id : strEmpty;
429
+ if (nextId) {
430
+ hasRunContext[nextId] = false;
431
+ }
432
+
433
+ hasRun = processPluginFn(itemCtx);
434
+ } catch (error) {
435
+ let hasNextRun = nextProxy ? hasRunContext[nextProxy._id] : true;
436
+ if (hasNextRun) {
437
+ // The next plugin after us has already run so set this one as complete
438
+ hasRun = true;
439
+ }
440
+
441
+ if (!nextProxy || !hasNextRun) {
442
+
443
+ // Either we have no next plugin or the current one did not attempt to call the next plugin
444
+ // Which means the current one is the root of the failure so log/report this failure
445
+ _throwInternal(
446
+ itemCtx.diagLog(),
447
+ eLoggingSeverity.CRITICAL,
448
+ _eInternalMessageId.PluginException,
449
+ "Plugin [" + plugin.identifier + "] failed during " + name + " - " + dumpObj(error) + ", run flags: " + dumpObj(hasRunContext));
450
+ }
451
+ }
452
+ }, details, isAsync);
453
+ }
454
+
455
+ return hasRun;
61
456
  }
62
-
63
- return _createProxyChain(plugins, itemCtx);
64
- }
65
457
 
66
- function _copyPluginChain(srcPlugins:IPlugin[], itemCtx:IProcessTelemetryContext, startAt:IPlugin) {
67
- let plugins:IPlugin[] = srcPlugins;
68
- let add = false;
69
- if (startAt && srcPlugins) {
70
- plugins = [];
458
+ function _processTelemetry(env: ITelemetryItem, itemCtx: IProcessTelemetryContext) {
459
+ itemCtx = itemCtx || _getTelCtx();
460
+
461
+ function _callProcessTelemetry(itemCtx: IProcessTelemetryContext) {
462
+ if (!plugin || !hasProcessTelemetry) {
463
+ return false;
464
+ }
465
+
466
+ let pluginState = _getPluginState(plugin);
467
+ if (pluginState[strTeardown] || pluginState[strDisabled]) {
468
+ return false;
469
+ }
470
+
471
+ // Ensure that we keep the context in sync (for processNext()), just in case a plugin
472
+ // doesn't calls processTelemetry() instead of itemContext.processNext() or some
473
+ // other form of error occurred
474
+ if (hasSetNext) {
475
+ // Backward compatibility setting the next plugin on the instance
476
+ plugin.setNextPlugin(nextProxy);
477
+ }
478
+
479
+ plugin.processTelemetry(env, itemCtx);
480
+
481
+ // Process Telemetry is expected to call itemCtx.processNext() or nextPlugin.processTelemetry()
482
+ return true;
483
+ }
484
+
485
+ if (!_processChain(itemCtx, _callProcessTelemetry, "processTelemetry", () => ({ item: env }), !((env as any).sync))) {
486
+ // The underlying plugin is either not defined, not enabled or does not have a processTelemetry implementation
487
+ // so we still want the next plugin to be executed.
488
+ itemCtx.processNext(env);
489
+ }
490
+ }
491
+
492
+ function _unloadPlugin(unloadCtx: IProcessTelemetryUnloadContext, unloadState: ITelemetryUnloadState) {
493
+
494
+ function _callTeardown() {
495
+ // Setting default of hasRun as false so the proxyProcessFn() is called as teardown() doesn't have to exist or call unloadNext().
496
+ let hasRun = false;
497
+ if (plugin) {
498
+ let pluginState = _getPluginState(plugin);
499
+ let pluginCore = plugin[strCore] || pluginState.core;
500
+ // Only teardown the plugin if it was initialized by the current core (i.e. It's not a shared plugin)
501
+ if (plugin && (!pluginCore || pluginCore === unloadCtx[strCore]()) && !pluginState[strTeardown]) {
502
+ // Handle plugins that don't extend from the BaseTelemetryPlugin
503
+ pluginState[strCore] = null;
504
+ pluginState[strTeardown] = true;
505
+ pluginState[strIsInitialized] = false;
71
506
 
72
- arrForEach(srcPlugins, thePlugin => {
73
- if (add || thePlugin === startAt) {
74
- add = true;
75
- plugins.push(thePlugin);
507
+ if (plugin[strTeardown] && plugin[strTeardown](unloadCtx, unloadState) === true) {
508
+ // plugin told us that it was going to (or has) call unloadCtx.processNext()
509
+ hasRun = true;
510
+ }
511
+ }
76
512
  }
77
- });
513
+
514
+ return hasRun;
515
+ }
516
+
517
+ if (!_processChain(unloadCtx, _callTeardown, "unload", () => {}, unloadState.isAsync)) {
518
+ // Only called if we hasRun was not true
519
+ unloadCtx.processNext(unloadState);
520
+ }
78
521
  }
79
522
 
80
- if (startAt && !add) {
81
- if (!plugins) {
82
- plugins = [];
523
+ function _updatePlugin(updateCtx: IProcessTelemetryUpdateContext, updateState: ITelemetryUpdateState) {
524
+
525
+ function _callUpdate() {
526
+ // Setting default of hasRun as false so the proxyProcessFn() is called as teardown() doesn't have to exist or call unloadNext().
527
+ let hasRun = false;
528
+ if (plugin) {
529
+ let pluginState = _getPluginState(plugin);
530
+ let pluginCore = plugin[strCore] || pluginState.core;
531
+
532
+ // Only update the plugin if it was initialized by the current core (i.e. It's not a shared plugin)
533
+ if (plugin && (!pluginCore || pluginCore === updateCtx[strCore]()) && !pluginState[strTeardown]) {
534
+ if (plugin[strUpdate] && plugin[strUpdate](updateCtx, updateState) === true) {
535
+ // plugin told us that it was going to (or has) call unloadCtx.processNext()
536
+ hasRun = true;
537
+ }
538
+ }
539
+ }
540
+
541
+ return hasRun;
542
+ }
543
+
544
+ if (!_processChain(updateCtx, _callUpdate, "update", () => {}, false)) {
545
+ // Only called if we hasRun was not true
546
+ updateCtx.processNext(updateState);
83
547
  }
84
- plugins.push(startAt);
85
548
  }
86
-
87
- return _createProxyChain(plugins, itemCtx);
549
+
550
+ return objFreeze(proxyChain);
88
551
  }
89
552
 
553
+ /**
554
+ * This class will be removed!
555
+ * @deprecated use createProcessTelemetryContext() instead
556
+ */
90
557
  export class ProcessTelemetryContext implements IProcessTelemetryContext {
91
558
  /**
92
559
  * Gets the current core config instance
@@ -95,7 +562,7 @@ export class ProcessTelemetryContext implements IProcessTelemetryContext {
95
562
 
96
563
  public getExtCfg: <T>(identifier:string, defaultValue?:T|any) => T;
97
564
 
98
- public getConfig: (identifier:string, field: string, defaultValue?: number | string | boolean) => number | string | boolean;
565
+ public getConfig: (identifier:string, field: string, defaultValue?: number | string | boolean | string[] | RegExp[] | Function) => number | string | boolean | string[] | RegExp[] | Function;
99
566
 
100
567
  /**
101
568
  * Returns the IAppInsightsCore instance for the current request
@@ -129,100 +596,42 @@ export class ProcessTelemetryContext implements IProcessTelemetryContext {
129
596
  * @param itemCtx - This is the context for the current request, ITelemetryPlugin instances
130
597
  * can optionally use this to access the current core instance or define / pass additional information
131
598
  * to later plugins (vs appending items to the telemetry item)
599
+ * @returns boolean (true) if there is no more plugins to process otherwise false or undefined (void)
132
600
  */
133
- public processNext: (env: ITelemetryItem) => void;
601
+ public processNext: (env: ITelemetryItem) => boolean | void;
134
602
 
603
+ /**
604
+ * Synchronously iterate over the context chain running the callback for each plugin, once
605
+ * every plugin has been executed via the callback, any associated onComplete will be called.
606
+ * @param callback - The function call for each plugin in the context chain
607
+ */
608
+ public iterate: <T extends ITelemetryPlugin = ITelemetryPlugin>(callback: (plugin: T) => void) => void;
609
+
135
610
  /**
136
611
  * Create a new context using the core and config from the current instance
612
+ * @param plugins - The execution order to process the plugins, if null or not supplied
613
+ * then the current execution order will be copied.
614
+ * @param startAt - The plugin to start processing from, if missing from the execution
615
+ * order then the next plugin will be NOT set.
137
616
  */
138
617
  public createNew: (plugins?:IPlugin[]|ITelemetryPluginChain, startAt?:IPlugin) => IProcessTelemetryContext;
139
-
618
+
619
+ /**
620
+ * Set the function to call when the current chain has executed all processNext or unloadNext items.
621
+ */
622
+ public onComplete: (onComplete: () => void) => void;
623
+
140
624
  /**
141
625
  * Creates a new Telemetry Item context with the current config, core and plugin execution chain
142
626
  * @param plugins - The plugin instances that will be executed
143
627
  * @param config - The current config
144
628
  * @param core - The current core instance
145
629
  */
146
- constructor(plugins:IPlugin[]|ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt?:IPlugin) {
630
+ constructor(pluginChain: ITelemetryPluginChain, config: IConfiguration, core:IAppInsightsCore, startAt?:IPlugin) {
147
631
  let _self = this;
148
- let _nextProxy: ITelemetryPluginChain = null; // Null == No next plugin
149
-
150
- // There is no next element (null) vs not defined (undefined)
151
- if (startAt !== null) {
152
- if (plugins && isFunction((plugins as ITelemetryPluginChain).getPlugin)) {
153
- // We have a proxy chain object
154
- _nextProxy = _copyProxyChain(plugins as ITelemetryPluginChain, _self, startAt||(plugins as ITelemetryPluginChain).getPlugin());
155
- } else {
156
- // We just have an array
157
- if (startAt) {
158
- _nextProxy = _copyPluginChain(plugins as IPlugin[], _self, startAt);
159
- } else if (isUndefined(startAt)) {
160
- // Undefined means copy the existing chain
161
- _nextProxy = _createProxyChain(plugins as IPlugin[], _self)
162
- }
163
- }
164
- }
165
632
 
166
- _self.core = () => {
167
- return core;
168
- };
169
-
170
- _self.diagLog = () => {
171
- return safeGetLogger(core, config);
172
- };
173
-
174
- _self.getCfg = () => {
175
- return config;
176
- };
177
-
178
- _self.getExtCfg = <T>(identifier:string, defaultValue:T|any = {}) => {
179
- let theConfig:T;
180
- if (config) {
181
- let extConfig = config.extensionConfig;
182
- if (extConfig && identifier) {
183
- theConfig = extConfig[identifier];
184
- }
185
- }
186
-
187
- return (theConfig ? theConfig : defaultValue) as T;
188
- };
189
-
190
- _self.getConfig = (identifier:string, field: string, defaultValue: number | string | boolean = false) => {
191
- let theValue;
192
- let extConfig = _self.getExtCfg(identifier, null);
193
- if (extConfig && !isNullOrUndefined(extConfig[field])) {
194
- theValue = extConfig[field];
195
- } else if (config && !isNullOrUndefined(config[field])) {
196
- theValue = config[field];
197
- }
198
-
199
- return !isNullOrUndefined(theValue) ? theValue : defaultValue;
200
- };
201
-
202
- _self.hasNext = () => {
203
- return _nextProxy != null;
204
- };
205
-
206
- _self.getNext = () => {
207
- return _nextProxy;
208
- }
209
-
210
- _self.setNext = (nextPlugin:ITelemetryPluginChain) => {
211
- _nextProxy = nextPlugin;
212
- };
213
-
214
- _self.processNext = (env: ITelemetryItem) => {
215
- let nextPlugin = _nextProxy;
216
-
217
- if (nextPlugin) {
218
- // Automatically move to the next plugin
219
- _nextProxy = nextPlugin.getNext();
220
- nextPlugin.processTelemetry(env, _self);
221
- }
222
- };
223
-
224
- _self.createNew = (plugins:IPlugin[]|ITelemetryPluginChain = null, startAt?:IPlugin) => {
225
- return new ProcessTelemetryContext(plugins||_nextProxy, config, core, startAt);
226
- }
633
+ let context = createProcessTelemetryContext(pluginChain, config, core, startAt);
634
+ // Proxy all functions of the context to this object
635
+ proxyFunctions(_self, context, objKeys(context) as any);
227
636
  }
228
637
  }