UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

280 lines 13.1 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.dispose = exports.Configuration = exports.wrapWithCorrelationContext = exports.startOperation = exports.getCorrelationContext = exports.start = exports.setup = exports.defaultClient = exports.TelemetryClient = exports.DistributedTracingModes = exports.Contracts = void 0; const api_1 = require("@opentelemetry/api"); const correlationContextManager_1 = require("./correlationContextManager"); const types_1 = require("./types"); Object.defineProperty(exports, "DistributedTracingModes", { enumerable: true, get: function () { return types_1.DistributedTracingModes; } }); const telemetryClient_1 = require("./telemetryClient"); Object.defineProperty(exports, "TelemetryClient", { enumerable: true, get: function () { return telemetryClient_1.TelemetryClient; } }); const Contracts = require("../declarations/contracts"); exports.Contracts = Contracts; const util_1 = require("../shared/util"); /** * Initializes the default client. Should be called after setting * configuration options. * * @param setupString the Connection String or Instrumentation Key to use. Optional, if * this is not specified, the value will be read from the environment * variable APPLICATIONINSIGHTS_CONNECTION_STRING or APPINSIGHTS_INSTRUMENTATIONKEY. * @returns {Configuration} the configuration class to initialize * and start the SDK. */ function setup(setupString) { if (!exports.defaultClient) { exports.defaultClient = new telemetryClient_1.TelemetryClient(setupString); } else { exports.defaultClient.pushWarningToLog("Setup has already been called once. To set up a new client, please use TelemetryClient instead."); } return Configuration; } exports.setup = setup; /** * Starts automatic collection of telemetry. Prior to calling start no * telemetry will be *automatically* collected, though manual collection * is enabled. * @returns {ApplicationInsights} this class */ function start() { try { if (!exports.defaultClient) { api_1.diag.setLogger(new api_1.DiagConsoleLogger()); api_1.diag.warn("Start cannot be called before setup. Please call setup() first."); } else { exports.defaultClient.initialize(); } return Configuration; } catch (error) { api_1.diag.warn(`Failed to start default client: ${util_1.Util.getInstance().dumpObj(error)}`); } } exports.start = start; /** * Returns an object that is shared across all code handling a given request. * This can be used similarly to thread-local storage in other languages. * Properties set on this object will be available to telemetry processors. * * Do not store sensitive information here. * Custom properties set on this object can be exposed in a future SDK * release via outgoing HTTP headers. * This is to allow for correlating data cross-component. * * This method will return null if automatic dependency correlation is disabled. * @returns A plain object for request storage or null if automatic dependency correlation is disabled. */ function getCorrelationContext() { return correlationContextManager_1.CorrelationContextManager.getCurrentContext(); } exports.getCorrelationContext = getCorrelationContext; /** * **(Experimental!)** * Starts a fresh context or propagates the current internal one. */ function startOperation(arg1, arg2) { return correlationContextManager_1.CorrelationContextManager.startOperation(arg1, arg2); } exports.startOperation = startOperation; /** * Returns a function that will get the same correlation context within its * function body as the code executing this function. * Use this method if automatic dependency correlation is not propagating * correctly to an asynchronous callback. */ function wrapWithCorrelationContext(fn, context) { return correlationContextManager_1.CorrelationContextManager.wrapCallback(fn, context); } exports.wrapWithCorrelationContext = wrapWithCorrelationContext; /** * The active configuration for global SDK behaviors, such as auto collection. */ class Configuration { /** * Only W3C traing mode is currently suppported so this method informs the user if they attempt to set the value. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars static setDistributedTracingMode(value) { if (exports.defaultClient) { exports.defaultClient.config.distributedTracingMode = value; } return Configuration; } /** * Sets the state of console and logger tracking (enabled by default for third-party loggers only) * @param value if true logger activity will be sent to Application Insights * @param collectConsoleLog if true, logger autocollection will include console.log calls (default false) * @returns {Configuration} this class */ static setAutoCollectConsole(value, collectConsoleLog = false) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectExternalLoggers = value; exports.defaultClient.config.enableAutoCollectConsole = collectConsoleLog; } return Configuration; } /** * Sets the state of exception tracking (enabled by default) * @param value if true uncaught exceptions will be sent to Application Insights * @returns {Configuration} this class */ static setAutoCollectExceptions(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectExceptions = value; } return Configuration; } /** * Sets the state of performance tracking (enabled by default) * @param value if true performance counters will be collected every second and sent to Application Insights * @param collectExtendedMetrics if true, extended metrics counters will be collected every minute and sent to Application Insights * @returns {Configuration} this class */ // eslint-disable-next-line @typescript-eslint/no-unused-vars static setAutoCollectPerformance(value, collectExtendedMetrics) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectPerformance = value; exports.defaultClient.config.enableAutoCollectExtendedMetrics = collectExtendedMetrics; } return Configuration; } /** * Sets the state of pre aggregated metrics tracking (enabled by default) * @param value if true pre aggregated metrics will be collected every minute and sent to Application Insights * @returns {Configuration} this class */ static setAutoCollectPreAggregatedMetrics(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectPreAggregatedMetrics = value; } return Configuration; } /** * Sets the state of request tracking (enabled by default) * @param value if true HeartBeat metric data will be collected every 15 minutes and sent to Application Insights * @returns {Configuration} this class */ static setAutoCollectHeartbeat(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectHeartbeat = value; } return Configuration; } /** * Sets the state of Web snippet injection * @param value if true Web snippet will try to be injected in server response * @param WebSnippetConnectionString if provided, Web snippet injection will use this ConnectionString. Default to use the connectionString in Node.js app initialization. * @returns {Configuration} this class */ static enableWebInstrumentation(value, WebSnippetConnectionString) { if (exports.defaultClient) { exports.defaultClient.config.enableWebInstrumentation = value; exports.defaultClient.config.webInstrumentationConnectionString = WebSnippetConnectionString; } return Configuration; } /** * Sets the state of request tracking (enabled by default) * @param value if true requests will be sent to Application Insights * @returns {Configuration} this class */ static setAutoCollectRequests(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectRequests = value; } return Configuration; } /** * Sets the state of dependency tracking (enabled by default) * @param value if true dependencies will be sent to Application Insights * @returns {Configuration} this class */ static setAutoCollectDependencies(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectDependencies = value; } return Configuration; } /** * Sets the state of automatic dependency correlation (enabled by default) * @param value if true dependencies will be correlated with requests * @param useAsyncHooks if true, forces use of experimental async_hooks module to provide correlation. If false, instead uses only patching-based techniques. If left blank, the best option is chosen for you based on your version of Node.js. * @returns {Configuration} this class */ static setAutoDependencyCorrelation(value, useAsyncHooks) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoDependencyCorrelation = value; exports.defaultClient.config.enableUseAsyncHooks = useAsyncHooks; } return Configuration; } /** * Enable or disable disk-backed retry caching to cache events when client is offline (enabled by default) * Note that this method only applies to the default client. Disk-backed retry caching is disabled by default for additional clients. * For enable for additional clients, use client.channel.setUseDiskRetryCaching(true). * These cached events are stored in your system or user's temporary directory and access restricted to your user when possible. * @param value if true events that occured while client is offline will be cached on disk * @param resendInterval The wait interval for resending cached events. * @param maxBytesOnDisk The maximum size (in bytes) that the created temporary directory for cache events can grow to, before caching is disabled. * @returns {Configuration} this class */ static setUseDiskRetryCaching(value, resendInterval, maxBytesOnDisk) { if (exports.defaultClient) { exports.defaultClient.config.enableUseDiskRetryCaching = value; exports.defaultClient.config.enableResendInterval = resendInterval; exports.defaultClient.config.enableMaxBytesOnDisk = maxBytesOnDisk; } return Configuration; } /** * Enables debug and warning Logger for AppInsights itself. * @param enableDebugLogger if true, enables debug Logger * @param enableWarningLogger if true, enables warning Logger * @returns {Configuration} this class */ static setInternalLogging(enableDebugLogger = false, enableWarningLogger = true) { if (exports.defaultClient) { exports.defaultClient.config.enableInternalDebugLogging = enableDebugLogger; exports.defaultClient.config.enableInternalWarningLogging = enableWarningLogger; } return Configuration; } /** * Enable automatic incoming request tracking when using Azure Functions * @param value if true auto collection of incoming requests will be enabled * @returns {Configuration} this class */ static setAutoCollectIncomingRequestAzureFunctions(value) { if (exports.defaultClient) { exports.defaultClient.config.enableAutoCollectIncomingRequestAzureFunctions = value; } return Configuration; } /** * Enables communication with Application Insights Live Metrics. * @param enable if true, enables communication with the live metrics service */ static setSendLiveMetrics(enable = false) { if (exports.defaultClient) { exports.defaultClient.config.enableSendLiveMetrics = enable; } return Configuration; } } exports.Configuration = Configuration; // Convenience shortcut to ApplicationInsights.start Configuration.start = start; /** * Disposes the default client and all the auto collectors so they can be reinitialized with different configuration */ function dispose() { if (exports.defaultClient) { exports.defaultClient.shutdown(); } exports.defaultClient = null; } exports.dispose = dispose; //# sourceMappingURL=applicationinsights.js.map