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.
Files changed (60) hide show
  1. package/README.md +2 -2
  2. package/dist/common/client.cjs +116 -105
  3. package/dist/common/client.cjs.map +1 -1
  4. package/dist/common/client.js +111 -100
  5. package/dist/common/client.js.map +1 -1
  6. package/dist/common/requestLogger.cjs +55 -2
  7. package/dist/common/requestLogger.cjs.map +1 -1
  8. package/dist/common/requestLogger.d.cts +2 -1
  9. package/dist/common/requestLogger.d.ts +2 -1
  10. package/dist/common/requestLogger.js +55 -2
  11. package/dist/common/requestLogger.js.map +1 -1
  12. package/dist/common/sentry.cjs +55 -0
  13. package/dist/common/sentry.cjs.map +1 -0
  14. package/dist/common/sentry.d.cts +6 -0
  15. package/dist/common/sentry.d.ts +6 -0
  16. package/dist/common/sentry.js +22 -0
  17. package/dist/common/sentry.js.map +1 -0
  18. package/dist/common/serverErrorCounter.cjs +58 -45
  19. package/dist/common/serverErrorCounter.cjs.map +1 -1
  20. package/dist/common/serverErrorCounter.d.cts +3 -6
  21. package/dist/common/serverErrorCounter.d.ts +3 -6
  22. package/dist/common/serverErrorCounter.js +53 -45
  23. package/dist/common/serverErrorCounter.js.map +1 -1
  24. package/dist/express/index.cjs +117 -106
  25. package/dist/express/index.cjs.map +1 -1
  26. package/dist/express/index.js +112 -101
  27. package/dist/express/index.js.map +1 -1
  28. package/dist/express/middleware.cjs +117 -106
  29. package/dist/express/middleware.cjs.map +1 -1
  30. package/dist/express/middleware.js +112 -101
  31. package/dist/express/middleware.js.map +1 -1
  32. package/dist/fastify/index.cjs +117 -106
  33. package/dist/fastify/index.cjs.map +1 -1
  34. package/dist/fastify/index.js +112 -101
  35. package/dist/fastify/index.js.map +1 -1
  36. package/dist/fastify/plugin.cjs +117 -106
  37. package/dist/fastify/plugin.cjs.map +1 -1
  38. package/dist/fastify/plugin.js +112 -101
  39. package/dist/fastify/plugin.js.map +1 -1
  40. package/dist/hono/index.cjs +117 -106
  41. package/dist/hono/index.cjs.map +1 -1
  42. package/dist/hono/index.js +112 -101
  43. package/dist/hono/index.js.map +1 -1
  44. package/dist/hono/middleware.cjs +117 -106
  45. package/dist/hono/middleware.cjs.map +1 -1
  46. package/dist/hono/middleware.js +112 -101
  47. package/dist/hono/middleware.js.map +1 -1
  48. package/dist/koa/index.cjs +119 -106
  49. package/dist/koa/index.cjs.map +1 -1
  50. package/dist/koa/index.js +114 -101
  51. package/dist/koa/index.js.map +1 -1
  52. package/dist/koa/middleware.cjs +119 -106
  53. package/dist/koa/middleware.cjs.map +1 -1
  54. package/dist/koa/middleware.js +114 -101
  55. package/dist/koa/middleware.js.map +1 -1
  56. package/dist/nestjs/index.cjs +117 -106
  57. package/dist/nestjs/index.cjs.map +1 -1
  58. package/dist/nestjs/index.js +112 -101
  59. package/dist/nestjs/index.js.map +1 -1
  60. package/package.json +1 -1
@@ -215,14 +215,116 @@ var RequestCounter = _RequestCounter;
215
215
  // src/common/requestLogger.ts
216
216
  var import_async_lock = __toESM(require("async-lock"), 1);
217
217
  var import_buffer2 = require("buffer");
218
- var import_crypto2 = require("crypto");
218
+ var import_crypto3 = require("crypto");
219
219
  var import_fs2 = require("fs");
