apitally 0.11.5 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/common/client.cjs +116 -105
- package/dist/common/client.cjs.map +1 -1
- package/dist/common/client.js +111 -100
- package/dist/common/client.js.map +1 -1
- package/dist/common/requestLogger.cjs +55 -2
- package/dist/common/requestLogger.cjs.map +1 -1
- package/dist/common/requestLogger.d.cts +2 -1
- package/dist/common/requestLogger.d.ts +2 -1
- package/dist/common/requestLogger.js +55 -2
- package/dist/common/requestLogger.js.map +1 -1
- package/dist/common/sentry.cjs +55 -0
- package/dist/common/sentry.cjs.map +1 -0
- package/dist/common/sentry.d.cts +6 -0
- package/dist/common/sentry.d.ts +6 -0
- package/dist/common/sentry.js +22 -0
- package/dist/common/sentry.js.map +1 -0
- package/dist/common/serverErrorCounter.cjs +58 -45
- package/dist/common/serverErrorCounter.cjs.map +1 -1
- package/dist/common/serverErrorCounter.d.cts +3 -6
- package/dist/common/serverErrorCounter.d.ts +3 -6
- package/dist/common/serverErrorCounter.js +53 -45
- package/dist/common/serverErrorCounter.js.map +1 -1
- package/dist/express/index.cjs +117 -106
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.js +112 -101
- package/dist/express/index.js.map +1 -1
- package/dist/express/middleware.cjs +117 -106
- package/dist/express/middleware.cjs.map +1 -1
- package/dist/express/middleware.js +112 -101
- package/dist/express/middleware.js.map +1 -1
- package/dist/fastify/index.cjs +117 -106
- package/dist/fastify/index.cjs.map +1 -1
- package/dist/fastify/index.js +112 -101
- package/dist/fastify/index.js.map +1 -1
- package/dist/fastify/plugin.cjs +117 -106
- package/dist/fastify/plugin.cjs.map +1 -1
- package/dist/fastify/plugin.js +112 -101
- package/dist/fastify/plugin.js.map +1 -1
- package/dist/hono/index.cjs +117 -106
- package/dist/hono/index.cjs.map +1 -1
- package/dist/hono/index.js +112 -101
- package/dist/hono/index.js.map +1 -1
- package/dist/hono/middleware.cjs +117 -106
- package/dist/hono/middleware.cjs.map +1 -1
- package/dist/hono/middleware.js +112 -101
- package/dist/hono/middleware.js.map +1 -1
- package/dist/koa/index.cjs +119 -106
- package/dist/koa/index.cjs.map +1 -1
- package/dist/koa/index.js +114 -101
- package/dist/koa/index.js.map +1 -1
- package/dist/koa/middleware.cjs +119 -106
- package/dist/koa/middleware.cjs.map +1 -1
- package/dist/koa/middleware.js +114 -101
- package/dist/koa/middleware.js.map +1 -1
- package/dist/nestjs/index.cjs +117 -106
- package/dist/nestjs/index.cjs.map +1 -1
- package/dist/nestjs/index.js +112 -101
- package/dist/nestjs/index.js.map +1 -1
- package/package.json +1 -1
package/dist/fastify/index.cjs
CHANGED
|
@@ -213,14 +213,116 @@ var RequestCounter = _RequestCounter;
|
|
|
213
213
|
// src/common/requestLogger.ts
|
|
214
214
|
var import_async_lock = __toESM(require("async-lock"), 1);
|
|
215
215
|
var import_buffer2 = require("buffer");
|
|
216
|
-
var
|
|
216
|
+
var import_crypto3 = require("crypto");
|
|
217
217
|
var import_fs2 = require("fs");
|
|
218
218
|
var import_os2 = require("os");
|
|
219
219
|
var import_path2 = require("path");
|
|
220
220
|
|
|
221
|
+
// src/common/sentry.ts
|
|
222
|
+
var sentry;
|
|
223
|
+
(async () => {
|
|
224
|
+
try {
|
|
225
|
+
sentry = await import("@sentry/node");
|
|
226
|
+
} catch (e) {
|
|
227
|
+
}
|
|
228
|
+
})();
|
|
229
|
+
function getSentryEventId() {
|
|
230
|
+
if (sentry && sentry.lastEventId) {
|
|
231
|
+
return sentry.lastEventId();
|
|
232
|
+
}
|
|
233
|
+
return void 0;
|
|
234
|
+
}
|
|
235
|
+
__name(getSentryEventId, "getSentryEventId");
|
|
236
|
+
|
|
237
|
+
// src/common/serverErrorCounter.ts
|
|
238
|
+
var import_crypto = require("crypto");
|
|
239
|
+
var MAX_MSG_LENGTH = 2048;
|
|
240
|
+
var MAX_STACKTRACE_LENGTH = 65536;
|
|
241
|
+
var _ServerErrorCounter = class _ServerErrorCounter {
|
|
242
|
+
errorCounts;
|
|
243
|
+
errorDetails;
|
|
244
|
+
sentryEventIds;
|
|
245
|
+
constructor() {
|
|
246
|
+
this.errorCounts = /* @__PURE__ */ new Map();
|
|
247
|
+
this.errorDetails = /* @__PURE__ */ new Map();
|
|
248
|
+
this.sentryEventIds = /* @__PURE__ */ new Map();
|
|
249
|
+
}
|
|
250
|
+
addServerError(serverError) {
|
|
251
|
+
const key = this.getKey(serverError);
|
|
252
|
+
if (!this.errorDetails.has(key)) {
|
|
253
|
+
this.errorDetails.set(key, serverError);
|
|
254
|
+
}
|
|
255
|
+
this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
|
|
256
|
+
const sentryEventId = getSentryEventId();
|
|
257
|
+
if (sentryEventId) {
|
|
258
|
+
this.sentryEventIds.set(key, sentryEventId);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
getAndResetServerErrors() {
|
|
262
|
+
const data = [];
|
|
263
|
+
this.errorCounts.forEach((count, key) => {
|
|
264
|
+
const serverError = this.errorDetails.get(key);
|
|
265
|
+
if (serverError) {
|
|
266
|
+
data.push({
|
|
267
|
+
consumer: serverError.consumer || null,
|
|
268
|
+
method: serverError.method,
|
|
269
|
+
path: serverError.path,
|
|
270
|
+
type: serverError.type,
|
|
271
|
+
msg: truncateExceptionMessage(serverError.msg),
|
|
272
|
+
traceback: truncateExceptionStackTrace(serverError.traceback),
|
|
273
|
+
sentry_event_id: this.sentryEventIds.get(key) || null,
|
|
274
|
+
error_count: count
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
this.errorCounts.clear();
|
|
279
|
+
this.errorDetails.clear();
|
|
280
|
+
return data;
|
|
281
|
+
}
|
|
282
|
+
getKey(serverError) {
|
|
283
|
+
const hashInput = [
|
|
284
|
+
serverError.consumer || "",
|
|
285
|
+
serverError.method.toUpperCase(),
|
|
286
|
+
serverError.path,
|
|
287
|
+
serverError.type,
|
|
288
|
+
serverError.msg.trim(),
|
|
289
|
+
serverError.traceback.trim()
|
|
290
|
+
].join("|");
|
|
291
|
+
return (0, import_crypto.createHash)("md5").update(hashInput).digest("hex");
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
__name(_ServerErrorCounter, "ServerErrorCounter");
|
|
295
|
+
var ServerErrorCounter = _ServerErrorCounter;
|
|
296
|
+
function truncateExceptionMessage(msg) {
|
|
297
|
+
if (msg.length <= MAX_MSG_LENGTH) {
|
|
298
|
+
return msg;
|
|
299
|
+
}
|
|
300
|
+
const suffix = "... (truncated)";
|
|
301
|
+
const cutoff = MAX_MSG_LENGTH - suffix.length;
|
|
302
|
+
return msg.substring(0, cutoff) + suffix;
|
|
303
|
+
}
|
|
304
|
+
__name(truncateExceptionMessage, "truncateExceptionMessage");
|
|
305
|
+
function truncateExceptionStackTrace(stack) {
|
|
306
|
+
const suffix = "... (truncated) ...";
|
|
307
|
+
const cutoff = MAX_STACKTRACE_LENGTH - suffix.length;
|
|
308
|
+
const lines = stack.trim().split("\n");
|
|
309
|
+
const truncatedLines = [];
|
|
310
|
+
let length = 0;
|
|
311
|
+
for (const line of lines) {
|
|
312
|
+
if (length + line.length + 1 > cutoff) {
|
|
313
|
+
truncatedLines.push(suffix);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
truncatedLines.push(line);
|
|
317
|
+
length += line.length + 1;
|
|
318
|
+
}
|
|
319
|
+
return truncatedLines.join("\n");
|
|
320
|
+
}
|
|
321
|
+
__name(truncateExceptionStackTrace, "truncateExceptionStackTrace");
|
|
322
|
+
|
|
221
323
|
// src/common/tempGzipFile.ts
|
|
222
324
|
var import_buffer = require("buffer");
|
|
223
|
-
var
|
|
325
|
+
var import_crypto2 = require("crypto");
|
|
224
326
|
var import_fs = require("fs");
|
|
225
327
|
var import_os = require("os");
|
|
226
328
|
var import_path = require("path");
|
|
@@ -233,7 +335,7 @@ var _TempGzipFile = class _TempGzipFile {
|
|
|
233
335
|
readyPromise;
|
|
234
336
|
closedPromise;
|
|
235
337
|
constructor() {
|
|
236
|
-
this.uuid = (0,
|
|
338
|
+
this.uuid = (0, import_crypto2.randomUUID)();
|
|
237
339
|
this.filePath = (0, import_path.join)((0, import_os.tmpdir)(), `apitally-${this.uuid}.gz`);
|
|
238
340
|
this.writeStream = (0, import_fs.createWriteStream)(this.filePath);
|
|
239
341
|
this.readyPromise = new Promise((resolve, reject) => {
|
|
@@ -340,6 +442,7 @@ var DEFAULT_CONFIG = {
|
|
|
340
442
|
logRequestBody: false,
|
|
341
443
|
logResponseHeaders: true,
|
|
342
444
|
logResponseBody: false,
|
|
445
|
+
logException: true,
|
|
343
446
|
maskQueryParams: [],
|
|
344
447
|
maskHeaders: [],
|
|
345
448
|
excludePaths: []
|
|
@@ -412,7 +515,7 @@ var _RequestLogger = class _RequestLogger {
|
|
|
412
515
|
this.shouldMaskHeader(k) ? MASKED : v
|
|
413
516
|
]);
|
|
414
517
|
}
|
|
415
|
-
logRequest(request, response) {
|
|
518
|
+
logRequest(request, response, error) {
|
|
416
519
|
var _a2, _b, _c;
|
|
417
520
|
if (!this.enabled || this.suspendUntil !== null) return;
|
|
418
521
|
const url = new URL(request.url);
|
|
@@ -458,9 +561,15 @@ var _RequestLogger = class _RequestLogger {
|
|
|
458
561
|
request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
|
|
459
562
|
response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
|
|
460
563
|
const item = {
|
|
461
|
-
uuid: (0,
|
|
564
|
+
uuid: (0, import_crypto3.randomUUID)(),
|
|
462
565
|
request: skipEmptyValues(request),
|
|
463
|
-
response: skipEmptyValues(response)
|
|
566
|
+
response: skipEmptyValues(response),
|
|
567
|
+
exception: error && this.config.logException ? {
|
|
568
|
+
type: error.name,
|
|
569
|
+
message: truncateExceptionMessage(error.message),
|
|
570
|
+
stacktrace: truncateExceptionStackTrace(error.stack || ""),
|
|
571
|
+
sentryEventId: getSentryEventId()
|
|
572
|
+
} : null
|
|
464
573
|
};
|
|
465
574
|
[
|
|
466
575
|
item.request.body,
|
|
@@ -612,7 +721,7 @@ function skipEmptyValues(data) {
|
|
|
612
721
|
__name(skipEmptyValues, "skipEmptyValues");
|
|
613
722
|
function checkWritableFs() {
|
|
614
723
|
try {
|
|
615
|
-
const testPath = (0, import_path2.join)((0, import_os2.tmpdir)(), `apitally-${(0,
|
|
724
|
+
const testPath = (0, import_path2.join)((0, import_os2.tmpdir)(), `apitally-${(0, import_crypto3.randomUUID)()}`);
|
|
616
725
|
(0, import_fs2.writeFileSync)(testPath, "test");
|
|
617
726
|
(0, import_fs2.unlinkSync)(testPath);
|
|
618
727
|
return true;
|
|
@@ -622,104 +731,6 @@ function checkWritableFs() {
|
|
|
622
731
|
}
|
|
623
732
|
__name(checkWritableFs, "checkWritableFs");
|
|
624
733
|
|
|
625
|
-
// src/common/serverErrorCounter.ts
|
|
626
|
-
var import_crypto3 = require("crypto");
|
|
627
|
-
var MAX_MSG_LENGTH = 2048;
|
|
628
|
-
var MAX_STACKTRACE_LENGTH = 65536;
|
|
629
|
-
var _ServerErrorCounter = class _ServerErrorCounter {
|
|
630
|
-
errorCounts;
|
|
631
|
-
errorDetails;
|
|
632
|
-
sentryEventIds;
|
|
633
|
-
sentry;
|
|
634
|
-
constructor() {
|
|
635
|
-
this.errorCounts = /* @__PURE__ */ new Map();
|
|
636
|
-
this.errorDetails = /* @__PURE__ */ new Map();
|
|
637
|
-
this.sentryEventIds = /* @__PURE__ */ new Map();
|
|
638
|
-
this.tryImportSentry();
|
|
639
|
-
}
|
|
640
|
-
addServerError(serverError) {
|
|
641
|
-
const key = this.getKey(serverError);
|
|
642
|
-
if (!this.errorDetails.has(key)) {
|
|
643
|
-
this.errorDetails.set(key, serverError);
|
|
644
|
-
}
|
|
645
|
-
this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
|
|
646
|
-
this.captureSentryEventId(key);
|
|
647
|
-
}
|
|
648
|
-
getAndResetServerErrors() {
|
|
649
|
-
const data = [];
|
|
650
|
-
this.errorCounts.forEach((count, key) => {
|
|
651
|
-
const serverError = this.errorDetails.get(key);
|
|
652
|
-
if (serverError) {
|
|
653
|
-
data.push({
|
|
654
|
-
consumer: serverError.consumer || null,
|
|
655
|
-
method: serverError.method,
|
|
656
|
-
path: serverError.path,
|
|
657
|
-
type: serverError.type,
|
|
658
|
-
msg: this.getTruncatedMessage(serverError.msg),
|
|
659
|
-
traceback: this.getTruncatedStack(serverError.traceback),
|
|
660
|
-
sentry_event_id: this.sentryEventIds.get(key) || null,
|
|
661
|
-
error_count: count
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
});
|
|
665
|
-
this.errorCounts.clear();
|
|
666
|
-
this.errorDetails.clear();
|
|
667
|
-
return data;
|
|
668
|
-
}
|
|
669
|
-
getKey(serverError) {
|
|
670
|
-
const hashInput = [
|
|
671
|
-
serverError.consumer || "",
|
|
672
|
-
serverError.method.toUpperCase(),
|
|
673
|
-
serverError.path,
|
|
674
|
-
serverError.type,
|
|
675
|
-
serverError.msg.trim(),
|
|
676
|
-
serverError.traceback.trim()
|
|
677
|
-
].join("|");
|
|
678
|
-
return (0, import_crypto3.createHash)("md5").update(hashInput).digest("hex");
|
|
679
|
-
}
|
|
680
|
-
getTruncatedMessage(msg) {
|
|
681
|
-
msg = msg.trim();
|
|
682
|
-
if (msg.length <= MAX_MSG_LENGTH) {
|
|
683
|
-
return msg;
|
|
684
|
-
}
|
|
685
|
-
const suffix = "... (truncated)";
|
|
686
|
-
const cutoff = MAX_MSG_LENGTH - suffix.length;
|
|
687
|
-
return msg.substring(0, cutoff) + suffix;
|
|
688
|
-
}
|
|
689
|
-
getTruncatedStack(stack) {
|
|
690
|
-
const suffix = "... (truncated) ...";
|
|
691
|
-
const cutoff = MAX_STACKTRACE_LENGTH - suffix.length;
|
|
692
|
-
const lines = stack.trim().split("\n");
|
|
693
|
-
const truncatedLines = [];
|
|
694
|
-
let length = 0;
|
|
695
|
-
for (const line of lines) {
|
|
696
|
-
if (length + line.length + 1 > cutoff) {
|
|
697
|
-
truncatedLines.push(suffix);
|
|
698
|
-
break;
|
|
699
|
-
}
|
|
700
|
-
truncatedLines.push(line);
|
|
701
|
-
length += line.length + 1;
|
|
702
|
-
}
|
|
703
|
-
return truncatedLines.join("\n");
|
|
704
|
-
}
|
|
705
|
-
captureSentryEventId(serverErrorKey) {
|
|
706
|
-
if (this.sentry && this.sentry.lastEventId) {
|
|
707
|
-
const eventId = this.sentry.lastEventId();
|
|
708
|
-
if (eventId) {
|
|
709
|
-
this.sentryEventIds.set(serverErrorKey, eventId);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
async tryImportSentry() {
|
|
714
|
-
try {
|
|
715
|
-
this.sentry = await import("@sentry/node");
|
|
716
|
-
} catch (e) {
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
};
|
|
720
|
-
__name(_ServerErrorCounter, "ServerErrorCounter");
|
|
721
|
-
var ServerErrorCounter = _ServerErrorCounter;
|
|
722
|
-
|
|
723
734
|
// src/common/validationErrorCounter.ts
|
|
724
735
|
var import_crypto4 = require("crypto");
|
|
725
736
|
var _ValidationErrorCounter = class _ValidationErrorCounter {
|
|
@@ -1167,7 +1178,7 @@ var apitallyPlugin = /* @__PURE__ */ __name(async (fastify, config) => {
|
|
|
1167
1178
|
headers: convertHeaders(reply.getHeaders()),
|
|
1168
1179
|
size: Number(responseSize),
|
|
1169
1180
|
body: convertBody(reply.payload, (_a2 = reply.getHeader("content-type")) == null ? void 0 : _a2.toString())
|
|
1170
|
-
});
|
|
1181
|
+
}, reply.serverError);
|
|
1171
1182
|
}
|
|
1172
1183
|
}
|
|
1173
1184
|
done();
|