UNPKG

apitally

Version:

Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.

118 lines 4.2 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var serverErrorCounter_exports = {}; __export(serverErrorCounter_exports, { default: () => ServerErrorCounter, truncateExceptionMessage: () => truncateExceptionMessage, truncateExceptionStackTrace: () => truncateExceptionStackTrace }); module.exports = __toCommonJS(serverErrorCounter_exports); var import_node_crypto = require("node:crypto"); var import_sentry = require("./sentry.js"); const MAX_MSG_LENGTH = 2048; const MAX_STACKTRACE_LENGTH = 65536; const _ServerErrorCounter = class _ServerErrorCounter { errorCounts; errorDetails; sentryEventIds; constructor() { this.errorCounts = /* @__PURE__ */ new Map(); this.errorDetails = /* @__PURE__ */ new Map(); this.sentryEventIds = /* @__PURE__ */ new Map(); } addServerError(serverError) { const key = this.getKey(serverError); if (!this.errorDetails.has(key)) { this.errorDetails.set(key, serverError); } this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1); const sentryEventId = (0, import_sentry.getSentryEventId)(); if (sentryEventId) { this.sentryEventIds.set(key, sentryEventId); } } getAndResetServerErrors() { const data = []; this.errorCounts.forEach((count, key) => { const serverError = this.errorDetails.get(key); if (serverError) { data.push({ consumer: serverError.consumer || null, method: serverError.method, path: serverError.path, type: serverError.type, msg: truncateExceptionMessage(serverError.msg), traceback: truncateExceptionStackTrace(serverError.traceback), sentry_event_id: this.sentryEventIds.get(key) || null, error_count: count }); } }); this.errorCounts.clear(); this.errorDetails.clear(); this.sentryEventIds.clear(); return data; } getKey(serverError) { const hashInput = [ serverError.consumer || "", serverError.method.toUpperCase(), serverError.path, serverError.type, serverError.msg.trim(), serverError.traceback.trim() ].join("|"); return (0, import_node_crypto.createHash)("md5").update(hashInput).digest("hex"); } }; __name(_ServerErrorCounter, "ServerErrorCounter"); let ServerErrorCounter = _ServerErrorCounter; function truncateExceptionMessage(msg) { if (msg.length <= MAX_MSG_LENGTH) { return msg; } const suffix = "... (truncated)"; const cutoff = MAX_MSG_LENGTH - suffix.length; return msg.substring(0, cutoff) + suffix; } __name(truncateExceptionMessage, "truncateExceptionMessage"); function truncateExceptionStackTrace(stack) { const suffix = "... (truncated) ..."; const cutoff = MAX_STACKTRACE_LENGTH - suffix.length; const lines = stack.trim().split("\n"); const truncatedLines = []; let length = 0; for (const line of lines) { if (length + line.length + 1 > cutoff) { truncatedLines.push(suffix); break; } truncatedLines.push(line); length += line.length + 1; } return truncatedLines.join("\n"); } __name(truncateExceptionStackTrace, "truncateExceptionStackTrace"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { truncateExceptionMessage, truncateExceptionStackTrace }); //# sourceMappingURL=serverErrorCounter.cjs.map