UNPKG

@azure/monitor-opentelemetry

Version:
126 lines 5.33 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { AzureMonitorMetricExporter } from "@azure/monitor-opentelemetry-exporter"; import { AggregationType, InstrumentType, PeriodicExportingMetricReader, } from "@opentelemetry/sdk-metrics"; import { StandardMetrics } from "./standardMetrics.js"; import { APPLICATION_INSIGHTS_NO_STANDARD_METRICS, HISTOGRAM_AGGREGATION_MAP } from "./types.js"; import { LiveMetrics } from "./quickpulse/liveMetrics.js"; import { PerformanceCounterMetrics } from "./performanceCounters.js"; import { Logger } from "../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 = HISTOGRAM_AGGREGATION_MAP[normalized]; if (aggregation) { return aggregation; } const validValues = Object.keys(HISTOGRAM_AGGREGATION_MAP) .map((v) => `'${v}'`) .join(" and "); Logger.getInstance().warn(`${DEFAULT_HISTOGRAM_AGGREGATION_ENV_VAR} has unsupported value '${envValue}'. Supported values are ${validValues}.`); return undefined; } class AzureMonitorMetricExporterWithAggregation extends AzureMonitorMetricExporter { _histogramAggregation; constructor(options, histogramAggregation) { super(options); this._histogramAggregation = histogramAggregation; } selectAggregation(instrumentType) { if (instrumentType === InstrumentType.HISTOGRAM && this._histogramAggregation) { return this._histogramAggregation; } return { type: AggregationType.DEFAULT }; } } /** * Azure Monitor OpenTelemetry Metric Handler */ export 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 PeriodicExportingMetricReader(metricReaderOptions); if (this._config.enableStandardMetrics && !process.env[APPLICATION_INSIGHTS_NO_STANDARD_METRICS]) { this._standardMetrics = new StandardMetrics(this._config); } if (this._config.enableLiveMetrics) { this._liveMetrics = new LiveMetrics(this._config); } if (this._config.enablePerformanceCounters) { this._performanceCounters = new 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(); } } //# sourceMappingURL=handler.js.map