UNPKG

@azure/monitor-opentelemetry

Version:
130 lines 5.57 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.MetricHandler = void 0; const monitor_opentelemetry_exporter_1 = require("@azure/monitor-opentelemetry-exporter"); const sdk_metrics_1 = require("@opentelemetry/sdk-metrics"); const standardMetrics_js_1 = require("./standardMetrics.js"); const types_js_1 = require("./types.js"); const liveMetrics_js_1 = require("./quickpulse/liveMetrics.js"); const performanceCounters_js_1 = require("./performanceCounters.js"); const index_js_1 = require("../shared/logging/index.js"); const DEFAULT_HISTOGRAM_AGGREGATION_ENV_VAR = "OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION"; function resolveHistogramAggregationFromEnv() { const envValue = process.env[DEFAULT_HISTOGRAM_AGGREGATION_ENV_VAR]; if (!envValue) { return undefined; } const normalized = envValue.trim().toLowerCase(); const aggregation = types_js_1.HISTOGRAM_AGGREGATION_MAP[normalized]; if (aggregation) { return aggregation; } const validValues = Object.keys(types_js_1.HISTOGRAM_AGGREGATION_MAP) .map((v) => `'${v}'`) .join(" and "); index_js_1.Logger.getInstance().warn(`${DEFAULT_HISTOGRAM_AGGREGATION_ENV_VAR} has unsupported value '${envValue}'. Supported values are ${validValues}.`); return undefined; } class AzureMonitorMetricExporterWithAggregation extends monitor_opentelemetry_exporter_1.AzureMonitorMetricExporter { _histogramAggregation; constructor(options, histogramAggregation) { super(options); this._histogramAggregation = histogramAggregation; } selectAggregation(instrumentType) { if (instrumentType === sdk_metrics_1.InstrumentType.HISTOGRAM && this._histogramAggregation) { return this._histogramAggregation; } return { type: sdk_metrics_1.AggregationType.DEFAULT }; } } /** * Azure Monitor OpenTelemetry Metric Handler */ class MetricHandler { _azureExporter; _metricReader; _standardMetrics; _performanceCounters; _liveMetrics; _config; _views; /** * Initializes a new instance of the MetricHandler class. * @param config - Distro configuration. * @param options - Metric Handler options. */ constructor(config) { const defaultInterval = 60000; this._config = config; // Adding Views of instrumentations will allow customer to add Metric Readers after, and get access to previously created metrics using the views shared state this._views = []; if (config.instrumentationOptions.azureSdk?.enabled) { this._views.push({ meterName: "@azure/opentelemetry-instrumentation-azure-sdk" }); } if (config.instrumentationOptions.http?.enabled) { this._views.push({ meterName: "@azure/opentelemetry-instrumentation-http" }); } if (config.instrumentationOptions.mongoDb?.enabled) { this._views.push({ meterName: "@azure/opentelemetry-instrumentation-mongodb" }); } if (config.instrumentationOptions.mySql?.enabled) { this._views.push({ meterName: "@opentelemetry/instrumentation-mysql" }); } if (config.instrumentationOptions.postgreSql?.enabled) { this._views.push({ meterName: "@opentelemetry/instrumentation-pg" }); } if (config.instrumentationOptions.redis?.enabled || config.instrumentationOptions.redis4?.enabled) { this._views.push({ meterName: "@opentelemetry/instrumentation-redis" }); } const histogramAggregation = resolveHistogramAggregationFromEnv(); this._azureExporter = new AzureMonitorMetricExporterWithAggregation(this._config.azureMonitorExporterOptions, histogramAggregation); const metricReaderOptions = { exporter: this._azureExporter, exportIntervalMillis: this._config.metricExportIntervalMillis || defaultInterval, }; this._metricReader = new sdk_metrics_1.PeriodicExportingMetricReader(metricReaderOptions); if (this._config.enableStandardMetrics && !process.env[types_js_1.APPLICATION_INSIGHTS_NO_STANDARD_METRICS]) { this._standardMetrics = new standardMetrics_js_1.StandardMetrics(this._config); } if (this._config.enableLiveMetrics) { this._liveMetrics = new liveMetrics_js_1.LiveMetrics(this._config); } if (this._config.enablePerformanceCounters) { this._performanceCounters = new performanceCounters_js_1.PerformanceCounterMetrics(this._config); } } getMetricReader() { return this._metricReader; } getViews() { return this._views; } markSpanAsProcessed(span) { this._standardMetrics?.markSpanAsProcessed(span); } recordSpan(span) { this._standardMetrics?.recordSpan(span); this._liveMetrics?.recordSpan(span); this._performanceCounters?.recordSpan(span); } recordLog(logRecord) { this._standardMetrics?.recordLog(logRecord); this._liveMetrics?.recordLog(logRecord); this._performanceCounters?.recordLog(logRecord); } /** * Shutdown handler */ async shutdown() { this._standardMetrics?.shutdown(); await this._liveMetrics?.shutdown(); await this._performanceCounters?.shutdown(); } } exports.MetricHandler = MetricHandler; //# sourceMappingURL=handler.js.map