220
220
  var import_os2 = require("os");
221
221
  var import_path2 = require("path");
222
222
 
223
+ // src/common/sentry.ts
224
+ var sentry;
225
+ (async () => {
226
+ try {
227
+ sentry = await import("@sentry/node");
228
+ } catch (e) {
229
+ }
230
+ })();
231
+ function getSentryEventId() {
232
+ if (sentry && sentry.lastEventId) {
233
+ return sentry.lastEventId();
234
+ }
235
+ return void 0;
236
+ }
237
+ __name(getSentryEventId, "getSentryEventId");
238
+
239
+ // src/common/serverErrorCounter.ts
240
+ var import_crypto = require("crypto");
241
+ var MAX_MSG_LENGTH = 2048;
242
+ var MAX_STACKTRACE_LENGTH = 65536;
243
+ var _ServerErrorCounter = class _ServerErrorCounter {
244
+ errorCounts;
245
+ errorDetails;
246
+ sentryEventIds;
247
+ constructor() {
248
+ this.errorCounts = /* @__PURE__ */ new Map();
249
+ this.errorDetails = /* @__PURE__ */ new Map();
250
+ this.sentryEventIds = /* @__PURE__ */ new Map();
251
+ }
252
+ addServerError(serverError) {
253
+ const key = this.getKey(serverError);
254
+ if (!this.errorDetails.has(key)) {
255
+ this.errorDetails.set(key, serverError);
256
+ }
257
+ this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
258
+ const sentryEventId = getSentryEventId();
259
+ if (sentryEventId) {
260
+ this.sentryEventIds.set(key, sentryEventId);
261
+ }
262
+ }
263
+ getAndResetServerErrors() {
264
+ const data = [];
265
+ this.errorCounts.forEach((count, key) => {
266
+ const serverError = this.errorDetails.get(key);
267
+ if (serverError) {
268
+ data.push({
269
+ consumer: serverError.consumer || null,
270
+ method: serverError.method,
271
+ path: serverError.path,
272
+ type: serverError.type,
273
+ msg: truncateExceptionMessage(serverError.msg),
274
+ traceback: truncateExceptionStackTrace(serverError.traceback),
275
+ sentry_event_id: this.sentryEventIds.get(key) || null,
276
+ error_count: count
277
+ });
278
+ }
279
+ });
280
+ this.errorCounts.clear();
281
+ this.errorDetails.clear();
282
+ return data;
283
+ }
284
+ getKey(serverError) {
285
+ const hashInput = [
286
+ serverError.consumer || "",
287
+ serverError.method.toUpperCase(),
288
+ serverError.path,
289
+ serverError.type,
290
+ serverError.msg.trim(),
291
+ serverError.traceback.trim()
292
+ ].join("|");
293
+ return (0, import_crypto.createHash)("md5").update(hashInput).digest("hex");
294
+ }
295
+ };
296
+ __name(_ServerErrorCounter, "ServerErrorCounter");
297
+ var ServerErrorCounter = _ServerErrorCounter;
298
+ function truncateExceptionMessage(msg) {
299
+ if (msg.length <= MAX_MSG_LENGTH) {
300
+ return msg;
301
+ }
302
+ const suffix = "... (truncated)";
303
+ const cutoff = MAX_MSG_LENGTH - suffix.length;
304
+ return msg.substring(0, cutoff) + suffix;
305
+ }
306
+ __name(truncateExceptionMessage, "truncateExceptionMessage");
307
+ function truncateExceptionStackTrace(stack) {
308
+ const suffix = "... (truncated) ...";
309
+ const cutoff = MAX_STACKTRACE_LENGTH - suffix.length;
310
+ const lines = stack.trim().split("\n");
311
+ const truncatedLines = [];
312
+ let length = 0;
313
+ for (const line of lines) {
314
+ if (length + line.length + 1 > cutoff) {
315
+ truncatedLines.push(suffix);
316
+ break;
317
+ }
318
+ truncatedLines.push(line);
319
+ length += line.length + 1;
320
+ }
321
+ return truncatedLines.join("\n");
322
+ }
323
+ __name(truncateExceptionStackTrace, "truncateExceptionStackTrace");
324
+
223
325
  // src/common/tempGzipFile.ts
