apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.
113 lines (110 loc) • 3.36 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/common/serverErrorCounter.ts
import { createHash } from "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 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");
export {
ServerErrorCounter as default,
truncateExceptionMessage,
truncateExceptionStackTrace
};
//# sourceMappingURL=serverErrorCounter.js.map