UNPKG

logs-interceptor

Version:

High-performance, production-ready log interceptor for Node.js applications with Loki integration. Built with Clean Architecture principles. Supports Node.js, Browser, and Node-RED.

99 lines 3.63 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextService = void 0; /** * Infrastructure: ContextService * Manages async context and tracing information */ const async_hooks_1 = require("async_hooks"); const api_1 = require("@opentelemetry/api"); const os = __importStar(require("os")); class ContextService { constructor(config) { this.config = config; this.asyncStorage = new async_hooks_1.AsyncLocalStorage(); } getContext() { var _a; return (_a = this.asyncStorage.getStore()) !== null && _a !== void 0 ? _a : {}; } getDynamicLabels() { var _a, _b; const labels = { app: this.config.appName, version: this.config.version, environment: this.config.environment, hostname: os.hostname(), pid: String(process.pid), ...this.config.labels, }; // Add OpenTelemetry trace/span IDs try { const span = api_1.trace.getSpan(api_1.context.active()); if (span) { const spanContext = span.spanContext(); labels.trace_id = (_a = spanContext.traceId) !== null && _a !== void 0 ? _a : 'undefined'; labels.span_id = (_b = spanContext.spanId) !== null && _b !== void 0 ? _b : 'undefined'; } } catch { // Ignore errors } // Add request ID from async context const store = this.asyncStorage.getStore(); if (store === null || store === void 0 ? void 0 : store.requestId) { labels.request_id = String(store.requestId); } // Add custom dynamic labels if (this.config.dynamicLabels) { for (const [key, fn] of Object.entries(this.config.dynamicLabels)) { try { labels[key] = String(fn()); } catch { labels[key] = 'error'; } } } return labels; } getMetadata() { const memUsage = process.memoryUsage(); const cpuUsage = process.cpuUsage(); return { memoryUsage: memUsage.heapUsed / 1024 / 1024, cpuUsage: cpuUsage.user / 1000000, // seconds }; } runWithContext(context, fn) { return this.asyncStorage.run(context, fn); } async runWithContextAsync(context, fn) { return this.asyncStorage.run(context, fn); } } exports.ContextService = ContextService; //# sourceMappingURL=ContextService.js.map