@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
95 lines • 4.95 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { AzureMonitorMetricExporter } from "@azure/monitor-opentelemetry-exporter";
import { PeriodicExportingMetricReader, View } 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 {
/**
* Initializes a new instance of the MetricHandler class.
* @param config - Distro configuration.
* @param options - Metric Handler options.
*/
constructor(config, options) {
var _a, _b, _c, _d, _e, _f, _g;
this._collectionInterval = 60000; // 60 seconds
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 ((_a = config.instrumentationOptions.azureSdk) === null || _a === void 0 ? void 0 : _a.enabled) {
this._views.push(new View({ meterName: "@azure/opentelemetry-instrumentation-azure-sdk" }));
}
if ((_b = config.instrumentationOptions.http) === null || _b === void 0 ? void 0 : _b.enabled) {
this._views.push(new View({ meterName: "@azure/opentelemetry-instrumentation-http" }));
}
if ((_c = config.instrumentationOptions.mongoDb) === null || _c === void 0 ? void 0 : _c.enabled) {
this._views.push(new View({ meterName: "@azure/opentelemetry-instrumentation-mongodb" }));
}
if ((_d = config.instrumentationOptions.mySql) === null || _d === void 0 ? void 0 : _d.enabled) {
this._views.push(new View({ meterName: "@opentelemetry/instrumentation-mysql" }));
}
if ((_e = config.instrumentationOptions.postgreSql) === null || _e === void 0 ? void 0 : _e.enabled) {
this._views.push(new View({ meterName: "@opentelemetry/instrumentation-pg" }));
}
if ((_f = config.instrumentationOptions.redis4) === null || _f === void 0 ? void 0 : _f.enabled) {
this._views.push(new View({ meterName: "@opentelemetry/instrumentation-redis-4" }));
}
if ((_g = config.instrumentationOptions.redis) === null || _g === void 0 ? void 0 : _g.enabled) {
this._views.push(new View({ meterName: "@azure/opentelemetry-instrumentation-redis" }));
}
this._azureExporter = new AzureMonitorMetricExporter(this._config.azureMonitorExporterOptions);
const metricReaderOptions = {
exporter: this._azureExporter,
exportIntervalMillis: (options === null || options === void 0 ? void 0 : options.collectionInterval) || this._collectionInterval,
};
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) {
var _a;
(_a = this._standardMetrics) === null || _a === void 0 ? void 0 : _a.markSpanAsProcessed(span);
}
recordSpan(span) {
var _a, _b, _c;
(_a = this._standardMetrics) === null || _a === void 0 ? void 0 : _a.recordSpan(span);
(_b = this._liveMetrics) === null || _b === void 0 ? void 0 : _b.recordSpan(span);
(_c = this._performanceCounters) === null || _c === void 0 ? void 0 : _c.recordSpan(span);
}
recordLog(logRecord) {
var _a, _b, _c;
(_a = this._standardMetrics) === null || _a === void 0 ? void 0 : _a.recordLog(logRecord);
(_b = this._liveMetrics) === null || _b === void 0 ? void 0 : _b.recordLog(logRecord);
(_c = this._performanceCounters) === null || _c === void 0 ? void 0 : _c.recordLog(logRecord);
}
/**
* Shutdown handler
*/
// eslint-disable-next-line @typescript-eslint/require-await
async shutdown() {
var _a, _b, _c;
(_a = this._standardMetrics) === null || _a === void 0 ? void 0 : _a.shutdown();
(_b = this._liveMetrics) === null || _b === void 0 ? void 0 : _b.shutdown();
(_c = this._performanceCounters) === null || _c === void 0 ? void 0 : _c.shutdown();
}
}
//# sourceMappingURL=handler.js.map