UNPKG

10.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var CorrelationContextManager = require("./AutoCollection/CorrelationContextManager"); // Keep this first
4var AutoCollectConsole = require("./AutoCollection/Console");
5var AutoCollectExceptions = require("./AutoCollection/Exceptions");
6var AutoCollectPerformance = require("./AutoCollection/Performance");
7var AutoCollectHttpDependencies = require("./AutoCollection/HttpDependencies");
8var AutoCollectHttpRequests = require("./AutoCollection/HttpRequests");
9var Logging = require("./Library/Logging");
10// We export these imports so that SDK users may use these classes directly.
11// They're exposed using "export import" so that types are passed along as expected
12exports.TelemetryClient = require("./Library/NodeClient");
13exports.Contracts = require("./Declarations/Contracts");
14// Default autocollection configuration
15var _isConsole = true;
16var _isConsoleLog = false;
17var _isExceptions = true;
18var _isPerformance = true;
19var _isRequests = true;
20var _isDependencies = true;
21var _isDiskRetry = true;
22var _isCorrelating = true;
23var _diskRetryInterval = undefined;
24var _diskRetryMaxBytes = undefined;
25var _console;
26var _exceptions;
27var _performance;
28var _serverRequests;
29var _clientRequests;
30var _isStarted = false;
31/**
32 * Initializes the default client. Should be called after setting
33 * configuration options.
34 *
35 * @param instrumentationKey the instrumentation key to use. Optional, if
36 * this is not specified, the value will be read from the environment
37 * variable APPINSIGHTS_INSTRUMENTATIONKEY.
38 * @returns {Configuration} the configuration class to initialize
39 * and start the SDK.
40 */
41function setup(instrumentationKey) {
42 if (!exports.defaultClient) {
43 exports.defaultClient = new exports.TelemetryClient(instrumentationKey);
44 _console = new AutoCollectConsole(exports.defaultClient);
45 _exceptions = new AutoCollectExceptions(exports.defaultClient);
46 _performance = new AutoCollectPerformance(exports.defaultClient);
47 _serverRequests = new AutoCollectHttpRequests(exports.defaultClient);
48 _clientRequests = new AutoCollectHttpDependencies(exports.defaultClient);
49 }
50 else {
51 Logging.info("The default client is already setup");
52 }
53 if (exports.defaultClient && exports.defaultClient.channel) {
54 exports.defaultClient.channel.setUseDiskRetryCaching(_isDiskRetry, _diskRetryInterval, _diskRetryMaxBytes);
55 }
56 return Configuration;
57}
58exports.setup = setup;
59/**
60 * Starts automatic collection of telemetry. Prior to calling start no
61 * telemetry will be *automatically* collected, though manual collection
62 * is enabled.
63 * @returns {ApplicationInsights} this class
64 */
65function start() {
66 if (!!exports.defaultClient) {
67 _isStarted = true;
68 _console.enable(_isConsole, _isConsoleLog);
69 _exceptions.enable(_isExceptions);
70 _performance.enable(_isPerformance);
71 _serverRequests.useAutoCorrelation(_isCorrelating);
72 _serverRequests.enable(_isRequests);
73 _clientRequests.enable(_isDependencies);
74 }
75 else {
76 Logging.warn("Start cannot be called before setup");
77 }
78 return Configuration;
79}
80exports.start = start;
81/**
82 * Returns an object that is shared across all code handling a given request.
83 * This can be used similarly to thread-local storage in other languages.
84 * Properties set on this object will be available to telemetry processors.
85 *
86 * Do not store sensitive information here.
87 * Custom properties set on this object can be exposed in a future SDK
88 * release via outgoing HTTP headers.
89 * This is to allow for correlating data cross-component.
90 *
91 * This method will return null if automatic dependency correlation is disabled.
92 * @returns A plain object for request storage or null if automatic dependency correlation is disabled.
93 */
94function getCorrelationContext() {
95 if (_isCorrelating) {
96 return CorrelationContextManager.CorrelationContextManager.getCurrentContext();
97 }
98 return null;
99}
100exports.getCorrelationContext = getCorrelationContext;
101/**
102 * Returns a function that will get the same correlation context within its
103 * function body as the code executing this function.
104 * Use this method if automatic dependency correlation is not propagating
105 * correctly to an asynchronous callback.
106 */
107function wrapWithCorrelationContext(fn) {
108 return CorrelationContextManager.CorrelationContextManager.wrapCallback(fn);
109}
110exports.wrapWithCorrelationContext = wrapWithCorrelationContext;
111/**
112 * The active configuration for global SDK behaviors, such as autocollection.
113 */
114var Configuration = (function () {
115 function Configuration() {
116 }
117 /**
118 * Sets the state of console and logger tracking (enabled by default for third-party loggers only)
119 * @param value if true logger activity will be sent to Application Insights
120 * @param collectConsoleLog if true, logger autocollection will include console.log calls (default false)
121 * @returns {Configuration} this class
122 */
123 Configuration.setAutoCollectConsole = function (value, collectConsoleLog) {
124 if (collectConsoleLog === void 0) { collectConsoleLog = false; }
125 _isConsole = value;
126 _isConsoleLog = collectConsoleLog;
127 if (_isStarted) {
128 _console.enable(value, collectConsoleLog);
129 }
130 return Configuration;
131 };
132 /**
133 * Sets the state of exception tracking (enabled by default)
134 * @param value if true uncaught exceptions will be sent to Application Insights
135 * @returns {Configuration} this class
136 */
137 Configuration.setAutoCollectExceptions = function (value) {
138 _isExceptions = value;
139 if (_isStarted) {
140 _exceptions.enable(value);
141 }
142 return Configuration;
143 };
144 /**
145 * Sets the state of performance tracking (enabled by default)
146 * @param value if true performance counters will be collected every second and sent to Application Insights
147 * @returns {Configuration} this class
148 */
149 Configuration.setAutoCollectPerformance = function (value) {
150 _isPerformance = value;
151 if (_isStarted) {
152 _performance.enable(value);
153 }
154 return Configuration;
155 };
156 /**
157 * Sets the state of request tracking (enabled by default)
158 * @param value if true requests will be sent to Application Insights
159 * @returns {Configuration} this class
160 */
161 Configuration.setAutoCollectRequests = function (value) {
162 _isRequests = value;
163 if (_isStarted) {
164 _serverRequests.enable(value);
165 }
166 return Configuration;
167 };
168 /**
169 * Sets the state of dependency tracking (enabled by default)
170 * @param value if true dependencies will be sent to Application Insights
171 * @returns {Configuration} this class
172 */
173 Configuration.setAutoCollectDependencies = function (value) {
174 _isDependencies = value;
175 if (_isStarted) {
176 _clientRequests.enable(value);
177 }
178 return Configuration;
179 };
180 /**
181 * Sets the state of automatic dependency correlation (enabled by default)
182 * @param value if true dependencies will be correlated with requests
183 * @returns {Configuration} this class
184 */
185 Configuration.setAutoDependencyCorrelation = function (value) {
186 _isCorrelating = value;
187 if (_isStarted) {
188 _serverRequests.useAutoCorrelation(value);
189 }
190 return Configuration;
191 };
192 /**
193 * Enable or disable disk-backed retry caching to cache events when client is offline (enabled by default)
194 * Note that this method only applies to the default client. Disk-backed retry caching is disabled by default for additional clients.
195 * For enable for additional clients, use client.channel.setUseDiskRetryCaching(true).
196 * These cached events are stored in your system or user's temporary directory and access restricted to your user when possible.
197 * @param value if true events that occured while client is offline will be cached on disk
198 * @param resendInterval The wait interval for resending cached events.
199 * @param maxBytesOnDisk The maximum size (in bytes) that the created temporary directory for cache events can grow to, before caching is disabled.
200 * @returns {Configuration} this class
201 */
202 Configuration.setUseDiskRetryCaching = function (value, resendInterval, maxBytesOnDisk) {
203 _isDiskRetry = value;
204 _diskRetryInterval = resendInterval;
205 _diskRetryMaxBytes = maxBytesOnDisk;
206 if (exports.defaultClient && exports.defaultClient.channel) {
207 exports.defaultClient.channel.setUseDiskRetryCaching(value, resendInterval, maxBytesOnDisk);
208 }
209 return Configuration;
210 };
211 /**
212 * Enables debug and warning logging for AppInsights itself.
213 * @param enableDebugLogging if true, enables debug logging
214 * @param enableWarningLogging if true, enables warning logging
215 * @returns {Configuration} this class
216 */
217 Configuration.setInternalLogging = function (enableDebugLogging, enableWarningLogging) {
218 if (enableDebugLogging === void 0) { enableDebugLogging = false; }
219 if (enableWarningLogging === void 0) { enableWarningLogging = true; }
220 Logging.enableDebug = enableDebugLogging;
221 Logging.disableWarnings = !enableWarningLogging;
222 return Configuration;
223 };
224 // Convenience shortcut to ApplicationInsights.start
225 Configuration.start = start;
226 return Configuration;
227}());
228exports.Configuration = Configuration;
229/**
230 * Disposes the default client and all the auto collectors so they can be reinitialized with different configuration
231*/
232function dispose() {
233 exports.defaultClient = null;
234 _isStarted = false;
235 if (_console) {
236 _console.dispose();
237 }
238 if (_exceptions) {
239 _exceptions.dispose();
240 }
241 if (_performance) {
242 _performance.dispose();
243 }
244 if (_serverRequests) {
245 _serverRequests.dispose();
246 }
247 if (_clientRequests) {
248 _clientRequests.dispose();
249 }
250}
251exports.dispose = dispose;
252//# sourceMappingURL=applicationinsights.js.map
\No newline at end of file