UNPKG

@azure/monitor-opentelemetry

Version:
97 lines 4.02 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { AzureMonitorMetricExporter } from "@azure/monitor-opentelemetry-exporter"; import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics"; import { StandardMetrics } from "./standardMetrics.js"; import { APPLICATION_INSIGHTS_NO_STANDARD_METRICS } from "./types.js"; import { LiveMetrics } from "./quickpulse/liveMetrics.js"; import { PerformanceCounterMetrics } from "./performanceCounters.js"; /** * 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.redis4?.enabled) { this._views.push({ meterName: "@opentelemetry/instrumentation-redis-4" }); } if (config.instrumentationOptions.redis?.enabled) { this._views.push({ meterName: "@azure/opentelemetry-instrumentation-redis" }); } this._azureExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterOptions); 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 */ // eslint-disable-next-line @typescript-eslint/require-await async shutdown() { this._standardMetrics?.shutdown(); this._liveMetrics?.shutdown(); this._performanceCounters?.shutdown(); } } //# sourceMappingURL=handler.js.map