224
326
  var import_buffer = require("buffer");
225
- var import_crypto = require("crypto");
327
+ var import_crypto2 = require("crypto");
226
328
  var import_fs = require("fs");
227
329
  var import_os = require("os");
228
330
  var import_path = require("path");
@@ -235,7 +337,7 @@ var _TempGzipFile = class _TempGzipFile {
235
337
  readyPromise;
236
338
  closedPromise;
237
339
  constructor() {
238
- this.uuid = (0, import_crypto.randomUUID)();
340
+ this.uuid = (0, import_crypto2.randomUUID)();
239
341
  this.filePath = (0, import_path.join)((0, import_os.tmpdir)(), `apitally-${this.uuid}.gz`);
240
342
  this.writeStream = (0, import_fs.createWriteStream)(this.filePath);
241
343
  this.readyPromise = new Promise((resolve, reject) => {
@@ -342,6 +444,7 @@ var DEFAULT_CONFIG = {
342
444
  logRequestBody: false,
343
445
  logResponseHeaders: true,
344
446
  logResponseBody: false,
447
+ logException: true,
345
448
  maskQueryParams: [],
346
449
  maskHeaders: [],
347
450
  excludePaths: []
@@ -414,7 +517,7 @@ var _RequestLogger = class _RequestLogger {
414
517
  this.shouldMaskHeader(k) ? MASKED : v
415
518
  ]);
416
519
  }
417
- logRequest(request, response) {
520
+ logRequest(request, response, error) {
418
521
  var _a3, _b, _c;
419
522
  if (!this.enabled || this.suspendUntil !== null) return;
420
523
  const url = new URL(request.url);
@@ -460,9 +563,15 @@ var _RequestLogger = class _RequestLogger {
460
563
  request.headers = this.config.logRequestHeaders ? this.maskHeaders(request.headers) : [];
461
564
  response.headers = this.config.logResponseHeaders ? this.maskHeaders(response.headers) : [];
462
565
  const item = {
463
- uuid: (0, import_crypto2.randomUUID)(),
566
+ uuid: (0, import_crypto3.randomUUID)(),
464
567
  request: skipEmptyValues(request),
465
- response: skipEmptyValues(response)
568
+ response: skipEmptyValues(response),
569
+ exception: error && this.config.logException ? {
570
+ type: error.name,
571
+ message: truncateExceptionMessage(error.message),
572
+ stacktrace: truncateExceptionStackTrace(error.stack || ""),
573
+ sentryEventId: getSentryEventId()
574
+ } : null
466
575
  };
467
576
  [
468
577
  item.request.body,
@@ -614,7 +723,7 @@ function skipEmptyValues(data) {
614
723
  __name(skipEmptyValues, "skipEmptyValues");
615
724
  function checkWritableFs() {
616
725
  try {
617
- const testPath = (0, import_path2.join)((0, import_os2.tmpdir)(), `apitally-${(0, import_crypto2.randomUUID)()}`);
726
+ const testPath = (0, import_path2.join)((0, import_os2.tmpdir)(), `apitally-${(0, import_crypto3.randomUUID)()}`);
618
727
  (0, import_fs2.writeFileSync)(testPath, "test");
619
728
  (0, import_fs2.unlinkSync)(testPath);
620
729
  return true;
@@ -624,104 +733,6 @@ function checkWritableFs() {
624
733
  }
625
734
  __name(checkWritableFs, "checkWritableFs");
626
735
 
627
- // src/common/serverErrorCounter.ts
628
- var import_crypto3 = require("crypto");
629
- var MAX_MSG_LENGTH = 2048;
630
- var MAX_STACKTRACE_LENGTH = 65536;
631
- var _ServerErrorCounter = class _ServerErrorCounter {
632
- errorCounts;
633
- errorDetails;
634
- sentryEventIds;
635
- sentry;
636
- constructor() {
637
- this.errorCounts = /* @__PURE__ */ new Map();
638
- this.errorDetails = /* @__PURE__ */ new Map();
639
- this.sentryEventIds = /* @__PURE__ */ new Map();
640
- this.tryImportSentry();
641
- }
642
- addServerError(serverError) {
643
- const key = this.getKey(serverError);
644
- if (!this.errorDetails.has(key)) {
645
- this.errorDetails.set(key, serverError);
646
- }
647
- this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
648
- this.captureSentryEventId(key);
649
- }
650
- getAndResetServerErrors() {
651
- const data = [];
652
- this.errorCounts.forEach((count, key) => {
653
- const serverError = this.errorDetails.get(key);
654
- if (serverError) {
655
- data.push({
656
- consumer: serverError.consumer || null,
657
- method: serverError.method,
658
- path: serverError.path,
659
- type: serverError.type,
660
- msg: this.getTruncatedMessage(serverError.msg),
661
- traceback: this.getTruncatedStack(serverError.traceback),
662
- sentry_event_id: this.sentryEventIds.get(key) || null,
663
- error_count: count
664
- });
665
- }
666
- });
667
- this.errorCounts.clear();
668
- this.errorDetails.clear();
669
- return data;
670
- }
671
- getKey(serverError) {
672
- const hashInput = [
673
- serverError.consumer || "",
674
- serverError.method.toUpperCase(),
675
- serverError.path,
676
- serverError.type,
677
- serverError.msg.trim(),
678
- serverError.traceback.trim()
679
- ].join("|");
680
- return (0, import_crypto3.createHash)("md5").update(hashInput).digest("hex");
681
- }
682
- getTruncatedMessage(msg) {
683
- msg = msg.trim();
684
- if (msg.length <= MAX_MSG_LENGTH) {
685
- return msg;
686
- }
687
- const suffix = "... (truncated)";
688
- const cutoff = MAX_MSG_LENGTH - suffix.length;
689
- return msg.substring(0, cutoff) + suffix;
690
- }
691
- getTruncatedStack(stack) {
692
- const suffix = "... (truncated) ...";
693
- const cutoff = MAX_STACKTRACE_LENGTH - suffix.length;
694
- const lines = stack.trim().split("\n");
695
- const truncatedLines = [];
696
- let length = 0;
697
- for (const line of lines) {
698
- if (length + line.length + 1 > cutoff) {
699
- truncatedLines.push(suffix);
700
- break;
701
- }
702
- truncatedLines.push(line);
703
- length += line.length + 1;
704
- }
705
- return truncatedLines.join("\n");
706
- }
707
- captureSentryEventId(serverErrorKey) {
708
- if (this.sentry && this.sentry.lastEventId) {
709
- const eventId = this.sentry.lastEventId();
710
- if (eventId) {
711
- this.sentryEventIds.set(serverErrorKey, eventId);
712
- }
713
- }
714
- }
715
- async tryImportSentry() {
716
- try {
717
- this.sentry = await import("@sentry/node");
718
- } catch (e) {
719
- }
720
- }
721
- };
722
- __name(_ServerErrorCounter, "ServerErrorCounter");
723
- var ServerErrorCounter = _ServerErrorCounter;
724
-
725
736
  // src/common/validationErrorCounter.ts
726
737
  var import_crypto4 = require("crypto");
727
738
  var _ValidationErrorCounter = class _ValidationErrorCounter {
@@ -1293,7 +1304,7 @@ var getMiddleware = /* @__PURE__ */ __name((app, client) => {
1293
1304
  headers: convertHeaders(res.getHeaders()),
1294
1305
  size: Number(res.get("content-length")),
1295
1306
  body: convertBody(res.locals.body, res.get("content-type"))
1296
- });
1307
+ }, res.locals.serverError);
1297
1308
  }
1298
1309
  } catch (error) {
1299
1310
  client.logger.error("Error while logging request in Apitally middleware.", {