UNPKG

otel-nestjs-instrumentation

Version:

Comprehensive OpenTelemetry instrumentation for NestJS applications - SQS, Kafka, HTTP/2, cron jobs and custom protocols

119 lines 4.21 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.otelInstrumentation = void 0; const otel = __importStar(require("@opentelemetry/api")); const tracer_name_1 = require("./tracer-name"); exports.otelInstrumentation = { getCurrentTransactionId() { return otel.trace.getActiveSpan()?.spanContext()?.traceId; }, create(transactionName, context, effectiveType) { const tracer = otel.trace.getTracer(tracer_name_1.tracerName); if (!tracer) return undefined; let spanContext = otel.context.active(); try { if (context.getType() === 'http') { const request = context.switchToHttp().getRequest(); if (request.headers) { spanContext = otel.propagation.extract(spanContext, request.headers); } } } catch { } let spanKind = otel.SpanKind.INTERNAL; const attributes = {}; effectiveType ??= context.getType(); if (effectiveType === 'http') { spanKind = otel.SpanKind.SERVER; try { const request = context.switchToHttp().getRequest(); attributes['http.method'] = request.method || ''; attributes['http.url'] = request.url || ''; if (request.route?.path) { attributes['http.route'] = request.route.path; } } catch { } } else if (effectiveType === 'rpc') { spanKind = otel.SpanKind.SERVER; try { attributes['rpc.method'] = context.getHandler()?.name ?? 'Call'; } catch { } } try { const controllerClass = context.getClass(); const handlerMethod = context.getHandler(); attributes['nestjs.controller'] = controllerClass?.name || 'Unknown'; attributes['nestjs.handler'] = handlerMethod?.name || 'unknown'; } catch { } const span = tracer.startSpan(transactionName, { kind: spanKind, attributes, }, spanContext); const spanContextData = span.spanContext(); const traceId = spanContextData?.traceId; return traceId; }, addAttributes(attributes) { try { const span = otel.trace.getActiveSpan(); if (span) { span.setAttributes(attributes); } } catch { } }, recordException(error) { try { const span = otel.trace.getActiveSpan(); if (span) { span.recordException(error); } } catch { } }, }; //# sourceMappingURL=otel-instrumentation.js.map