@azure/monitor-opentelemetry
Version:
Azure Monitor OpenTelemetry (Node.js)
104 lines • 5.21 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { createAzureSdkInstrumentation } from "@azure/opentelemetry-instrumentation-azure-sdk";
import { AzureMonitorTraceExporter } from "@azure/monitor-opentelemetry-exporter";
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { MongoDBInstrumentation } from "@opentelemetry/instrumentation-mongodb";
import { MySQLInstrumentation } from "@opentelemetry/instrumentation-mysql";
import { PgInstrumentation } from "@opentelemetry/instrumentation-pg";
import { RedisInstrumentation } from "@opentelemetry/instrumentation-redis";
import { RedisInstrumentation as Redis4Instrumentation } from "@opentelemetry/instrumentation-redis-4";
import { ignoreOutgoingRequestHook } from "../utils/common.js";
import { AzureMonitorSpanProcessor } from "./spanProcessor.js";
import { AzureFunctionsHook } from "./azureFnHook.js";
import { ApplicationInsightsSampler } from "./sampler.js";
/**
* Azure Monitor OpenTelemetry Trace Handler
*/
export class TraceHandler {
/**
* 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 = [];
this._aiSampler = new ApplicationInsightsSampler(this._config.samplingRatio);
this._azureExporter = new AzureMonitorTraceExporter(this._config.azureMonitorExporterOptions);
const bufferConfig = {
maxExportBatchSize: 512,
scheduledDelayMillis: 5000,
exportTimeoutMillis: 30000,
maxQueueSize: 2048,
};
this._batchSpanProcessor = new BatchSpanProcessor(this._azureExporter, bufferConfig);
this._azureSpanProcessor = new AzureMonitorSpanProcessor(this._metricHandler);
this._azureFunctionsHook = new AzureFunctionsHook();
this._initializeInstrumentations();
}
getSampler() {
return this._aiSampler;
}
getBatchSpanProcessor() {
return this._batchSpanProcessor;
}
getAzureMonitorSpanProcessor() {
return this._azureSpanProcessor;
}
getInstrumentations() {
return this._instrumentations;
}
/**
* Shutdown handler
*/
// eslint-disable-next-line @typescript-eslint/require-await
async shutdown() {
this._azureFunctionsHook.shutdown();
}
/**
* Start auto collection of telemetry
*/
_initializeInstrumentations() {
var _a, _b, _c, _d, _e, _f, _g;
if ((_a = this._config.instrumentationOptions.http) === null || _a === void 0 ? void 0 : _a.enabled) {
const httpinstrumentationOptions = this._config.instrumentationOptions
.http;
const providedIgnoreOutgoingRequestHook = httpinstrumentationOptions.ignoreOutgoingRequestHook;
const mergedIgnoreOutgoingRequestHook = (request) => {
const result = 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 HttpInstrumentation(this._config.instrumentationOptions.http));
}
if ((_b = this._config.instrumentationOptions.azureSdk) === null || _b === void 0 ? void 0 : _b.enabled) {
this._instrumentations.push(createAzureSdkInstrumentation(this._config.instrumentationOptions.azureSdk));
}
if ((_c = this._config.instrumentationOptions.mongoDb) === null || _c === void 0 ? void 0 : _c.enabled) {
this._instrumentations.push(new MongoDBInstrumentation(this._config.instrumentationOptions.mongoDb));
}
if ((_d = this._config.instrumentationOptions.mySql) === null || _d === void 0 ? void 0 : _d.enabled) {
this._instrumentations.push(new MySQLInstrumentation(this._config.instrumentationOptions.mySql));
}
if ((_e = this._config.instrumentationOptions.postgreSql) === null || _e === void 0 ? void 0 : _e.enabled) {
this._instrumentations.push(new PgInstrumentation(this._config.instrumentationOptions.postgreSql));
}
if ((_f = this._config.instrumentationOptions.redis) === null || _f === void 0 ? void 0 : _f.enabled) {
this._instrumentations.push(new RedisInstrumentation(this._config.instrumentationOptions.redis));
}
if ((_g = this._config.instrumentationOptions.redis4) === null || _g === void 0 ? void 0 : _g.enabled) {
this._instrumentations.push(new Redis4Instrumentation(this._config.instrumentationOptions.redis4));
}
}
}
//# sourceMappingURL=handler.js.map