UNPKG

@azure/monitor-opentelemetry

Version:
97 lines 5.49 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.useAzureMonitor = useAzureMonitor; exports.shutdownAzureMonitor = shutdownAzureMonitor; const api_1 = require("@opentelemetry/api"); const api_logs_1 = require("@opentelemetry/api-logs"); const sdk_node_1 = require("@opentelemetry/sdk-node"); const config_js_1 = require("./shared/config.js"); const index_js_1 = require("./metrics/index.js"); const handler_js_1 = require("./traces/handler.js"); const index_js_2 = require("./logs/index.js"); const types_js_1 = require("./types.js"); const browserSdkLoader_js_1 = require("./browserSdkLoader/browserSdkLoader.js"); const utils_js_1 = require("./metrics/quickpulse/utils.js"); const statsbeat_js_1 = require("./utils/statsbeat.js"); const opentelemetryInstrumentationPatcher_js_1 = require("./utils/opentelemetryInstrumentationPatcher.js"); const common_js_1 = require("./utils/common.js"); process.env["AZURE_MONITOR_DISTRO_VERSION"] = types_js_1.AZURE_MONITOR_OPENTELEMETRY_VERSION; let sdk; let browserSdkLoader; /** * Initialize Azure Monitor Distro * @param options - Azure Monitor OpenTelemetry Options */ function useAzureMonitor(options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; const config = new config_js_1.InternalConfig(options); (0, opentelemetryInstrumentationPatcher_js_1.patchOpenTelemetryInstrumentationEnable)(); const statsbeatInstrumentations = { // Instrumentations azureSdk: (_b = (_a = config.instrumentationOptions) === null || _a === void 0 ? void 0 : _a.azureSdk) === null || _b === void 0 ? void 0 : _b.enabled, mongoDb: (_d = (_c = config.instrumentationOptions) === null || _c === void 0 ? void 0 : _c.mongoDb) === null || _d === void 0 ? void 0 : _d.enabled, mySql: (_f = (_e = config.instrumentationOptions) === null || _e === void 0 ? void 0 : _e.mySql) === null || _f === void 0 ? void 0 : _f.enabled, postgreSql: (_h = (_g = config.instrumentationOptions) === null || _g === void 0 ? void 0 : _g.postgreSql) === null || _h === void 0 ? void 0 : _h.enabled, redis: (_k = (_j = config.instrumentationOptions) === null || _j === void 0 ? void 0 : _j.redis) === null || _k === void 0 ? void 0 : _k.enabled, bunyan: (_m = (_l = config.instrumentationOptions) === null || _l === void 0 ? void 0 : _l.bunyan) === null || _m === void 0 ? void 0 : _m.enabled, winston: (_p = (_o = config.instrumentationOptions) === null || _o === void 0 ? void 0 : _o.winston) === null || _p === void 0 ? void 0 : _p.enabled, }; const statsbeatFeatures = { browserSdkLoader: config.browserSdkLoaderOptions.enabled, aadHandling: !!((_q = config.azureMonitorExporterOptions) === null || _q === void 0 ? void 0 : _q.credential), diskRetry: !((_r = config.azureMonitorExporterOptions) === null || _r === void 0 ? void 0 : _r.disableOfflineStorage), }; (0, statsbeat_js_1.getInstance)().setStatsbeatFeatures(statsbeatInstrumentations, statsbeatFeatures); if (config.browserSdkLoaderOptions.enabled) { browserSdkLoader = new browserSdkLoader_js_1.BrowserSdkLoader(config); } // Remove global providers in OpenTelemetry, these would be overridden if present api_1.metrics.disable(); api_1.trace.disable(); api_logs_1.logs.disable(); // Create internal handlers const metricHandler = new index_js_1.MetricHandler(config); const traceHandler = new handler_js_1.TraceHandler(config, metricHandler); const logHandler = new index_js_2.LogHandler(config, metricHandler); const instrumentations = traceHandler .getInstrumentations() .concat(logHandler.getInstrumentations()); const resourceDetectorsList = (0, common_js_1.parseResourceDetectorsFromEnvVar)(); // Add extra SpanProcessors, and logRecordProcessors from user configuration const spanProcessors = (options === null || options === void 0 ? void 0 : options.spanProcessors) || []; const logRecordProcessors = (options === null || options === void 0 ? void 0 : options.logRecordProcessors) || []; // Initialize OpenTelemetry SDK const sdkConfig = { autoDetectResources: true, metricReader: metricHandler.getMetricReader(), views: metricHandler.getViews(), instrumentations: instrumentations, logRecordProcessors: [ logHandler.getAzureLogRecordProcessor(), ...logRecordProcessors, logHandler.getBatchLogRecordProcessor(), ], resource: config.resource, sampler: traceHandler.getSampler(), spanProcessors: [ traceHandler.getAzureMonitorSpanProcessor(), ...spanProcessors, traceHandler.getBatchSpanProcessor(), ], resourceDetectors: resourceDetectorsList, }; sdk = new sdk_node_1.NodeSDK(sdkConfig); (0, utils_js_1.setSdkPrefix)(); sdk.start(); } /** * Shutdown Azure Monitor Open Telemetry Distro * @see https://github.com/open-telemetry/opentelemetry-js/blob/0229434cb5a3179f63c021105f36270ae7897929/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L398 */ function shutdownAzureMonitor() { browserSdkLoader === null || browserSdkLoader === void 0 ? void 0 : browserSdkLoader.dispose(); return sdk === null || sdk === void 0 ? void 0 : sdk.shutdown(); } //# sourceMappingURL=index.js.map