@vtex/diagnostics-nodejs
Version:
Diagnostics library for Node.js applications
187 lines • 7.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonInstrumentations = exports.createCompositePropagator = exports.HttpInstrumentation = exports.NetInstrumentation = exports.Middlewares = exports.Propagation = void 0;
exports.ExpressInstrumentation = getExpressInstrumentation;
exports.KoaInstrumentation = getKoaInstrumentation;
exports.NestInstrumentation = getNestInstrumentation;
exports.setupInstrumentations = setupInstrumentations;
const tslib_1 = require("tslib");
exports.Propagation = tslib_1.__importStar(require("./propagation"));
exports.Middlewares = tslib_1.__importStar(require("./middlewares"));
var instrumentation_net_1 = require("@opentelemetry/instrumentation-net");
Object.defineProperty(exports, "NetInstrumentation", { enumerable: true, get: function () { return instrumentation_net_1.NetInstrumentation; } });
var instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
Object.defineProperty(exports, "HttpInstrumentation", { enumerable: true, get: function () { return instrumentation_http_1.HttpInstrumentation; } });
const instrumentation_1 = require("@opentelemetry/instrumentation");
let ExpressInstrumentation = null;
let KoaInstrumentation = null;
let NestInstrumentation = null;
function getExpressInstrumentation() {
if (ExpressInstrumentation === null) {
try {
ExpressInstrumentation = require('@opentelemetry/instrumentation-express').ExpressInstrumentation;
}
catch {
ExpressInstrumentation = false;
}
}
return ExpressInstrumentation;
}
function getKoaInstrumentation() {
if (KoaInstrumentation === null) {
try {
KoaInstrumentation = require('@opentelemetry/instrumentation-koa').KoaInstrumentation;
}
catch {
KoaInstrumentation = false;
}
}
return KoaInstrumentation;
}
function getNestInstrumentation() {
if (NestInstrumentation === null) {
try {
NestInstrumentation = require('@opentelemetry/instrumentation-nestjs-core').NestInstrumentation;
}
catch {
NestInstrumentation = false;
}
}
return NestInstrumentation;
}
var propagation_1 = require("./propagation");
Object.defineProperty(exports, "createCompositePropagator", { enumerable: true, get: function () { return propagation_1.createCompositePropagator; } });
const instrumentation_net_2 = require("@opentelemetry/instrumentation-net");
const instrumentation_http_2 = require("@opentelemetry/instrumentation-http");
function setupInstrumentations(instrumentations) {
if (instrumentations.length === 0) {
console.warn('[Diagnostics] No instrumentations provided!');
return;
}
const flatInstrumentations = Array.isArray(instrumentations)
? instrumentations.flat ? instrumentations.flat() : instrumentations
: [instrumentations];
// We assume that we have global provider set up already
(0, instrumentation_1.registerInstrumentations)({
instrumentations: flatInstrumentations
});
}
exports.CommonInstrumentations = {
express: () => {
const instrumentations = [
new instrumentation_http_2.HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
const url = req.url || '';
return url.includes('/health') ||
url.includes('/metrics') ||
url.includes('/favicon.ico') ||
url.includes('/ping');
},
}),
new instrumentation_net_2.NetInstrumentation(),
];
const ExpressInstr = getExpressInstrumentation();
if (ExpressInstr) {
instrumentations.push(new ExpressInstr());
}
else {
console.warn('[CommonInstrumentations] ExpressInstrumentation not available. Install: npm install @opentelemetry/instrumentation-express');
}
return instrumentations;
},
fastify: () => {
return [
new instrumentation_http_2.HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
const url = req.url || '';
return url.includes('/health') ||
url.includes('/metrics') ||
url.includes('/favicon.ico') ||
url.includes('/ping');
},
}),
new instrumentation_net_2.NetInstrumentation(),
];
},
koa: () => {
const instrumentations = [
new instrumentation_http_2.HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
const url = req.url || '';
return url.includes('/health') ||
url.includes('/metrics') ||
url.includes('/favicon.ico') ||
url.includes('/ping');
},
}),
new instrumentation_net_2.NetInstrumentation(),
];
const KoaInstr = getKoaInstrumentation();
if (KoaInstr) {
instrumentations.push(new KoaInstr());
}
else {
console.warn('[CommonInstrumentations] KoaInstrumentation not available. Install: npm install @opentelemetry/instrumentation-koa');
}
return instrumentations;
},
nestjs: () => {
const instrumentations = [
new instrumentation_http_2.HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
const url = req.url || '';
return url.includes('/health') ||
url.includes('/metrics') ||
url.includes('/favicon.ico') ||
url.includes('/ping');
},
}),
new instrumentation_net_2.NetInstrumentation(),
];
const ExpressInstr = getExpressInstrumentation();
if (ExpressInstr) {
instrumentations.push(new ExpressInstr());
}
const NestInstr = getNestInstrumentation();
if (NestInstr) {
instrumentations.push(new NestInstr());
}
return instrumentations;
},
minimal: () => [
new instrumentation_http_2.HttpInstrumentation({
ignoreIncomingRequestHook: (req) => {
const url = req.url || '';
return url.includes('/health') ||
url.includes('/metrics') ||
url.includes('/favicon.ico') ||
url.includes('/ping');
},
}),
new instrumentation_net_2.NetInstrumentation(),
],
autoDetect: () => {
try {
require.resolve('express');
return exports.CommonInstrumentations.express();
}
catch { }
try {
require.resolve('@nestjs/core');
return exports.CommonInstrumentations.nestjs();
}
catch { }
try {
require.resolve('koa');
return exports.CommonInstrumentations.koa();
}
catch { }
try {
require.resolve('fastify');
return exports.CommonInstrumentations.fastify();
}
catch { }
return exports.CommonInstrumentations.minimal();
}
};
//# sourceMappingURL=index.js.map