UNPKG

apitally

Version:

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

147 lines (144 loc) 5.09 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/common/serverErrorCounter.ts var serverErrorCounter_exports = {}; __export(serverErrorCounter_exports, { default: () => ServerErrorCounter, truncateExceptionMessage: () => truncateExceptionMessage, truncateExceptionStackTrace: () => truncateExceptionStackTrace }); module.exports = __toCommonJS(serverErrorCounter_exports); var import_node_crypto = require("crypto"); // src/common/sentry.ts var sentry; (async () => { try { sentry = await import("@sentry/node"); } catch (e) { } })(); function getSentryEventId() { if (sentry && sentry.lastEventId) { return sentry.lastEventId(); } return void 0; } __name(getSentryEventId, "getSentryEventId"); // src/common/serverErrorCounter.ts var MAX_MSG_LENGTH = 2048; var MAX_STACKTRACE_LENGTH = 65536; var _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 = 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"); var 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