UNPKG

applicationinsights

Version:
162 lines 7.46 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.TelemetryClientProvider = void 0; const api_1 = require("@opentelemetry/api"); const monitor_opentelemetry_exporter_1 = require("@azure/monitor-opentelemetry-exporter"); const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http"); const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http"); const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http"); const sdk_logs_1 = require("@opentelemetry/sdk-logs"); const sdk_metrics_1 = require("@opentelemetry/sdk-metrics"); const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base"); const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node"); const resources_1 = require("@opentelemetry/resources"); /** * Provides isolated OpenTelemetry providers for a TelemetryClient instance. */ class TelemetryClientProvider { constructor(_options) { var _a; this._options = _options; this._metricReaders = []; this._spanProcessors = []; this._logProcessors = []; const resource = (_a = this._options.resource) !== null && _a !== void 0 ? _a : (0, resources_1.defaultResource)(); this._spanProcessors = this._setupTracing(); this._logProcessors = this._setupLogging(); this._metricReaders = this._setupMetrics(); this._tracerProvider = new sdk_trace_node_1.NodeTracerProvider({ resource, sampler: this._createSampler(), spanProcessors: this._spanProcessors, }); this._meterProvider = new sdk_metrics_1.MeterProvider({ resource, readers: this._metricReaders, }); this._loggerProvider = new sdk_logs_1.LoggerProvider({ resource, processors: this._logProcessors, }); } getTracer(name) { return this._tracerProvider.getTracer(name); } getMeter(name) { return this._meterProvider.getMeter(name); } getLogger(name) { return this._loggerProvider.getLogger(name); } async flush() { await Promise.all([ this._runWithErrorHandling(this._meterProvider.forceFlush(), "Failed to flush metrics"), this._runWithErrorHandling(this._tracerProvider.forceFlush(), "Failed to flush traces"), this._runWithErrorHandling(this._loggerProvider.forceFlush(), "Failed to flush logs"), ]); } async shutdown() { await Promise.all([ ...this._metricReaders.map((reader) => this._runWithErrorHandling(reader.shutdown(), "Failed to shutdown metric reader")), ...this._spanProcessors.map((processor) => this._runWithErrorHandling(processor.shutdown(), "Failed to shutdown span processor")), ...this._logProcessors.map((processor) => this._runWithErrorHandling(processor.shutdown(), "Failed to shutdown log processor")), this._runWithErrorHandling(this._meterProvider.shutdown(), "Failed to shutdown meter provider"), this._runWithErrorHandling(this._tracerProvider.shutdown(), "Failed to shutdown tracer provider"), this._runWithErrorHandling(this._loggerProvider.shutdown(), "Failed to shutdown logger provider"), ]); } _createSampler() { var _a; if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.samplingRatio) === undefined) { return undefined; } return new sdk_trace_base_1.ParentBasedSampler({ root: new sdk_trace_base_1.TraceIdRatioBasedSampler(this._options.samplingRatio), }); } _setupTracing() { var _a; const processors = []; try { const exporter = new monitor_opentelemetry_exporter_1.AzureMonitorTraceExporter(this._options.azureMonitorExporterOptions); processors.push(new sdk_trace_base_1.BatchSpanProcessor(exporter)); } catch (error) { api_1.diag.error("Failed to configure Azure Monitor trace exporter", error); } if ((_a = this._options.otlpTraceExporterConfig) === null || _a === void 0 ? void 0 : _a.enabled) { try { const otlpExporter = new exporter_trace_otlp_http_1.OTLPTraceExporter(this._options.otlpTraceExporterConfig); processors.push(new sdk_trace_base_1.BatchSpanProcessor(otlpExporter)); } catch (error) { api_1.diag.error("Failed to configure OTLP trace exporter", error); } } if (this._options.spanProcessors) { for (const processor of this._options.spanProcessors) { processors.push(processor); } } return processors; } _setupLogging() { var _a; const processors = []; try { const exporter = new monitor_opentelemetry_exporter_1.AzureMonitorLogExporter(this._options.azureMonitorExporterOptions); processors.push(new sdk_logs_1.BatchLogRecordProcessor(exporter)); } catch (error) { api_1.diag.error("Failed to configure Azure Monitor log exporter", error); } if ((_a = this._options.otlpLogExporterConfig) === null || _a === void 0 ? void 0 : _a.enabled) { try { const otlpExporter = new exporter_logs_otlp_http_1.OTLPLogExporter(this._options.otlpLogExporterConfig); processors.push(new sdk_logs_1.BatchLogRecordProcessor(otlpExporter)); } catch (error) { api_1.diag.error("Failed to configure OTLP log exporter", error); } } if (this._options.logRecordProcessors) { for (const processor of this._options.logRecordProcessors) { processors.push(processor); } } return processors; } _setupMetrics() { var _a; const readers = []; try { const exporter = new monitor_opentelemetry_exporter_1.AzureMonitorMetricExporter(this._options.azureMonitorExporterOptions); readers.push(new sdk_metrics_1.PeriodicExportingMetricReader({ exporter })); } catch (error) { api_1.diag.error("Failed to configure Azure Monitor metric exporter", error); } if ((_a = this._options.otlpMetricExporterConfig) === null || _a === void 0 ? void 0 : _a.enabled) { try { const otlpExporter = new exporter_metrics_otlp_http_1.OTLPMetricExporter(this._options.otlpMetricExporterConfig); readers.push(new sdk_metrics_1.PeriodicExportingMetricReader({ exporter: otlpExporter })); } catch (error) { api_1.diag.error("Failed to configure OTLP metric exporter", error); } } return readers; } async _runWithErrorHandling(promise, message) { try { await promise; } catch (error) { api_1.diag.error(message, error); } } } exports.TelemetryClientProvider = TelemetryClientProvider; //# sourceMappingURL=telemetryClientProvider.js.map