@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
154 lines • 7.41 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TraceHandler = void 0;
const tslib_1 = require("tslib");
const opentelemetry_instrumentation_azure_sdk_1 = require("@azure/opentelemetry-instrumentation-azure-sdk");
const coreTracing = tslib_1.__importStar(require("@azure/core-tracing"));
const monitor_opentelemetry_exporter_1 = require("@azure/monitor-opentelemetry-exporter");
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
const instrumentation_mongodb_1 = require("@opentelemetry/instrumentation-mongodb");
const instrumentation_mysql_1 = require("@opentelemetry/instrumentation-mysql");
const instrumentation_pg_1 = require("@opentelemetry/instrumentation-pg");
const instrumentation_redis_1 = require("@opentelemetry/instrumentation-redis");
const common_js_1 = require("../utils/common.js");
const spanProcessor_js_1 = require("./spanProcessor.js");
const azureFnHook_js_1 = require("./azureFnHook.js");
const sampler_js_1 = require("./sampler.js");
const index_js_1 = require("../shared/logging/index.js");
/**
* Azure Monitor OpenTelemetry Trace Handler
*/
class TraceHandler {
_batchSpanProcessor;
_azureSpanProcessor;
_azureExporter;
_instrumentations;
_config;
_metricHandler;
_azureFunctionsHook;
_sampler;
/**
* Initializes a new instance of the TraceHandler class.
* @param _config - Configuration.
* @param _metricHandler - MetricHandler.
*/
constructor(config, metricHandler) {
this._config = config;
this._metricHandler = metricHandler;
this._instrumentations = [];
// Check sampler precedence
if (this._config.sampler) {
this._sampler = this._config.sampler;
}
else if (this._config.tracesPerSecond && this._config.tracesPerSecond > 0) {
// If tracesPerSecond is set to a positive number, use RateLimitedSampler
this._sampler = new monitor_opentelemetry_exporter_1.RateLimitedSampler(this._config.tracesPerSecond);
}
else {
// Otherwise, use PercentageSampler with samplingRatio
this._sampler = new sampler_js_1.ApplicationInsightsSampler(this._config.samplingRatio);
}
this._azureExporter = new monitor_opentelemetry_exporter_1.AzureMonitorTraceExporter(this._config.azureMonitorExporterOptions);
const bufferConfig = {
maxExportBatchSize: 512,
scheduledDelayMillis: 5000,
exportTimeoutMillis: 30000,
maxQueueSize: 2048,
};
this._batchSpanProcessor = new sdk_trace_base_1.BatchSpanProcessor(this._azureExporter, bufferConfig);
this._azureSpanProcessor = new spanProcessor_js_1.AzureMonitorSpanProcessor(this._metricHandler);
this._azureFunctionsHook = new azureFnHook_js_1.AzureFunctionsHook();
this._initializeInstrumentations();
}
getSampler() {
return this._sampler;
}
getBatchSpanProcessor() {
return this._batchSpanProcessor;
}
getAzureMonitorSpanProcessor() {
return this._azureSpanProcessor;
}
getInstrumentations() {
return this._instrumentations;
}
/**
* Shutdown handler
*/
async shutdown() {
this._azureFunctionsHook.shutdown();
await this._batchSpanProcessor.shutdown();
await this._azureSpanProcessor.shutdown();
await this._azureExporter.shutdown();
}
/**
* Start auto collection of telemetry
*/
_initializeInstrumentations() {
if (this._config.instrumentationOptions.http?.enabled) {
const httpinstrumentationOptions = this._config.instrumentationOptions
.http;
const providedIgnoreOutgoingRequestHook = httpinstrumentationOptions.ignoreOutgoingRequestHook;
const mergedIgnoreOutgoingRequestHook = (request) => {
const result = (0, common_js_1.ignoreOutgoingRequestHook)(request);
if (!result) {
// Not internal call
if (providedIgnoreOutgoingRequestHook) {
// Provided hook in config
return providedIgnoreOutgoingRequestHook(request);
}
}
return result;
};
httpinstrumentationOptions.ignoreOutgoingRequestHook = mergedIgnoreOutgoingRequestHook;
this._instrumentations.push(new instrumentation_http_1.HttpInstrumentation(this._config.instrumentationOptions.http));
}
if (this._config.instrumentationOptions.azureSdk?.enabled) {
const azureSdkInstrumentation = (0, opentelemetry_instrumentation_azure_sdk_1.createAzureSdkInstrumentation)(this._config.instrumentationOptions.azureSdk);
this._instrumentations.push(azureSdkInstrumentation);
this._wireAzureSdkInstrumenter(azureSdkInstrumentation);
}
if (this._config.instrumentationOptions.mongoDb?.enabled) {
this._instrumentations.push(new instrumentation_mongodb_1.MongoDBInstrumentation(this._config.instrumentationOptions.mongoDb));
}
if (this._config.instrumentationOptions.mySql?.enabled) {
this._instrumentations.push(new instrumentation_mysql_1.MySQLInstrumentation(this._config.instrumentationOptions.mySql));
}
if (this._config.instrumentationOptions.postgreSql?.enabled) {
this._instrumentations.push(new instrumentation_pg_1.PgInstrumentation(this._config.instrumentationOptions.postgreSql));
}
if (this._config.instrumentationOptions.redis?.enabled ||
this._config.instrumentationOptions.redis4?.enabled) {
this._instrumentations.push(new instrumentation_redis_1.RedisInstrumentation(this._config.instrumentationOptions.redis));
}
}
/**
* Wire the Azure SDK instrumenter into `@azure/core-tracing` directly.
*
* The Azure SDK instrumentation registers its instrumenter through an OpenTelemetry
* module-patch hook that only fires via `require`/`import`-in-the-middle. In ESM hosts
* where the OpenTelemetry loader cannot be registered up front (for example, Azure
* Functions, which controls the Node.js start command), that hook never runs, so Azure
* SDK dependency spans are missing. Because `@azure/core-tracing` resolves its
* instrumenter lazily at span-creation time from shared module-local state, applying
* the same patch directly here enables Azure SDK tracing regardless of module system.
*/
_wireAzureSdkInstrumenter(instrumentation) {
try {
const moduleDefinitions = instrumentation.getModuleDefinitions?.() ?? [];
for (const moduleDefinition of moduleDefinitions) {
if (moduleDefinition.name === "@azure/core-tracing" && moduleDefinition.patch) {
moduleDefinition.patch(coreTracing);
}
}
}
catch (error) {
index_js_1.Logger.getInstance().warn("Failed to enable Azure SDK tracing for ESM applications", error);
}
}
}
exports.TraceHandler = TraceHandler;
//# sourceMappingURL=handler.js.map