@futurewindai/wotchi 0.1.0-beta.1 → 0.1.0-beta.5

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 (101) hide show
  1. package/README.md +182 -56
  2. package/SECURITY.md +2 -2
  3. package/dist/cjs/core/client.js +380 -21
  4. package/dist/cjs/core/config.js +177 -18
  5. package/dist/cjs/core/diagnostics.js +12 -1
  6. package/dist/cjs/core/group-store.js +15 -1
  7. package/dist/cjs/core/incident-builder.js +18 -0
  8. package/dist/cjs/core/incident-policy.js +2 -7
  9. package/dist/cjs/core/limits.js +13 -0
  10. package/dist/cjs/core/normalize.js +10 -5
  11. package/dist/cjs/core/notification-queue.js +20 -3
  12. package/dist/cjs/core/redact.js +83 -8
  13. package/dist/cjs/index.js +3 -1
  14. package/dist/cjs/integrations/express/error-handler.js +4 -1
  15. package/dist/cjs/integrations/express/index.js +4 -1
  16. package/dist/cjs/integrations/express/state.js +30 -0
  17. package/dist/cjs/integrations/express/status-observer.js +86 -0
  18. package/dist/cjs/integrations/nest/exception-filter.js +9 -1
  19. package/dist/cjs/integrations/nest/index.js +3 -1
  20. package/dist/cjs/integrations/nest/register.js +9 -0
  21. package/dist/cjs/integrations/request-context.js +83 -1
  22. package/dist/cjs/notifiers/alert-payload.js +20 -0
  23. package/dist/cjs/notifiers/console.js +100 -10
  24. package/dist/cjs/notifiers/telegram-format.js +75 -5
  25. package/dist/cjs/notifiers/webhook-http.js +394 -0
  26. package/dist/cjs/notifiers/webhook.js +28 -0
  27. package/dist/esm/core/client.js +380 -21
  28. package/dist/esm/core/config.js +177 -18
  29. package/dist/esm/core/diagnostics.js +12 -1
  30. package/dist/esm/core/group-store.js +15 -1
  31. package/dist/esm/core/incident-builder.js +18 -0
  32. package/dist/esm/core/incident-policy.js +2 -7
  33. package/dist/esm/core/limits.js +10 -0
  34. package/dist/esm/core/normalize.js +10 -5
  35. package/dist/esm/core/notification-queue.js +20 -3
  36. package/dist/esm/core/redact.js +83 -8
  37. package/dist/esm/index.js +2 -1
  38. package/dist/esm/integrations/express/error-handler.js +4 -1
  39. package/dist/esm/integrations/express/index.js +2 -1
  40. package/dist/esm/integrations/express/state.js +26 -0
  41. package/dist/esm/integrations/express/status-observer.js +83 -0
  42. package/dist/esm/integrations/nest/exception-filter.js +9 -1
  43. package/dist/esm/integrations/nest/index.js +2 -2
  44. package/dist/esm/integrations/nest/register.js +8 -0
  45. package/dist/esm/integrations/request-context.js +83 -1
  46. package/dist/esm/notifiers/alert-payload.js +16 -0
  47. package/dist/esm/notifiers/console.js +100 -10
  48. package/dist/esm/notifiers/telegram-format.js +75 -5
  49. package/dist/esm/notifiers/webhook-http.js +387 -0
  50. package/dist/esm/notifiers/webhook.js +24 -0
  51. package/dist/types/core/config.d.ts +7 -1
  52. package/dist/types/core/diagnostics.d.ts +5 -1
  53. package/dist/types/core/limits.d.ts +10 -0
  54. package/dist/types/core/notification-queue.d.ts +5 -1
  55. package/dist/types/core/types.d.ts +97 -1
  56. package/dist/types/index.d.ts +4 -2
  57. package/dist/types/integrations/express/index.d.ts +4 -2
  58. package/dist/types/integrations/express/state.d.ts +2 -0
  59. package/dist/types/integrations/express/status-observer.d.ts +11 -0
  60. package/dist/types/integrations/nest/index.d.ts +4 -3
  61. package/dist/types/integrations/nest/register.d.ts +3 -0
  62. package/dist/types/integrations/request-context.d.ts +3 -0
  63. package/dist/types/notifiers/alert-payload.d.ts +5 -0
  64. package/dist/types/notifiers/console.d.ts +1 -1
  65. package/dist/types/notifiers/webhook-http.d.ts +37 -0
  66. package/dist/types/notifiers/webhook.d.ts +4 -0
  67. package/dist/types-cjs/core/client.d.cts +2 -0
  68. package/dist/types-cjs/core/config.d.cts +35 -0
  69. package/dist/types-cjs/core/diagnostics.d.cts +11 -0
  70. package/dist/types-cjs/core/errors.d.cts +3 -0
  71. package/dist/types-cjs/core/fingerprint.d.cts +3 -0
  72. package/dist/types-cjs/core/group-store.d.cts +15 -0
  73. package/dist/types-cjs/core/incident-builder.d.cts +3 -0
  74. package/dist/types-cjs/core/incident-policy.d.cts +15 -0
  75. package/dist/types-cjs/core/limits.d.cts +10 -0
  76. package/dist/types-cjs/core/normalize.d.cts +17 -0
  77. package/dist/types-cjs/core/notification-queue.d.cts +20 -0
  78. package/dist/types-cjs/core/process-monitor.d.cts +5 -0
  79. package/dist/types-cjs/core/redact.d.cts +11 -0
  80. package/dist/types-cjs/core/rolling-window.d.cts +12 -0
  81. package/dist/types-cjs/core/stack-frame.d.cts +2 -0
  82. package/dist/types-cjs/core/types.d.cts +205 -0
  83. package/dist/types-cjs/index.d.cts +11 -0
  84. package/dist/types-cjs/integrations/express/error-handler.d.cts +4 -0
  85. package/dist/types-cjs/integrations/express/index.d.cts +9 -0
  86. package/dist/types-cjs/integrations/express/request-context.d.cts +5 -0
  87. package/dist/types-cjs/integrations/express/state.d.cts +2 -0
  88. package/dist/types-cjs/integrations/express/status-observer.d.cts +11 -0
  89. package/dist/types-cjs/integrations/nest/exception-filter.d.cts +10 -0
  90. package/dist/types-cjs/integrations/nest/index.d.cts +5 -0
  91. package/dist/types-cjs/integrations/nest/register.d.cts +13 -0
  92. package/dist/types-cjs/integrations/nest/request-context.d.cts +5 -0
  93. package/dist/types-cjs/integrations/request-context.d.cts +16 -0
  94. package/dist/types-cjs/notifiers/alert-payload.d.cts +5 -0
  95. package/dist/types-cjs/notifiers/console.d.cts +3 -0
  96. package/dist/types-cjs/notifiers/telegram-format.d.cts +2 -0
  97. package/dist/types-cjs/notifiers/telegram-http.d.cts +21 -0
  98. package/dist/types-cjs/notifiers/telegram.d.cts +4 -0
  99. package/dist/types-cjs/notifiers/webhook-http.d.cts +37 -0
  100. package/dist/types-cjs/notifiers/webhook.d.cts +4 -0
  101. package/package.json +28 -6
@@ -0,0 +1,83 @@
1
+ import { getExpressRequestContext } from "./request-context.js";
2
+ import { wasExpressErrorCaptured } from "./state.js";
3
+ const DEFAULT_STATUS_CODES = [401, 403, 429];
4
+ const DEFAULT_STATUS_CLASSES = ["5xx"];
5
+ const MAX_STATUS_CODES = 100;
6
+ const normalizeStatuses = (values, defaults) => {
7
+ const selected = values ?? defaults;
8
+ if (selected.length > MAX_STATUS_CODES) {
9
+ throw new TypeError("statusCodes must contain at most 100 values");
10
+ }
11
+ for (const status of selected) {
12
+ if (!Number.isSafeInteger(status) || status < 100 || status > 599) {
13
+ throw new TypeError("statusCodes must contain HTTP status codes from 100 through 599");
14
+ }
15
+ }
16
+ return new Set(selected);
17
+ };
18
+ const normalizeStatusClasses = (values) => {
19
+ const selected = values ?? DEFAULT_STATUS_CLASSES;
20
+ for (const statusClass of selected) {
21
+ if (statusClass !== "4xx" && statusClass !== "5xx") {
22
+ throw new TypeError('statusClasses must contain only "4xx" or "5xx"');
23
+ }
24
+ }
25
+ return new Set(selected);
26
+ };
27
+ const normalizeAlertThreshold = (value) => {
28
+ if (value === undefined) {
29
+ return undefined;
30
+ }
31
+ if (!Number.isSafeInteger(value) || value <= 0 || value > 1_000_000) {
32
+ throw new TypeError("alertThreshold must be a positive integer no greater than 1000000");
33
+ }
34
+ return value;
35
+ };
36
+ const normalizeOptions = (options) => {
37
+ const alertThreshold = normalizeAlertThreshold(options?.alertThreshold);
38
+ return {
39
+ ...(options?.requestIdProperty === undefined
40
+ ? {}
41
+ : { requestIdProperty: options.requestIdProperty }),
42
+ ...(options?.correlationIdProperty === undefined
43
+ ? {}
44
+ : { correlationIdProperty: options.correlationIdProperty }),
45
+ ...(options?.traceContextProperty === undefined
46
+ ? {}
47
+ : { traceContextProperty: options.traceContextProperty }),
48
+ statusCodes: normalizeStatuses(options?.statusCodes, DEFAULT_STATUS_CODES),
49
+ statusClasses: normalizeStatusClasses(options?.statusClasses),
50
+ ignoreStatusCodes: normalizeStatuses(options?.ignoreStatusCodes, []),
51
+ ...(alertThreshold === undefined ? {} : { alertThreshold }),
52
+ };
53
+ };
54
+ const isObservedStatus = (statusCode, options) => !options.ignoreStatusCodes.has(statusCode) &&
55
+ (options.statusCodes.has(statusCode) ||
56
+ (statusCode >= 400 && statusCode < 500 && options.statusClasses.has("4xx")) ||
57
+ (statusCode >= 500 && statusCode < 600 && options.statusClasses.has("5xx")));
58
+ export function wotchiStatusObserver(client, options) {
59
+ const normalizedOptions = normalizeOptions(options);
60
+ return (request, response, next) => {
61
+ response.once("finish", () => {
62
+ const statusCode = response.statusCode;
63
+ if (wasExpressErrorCaptured(request) || !isObservedStatus(statusCode, normalizedOptions)) {
64
+ return;
65
+ }
66
+ try {
67
+ const requestContext = getExpressRequestContext(request, response, normalizedOptions);
68
+ client.captureEvent({
69
+ level: "error",
70
+ message: `HTTP ${statusCode} response`,
71
+ ...(normalizedOptions.alertThreshold === undefined
72
+ ? {}
73
+ : { alertThreshold: normalizedOptions.alertThreshold }),
74
+ ...(requestContext === undefined ? {} : { request: requestContext }),
75
+ });
76
+ }
77
+ catch {
78
+ // Status observation must never change the host response lifecycle.
79
+ }
80
+ });
81
+ next();
82
+ };
83
+ }
@@ -9,6 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  };
10
10
  import { Catch } from "@nestjs/common";
11
11
  import { BaseExceptionFilter } from "@nestjs/core";
12
+ import { markExpressErrorCaptured } from "../express/state.js";
12
13
  import { getNestRequestContext } from "./request-context.js";
13
14
  let WotchiNestExceptionFilter = class WotchiNestExceptionFilter extends BaseExceptionFilter {
14
15
  constructor(client, applicationRef, options) {
@@ -18,7 +19,14 @@ let WotchiNestExceptionFilter = class WotchiNestExceptionFilter extends BaseExce
18
19
  }
19
20
  catch(exception, host) {
20
21
  try {
21
- this.client.captureException(exception, getNestRequestContext(host, exception, this.options));
22
+ markExpressErrorCaptured(host.switchToHttp().getRequest());
23
+ }
24
+ catch {
25
+ // Non-HTTP NestJS contexts do not expose an Express request to mark.
26
+ }
27
+ try {
28
+ const requestContext = getNestRequestContext(host, exception, this.options);
29
+ this.client.captureException(exception, undefined, requestContext === undefined ? undefined : { request: requestContext });
22
30
  }
23
31
  catch {
24
32
  // A capture failure must never change NestJS exception handling.
@@ -1,2 +1,2 @@
1
- export { consoleNotifier, createWotchi, telegramNotifier } from "../../index.js";
2
- export { registerWotchiNest } from "./register.js";
1
+ export { consoleNotifier, createWotchi, telegramNotifier, webhookNotifier } from "../../index.js";
2
+ export { registerWotchiNest, registerWotchiNestStatusObserver } from "./register.js";
@@ -1,5 +1,6 @@
1
1
  import { HttpAdapterHost } from "@nestjs/core";
2
2
  import { WotchiNestExceptionFilter } from "./exception-filter.js";
3
+ import { wotchiStatusObserver } from "../express/status-observer.js";
3
4
  export function registerWotchiNest(app, client, options) {
4
5
  const application = app;
5
6
  const directAdapter = application.getHttpAdapter?.();
@@ -8,3 +9,10 @@ export function registerWotchiNest(app, client, options) {
8
9
  : directAdapter;
9
10
  application.useGlobalFilters(new WotchiNestExceptionFilter(client, httpAdapter, options));
10
11
  }
12
+ export function registerWotchiNestStatusObserver(app, client, options) {
13
+ const application = app;
14
+ if (typeof application.use !== "function") {
15
+ throw new TypeError("Nest application does not expose use(); an Express-based adapter is required");
16
+ }
17
+ application.use(wotchiStatusObserver(client, options));
18
+ }
@@ -1,5 +1,6 @@
1
1
  const MAX_ROUTE_LENGTH = 500;
2
2
  const MAX_REQUEST_ID_LENGTH = 200;
3
+ const MAX_CORRELATION_ID_LENGTH = 200;
3
4
  const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
4
5
  const HEX_PATTERN = /^[0-9a-f]{8,}$/i;
5
6
  const isRecord = (value) => typeof value === "object" && value !== null;
@@ -28,6 +29,24 @@ const normalizeRequestIdProperty = (property) => {
28
29
  }
29
30
  return property;
30
31
  };
32
+ const normalizeTraceContextProperty = (property) => {
33
+ if (property === undefined) {
34
+ return undefined;
35
+ }
36
+ if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(property)) {
37
+ throw new TypeError("traceContextProperty must be a simple property name");
38
+ }
39
+ return property;
40
+ };
41
+ const normalizeCorrelationIdProperty = (property) => {
42
+ if (property === undefined) {
43
+ return undefined;
44
+ }
45
+ if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(property)) {
46
+ throw new TypeError("correlationIdProperty must be a simple property name");
47
+ }
48
+ return property;
49
+ };
31
50
  const readRequestId = (request, property) => {
32
51
  if (property === undefined || !isRecord(request)) {
33
52
  return undefined;
@@ -43,8 +62,51 @@ const readRequestId = (request, property) => {
43
62
  return undefined;
44
63
  }
45
64
  };
65
+ const readTraceContext = (request, property) => {
66
+ if (property === undefined || !isRecord(request)) {
67
+ return undefined;
68
+ }
69
+ try {
70
+ if (!Object.prototype.hasOwnProperty.call(request, property)) {
71
+ return undefined;
72
+ }
73
+ const value = request[property];
74
+ if (!isRecord(value)) {
75
+ return undefined;
76
+ }
77
+ const traceId = typeof value.traceId === "string" ? value.traceId.slice(0, 128) : undefined;
78
+ const spanId = typeof value.spanId === "string" ? value.spanId.slice(0, 128) : undefined;
79
+ if (traceId === undefined && spanId === undefined) {
80
+ return undefined;
81
+ }
82
+ return {
83
+ ...(traceId === undefined ? {} : { traceId }),
84
+ ...(spanId === undefined ? {} : { spanId }),
85
+ };
86
+ }
87
+ catch {
88
+ return undefined;
89
+ }
90
+ };
91
+ const readCorrelationId = (request, property) => {
92
+ if (property === undefined || !isRecord(request)) {
93
+ return undefined;
94
+ }
95
+ try {
96
+ if (!Object.prototype.hasOwnProperty.call(request, property)) {
97
+ return undefined;
98
+ }
99
+ const value = request[property];
100
+ return typeof value === "string" ? value.slice(0, MAX_CORRELATION_ID_LENGTH) : undefined;
101
+ }
102
+ catch {
103
+ return undefined;
104
+ }
105
+ };
46
106
  export function buildRequestContext(input) {
47
107
  const requestIdProperty = normalizeRequestIdProperty(input.options?.requestIdProperty);
108
+ const correlationIdProperty = normalizeCorrelationIdProperty(input.options?.correlationIdProperty);
109
+ const traceContextProperty = normalizeTraceContextProperty(input.options?.traceContextProperty);
48
110
  let requestId;
49
111
  try {
50
112
  requestId = readRequestId(input.request, requestIdProperty);
@@ -52,6 +114,22 @@ export function buildRequestContext(input) {
52
114
  catch {
53
115
  requestId = undefined;
54
116
  }
117
+ let correlationId;
118
+ try {
119
+ correlationId = readCorrelationId(input.request, correlationIdProperty);
120
+ }
121
+ catch {
122
+ correlationId = undefined;
123
+ }
124
+ let trace;
125
+ try {
126
+ const explicitTrace = input.trace;
127
+ trace = readTraceContext({ trace: explicitTrace }, "trace");
128
+ trace ??= readTraceContext(input.request, traceContextProperty);
129
+ }
130
+ catch {
131
+ trace = undefined;
132
+ }
55
133
  const method = typeof input.method === "string" ? input.method.slice(0, 16) : undefined;
56
134
  const route = normalizeRoutePath(input.route);
57
135
  const statusCode = typeof input.statusCode === "number" && Number.isSafeInteger(input.statusCode)
@@ -60,7 +138,9 @@ export function buildRequestContext(input) {
60
138
  if (method === undefined &&
61
139
  route === undefined &&
62
140
  statusCode === undefined &&
63
- requestId === undefined) {
141
+ requestId === undefined &&
142
+ correlationId === undefined &&
143
+ trace === undefined) {
64
144
  return undefined;
65
145
  }
66
146
  return {
@@ -68,5 +148,7 @@ export function buildRequestContext(input) {
68
148
  ...(route === undefined ? {} : { route }),
69
149
  ...(statusCode === undefined ? {} : { statusCode }),
70
150
  ...(requestId === undefined ? {} : { requestId }),
151
+ ...(correlationId === undefined ? {} : { correlationId }),
152
+ ...(trace === undefined ? {} : { trace }),
71
153
  };
72
154
  }
@@ -0,0 +1,16 @@
1
+ import { redactValue } from "../core/redact.js";
2
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3
+ export function toBoundedPayload(value) {
4
+ const redacted = redactValue(value, {
5
+ maxDepth: 6,
6
+ maxKeys: 100,
7
+ maxStringLength: 1_000,
8
+ maxStackLength: 4_000,
9
+ });
10
+ return isRecord(redacted)
11
+ ? redacted
12
+ : { title: "Wotchi alert", summary: "Incident details were unavailable." };
13
+ }
14
+ export function toBoundedAlertPayload(alert) {
15
+ return toBoundedPayload(alert);
16
+ }
@@ -1,19 +1,109 @@
1
+ import { redactValue } from "../core/redact.js";
2
+ import { toBoundedAlertPayload } from "./alert-payload.js";
1
3
  const MAX_FIELD_LENGTH = 1_000;
2
4
  const MAX_ACTIONS = 5;
3
5
  const limit = (value, maxLength = MAX_FIELD_LENGTH) => value.slice(0, maxLength);
4
- export function formatConsoleAlert(alert) {
6
+ const safeText = (value, maxLength = MAX_FIELD_LENGTH) => {
7
+ const redacted = redactValue(value, { maxStringLength: maxLength });
8
+ return typeof redacted === "string" ? limit(redacted, maxLength) : "[unreadable value]";
9
+ };
10
+ const safeJson = (value) => {
11
+ try {
12
+ return JSON.stringify(value);
13
+ }
14
+ catch {
15
+ return "[unreadable value]";
16
+ }
17
+ };
18
+ const boundedAlert = (alert) => {
19
+ const payload = toBoundedAlertPayload(alert);
20
+ return {
21
+ ...payload,
22
+ title: safeText(typeof payload.title === "string" ? payload.title : alert.title),
23
+ fingerprint: safeText(typeof payload.fingerprint === "string" ? payload.fingerprint : alert.fingerprint),
24
+ severity: alert.severity,
25
+ summary: safeText(typeof payload.summary === "string" ? payload.summary : alert.summary),
26
+ suggestedActions: alert.suggestedActions
27
+ .slice(0, MAX_ACTIONS)
28
+ .map((action) => safeText(action, 300)),
29
+ firstSeenAt: safeText(alert.firstSeenAt, 100),
30
+ lastSeenAt: safeText(alert.lastSeenAt, 100),
31
+ occurrences: Number.isSafeInteger(alert.occurrences) && alert.occurrences >= 0 ? alert.occurrences : 0,
32
+ service: safeText(alert.service, 300),
33
+ environment: safeText(alert.environment, 300),
34
+ };
35
+ };
36
+ const textDetails = (alert) => {
37
+ const lines = [];
38
+ if (alert.release !== undefined) {
39
+ lines.push(`Release: ${safeText(alert.release, 200)}`);
40
+ }
41
+ if (alert.instance !== undefined) {
42
+ lines.push(`Instance: ${safeText(alert.instance, 200)}`);
43
+ }
44
+ if (alert.operation !== undefined) {
45
+ lines.push(`Operation: ${safeText(alert.operation, 200)}`);
46
+ }
47
+ if (alert.job !== undefined) {
48
+ lines.push(`Job: ${safeText(alert.job, 200)}`);
49
+ }
50
+ if (alert.correlationId !== undefined) {
51
+ lines.push(`Correlation ID: ${safeText(alert.correlationId, 200)}`);
52
+ }
53
+ if (alert.tags !== undefined) {
54
+ lines.push(`Tags: ${safeText(safeJson(alert.tags), 600)}`);
55
+ }
56
+ if (alert.error !== undefined) {
57
+ lines.push(`Error: ${safeText(alert.error.name, 300)} — ${safeText(alert.error.message, 600)}`);
58
+ if (alert.error.applicationFrame !== undefined) {
59
+ lines.push(`Application frame: ${safeText(alert.error.applicationFrame, 500)}`);
60
+ }
61
+ }
62
+ if (alert.request !== undefined) {
63
+ const request = [alert.request.method, alert.request.route].filter((value) => typeof value === "string");
64
+ if (alert.request.statusCode !== undefined) {
65
+ request.push(`status ${alert.request.statusCode}`);
66
+ }
67
+ if (request.length > 0) {
68
+ lines.push(`Request: ${request.join(" ")}`);
69
+ }
70
+ if (alert.request.requestId !== undefined) {
71
+ lines.push(`Request ID: ${safeText(alert.request.requestId, 200)}`);
72
+ }
73
+ if (alert.request.correlationId !== undefined) {
74
+ lines.push(`Correlation ID: ${safeText(alert.request.correlationId, 200)}`);
75
+ }
76
+ }
77
+ const trace = alert.trace ?? alert.request?.trace;
78
+ if (trace?.traceId !== undefined || trace?.spanId !== undefined) {
79
+ lines.push(`Trace: ${safeText(trace.traceId ?? "", 128)}${trace.spanId === undefined ? "" : ` span ${safeText(trace.spanId, 128)}`}`);
80
+ }
81
+ if (alert.context !== undefined) {
82
+ lines.push(`Context: ${safeText(safeJson(alert.context), 1_000)}`);
83
+ }
84
+ if (alert.links !== undefined) {
85
+ lines.push(`Links: ${safeText(safeJson(alert.links), 600)}`);
86
+ }
87
+ return lines;
88
+ };
89
+ export function formatConsoleAlert(alert, format = "text") {
90
+ const bounded = boundedAlert(alert);
91
+ if (format === "json") {
92
+ return JSON.stringify(bounded);
93
+ }
5
94
  const actions = alert.suggestedActions
6
95
  .slice(0, MAX_ACTIONS)
7
- .map((action) => `- ${limit(action, 300)}`)
96
+ .map((action) => `- ${safeText(action, 300)}`)
8
97
  .join("\n");
9
98
  return [
10
- limit(alert.title),
11
- `Service: ${limit(alert.service, 300)}`,
12
- `Environment: ${limit(alert.environment, 300)}`,
13
- `Summary: ${limit(alert.summary)}`,
14
- `Occurrences: ${alert.occurrences}`,
15
- `First seen: ${limit(alert.firstSeenAt, 100)}`,
16
- `Last seen: ${limit(alert.lastSeenAt, 100)}`,
99
+ bounded.title,
100
+ `Service: ${bounded.service}`,
101
+ `Environment: ${bounded.environment}`,
102
+ `Summary: ${bounded.summary}`,
103
+ `Occurrences: ${bounded.occurrences}`,
104
+ `First seen: ${bounded.firstSeenAt}`,
105
+ `Last seen: ${bounded.lastSeenAt}`,
106
+ ...textDetails(alert),
17
107
  "Suggested checks:",
18
108
  actions,
19
109
  ].join("\n");
@@ -23,7 +113,7 @@ export function consoleNotifier(options) {
23
113
  return {
24
114
  name: "console",
25
115
  async send(alert) {
26
- write(formatConsoleAlert(alert));
116
+ write(formatConsoleAlert(alert, options?.format ?? "text"));
27
117
  },
28
118
  };
29
119
  }
@@ -1,3 +1,4 @@
1
+ import { redactValue } from "../core/redact.js";
1
2
  const MAX_MESSAGE_LENGTH = 4_096;
2
3
  const MAX_ACTIONS = 5;
3
4
  const MAX_TITLE_LENGTH = 300;
@@ -5,9 +6,76 @@ const MAX_SERVICE_LENGTH = 300;
5
6
  const MAX_ENVIRONMENT_LENGTH = 300;
6
7
  const MAX_SUMMARY_LENGTH = 2_000;
7
8
  const MAX_ACTION_LENGTH = 600;
9
+ const MAX_DETAIL_LENGTH = 900;
8
10
  const escapeHtml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
9
11
  const limit = (value, maxLength) => value.slice(0, maxLength);
10
- const render = (title, service, environment, summary, actions, firstSeenAt, lastSeenAt, occurrences) => {
12
+ const safeText = (value, maxLength) => {
13
+ const redacted = redactValue(value, { maxStringLength: maxLength });
14
+ return typeof redacted === "string" ? limit(redacted, maxLength) : "[unreadable value]";
15
+ };
16
+ const safeJson = (value) => {
17
+ try {
18
+ return JSON.stringify(value);
19
+ }
20
+ catch {
21
+ return "[unreadable value]";
22
+ }
23
+ };
24
+ const details = (alert) => {
25
+ const lines = [];
26
+ if (alert.release !== undefined) {
27
+ lines.push(`Release: ${safeText(alert.release, 200)}`);
28
+ }
29
+ if (alert.instance !== undefined) {
30
+ lines.push(`Instance: ${safeText(alert.instance, 200)}`);
31
+ }
32
+ if (alert.operation !== undefined) {
33
+ lines.push(`Operation: ${safeText(alert.operation, 200)}`);
34
+ }
35
+ if (alert.job !== undefined) {
36
+ lines.push(`Job: ${safeText(alert.job, 200)}`);
37
+ }
38
+ if (alert.correlationId !== undefined) {
39
+ lines.push(`Correlation ID: ${safeText(alert.correlationId, 200)}`);
40
+ }
41
+ if (alert.tags !== undefined) {
42
+ lines.push(`Tags: ${safeText(safeJson(alert.tags), MAX_DETAIL_LENGTH)}`);
43
+ }
44
+ if (alert.error !== undefined) {
45
+ lines.push(`Error: ${safeText(alert.error.name, 250)} — ${safeText(alert.error.message, 500)}`);
46
+ if (alert.error.applicationFrame !== undefined) {
47
+ lines.push(`Application frame: ${safeText(alert.error.applicationFrame, 500)}`);
48
+ }
49
+ }
50
+ if (alert.request !== undefined) {
51
+ const request = [alert.request.method, alert.request.route].filter((value) => typeof value === "string");
52
+ if (alert.request.statusCode !== undefined) {
53
+ request.push(`status ${alert.request.statusCode}`);
54
+ }
55
+ if (request.length > 0) {
56
+ lines.push(`Request: ${request.join(" ")}`);
57
+ }
58
+ if (alert.request.requestId !== undefined) {
59
+ lines.push(`Request ID: ${safeText(alert.request.requestId, 200)}`);
60
+ }
61
+ if (alert.request.correlationId !== undefined) {
62
+ lines.push(`Correlation ID: ${safeText(alert.request.correlationId, 200)}`);
63
+ }
64
+ }
65
+ const trace = alert.trace ?? alert.request?.trace;
66
+ if (trace?.traceId !== undefined || trace?.spanId !== undefined) {
67
+ lines.push(`Trace: ${safeText(trace.traceId ?? "", 128)}${trace.spanId === undefined ? "" : ` span ${safeText(trace.spanId, 128)}`}`);
68
+ }
69
+ if (alert.context !== undefined) {
70
+ const redacted = redactValue(alert.context, { maxDepth: 4, maxKeys: 40, maxStringLength: 300 });
71
+ lines.push(`Context: ${safeText(safeJson(redacted), MAX_DETAIL_LENGTH)}`);
72
+ }
73
+ if (alert.links !== undefined) {
74
+ lines.push(`Links: ${safeText(safeJson(alert.links), MAX_DETAIL_LENGTH)}`);
75
+ }
76
+ return lines.map((line) => limit(line, MAX_DETAIL_LENGTH));
77
+ };
78
+ const render = (title, service, environment, summary, actions, context, firstSeenAt, lastSeenAt, occurrences) => {
11
79
  const actionText = actions.length === 0
12
80
  ? "- No suggested checks."
13
81
  : actions.map((action) => `- ${escapeHtml(action)}`).join("\n");
@@ -19,6 +87,7 @@ const render = (title, service, environment, summary, actions, firstSeenAt, last
19
87
  `Occurrences: ${occurrences}`,
20
88
  `First seen: ${escapeHtml(firstSeenAt)}`,
21
89
  `Last seen: ${escapeHtml(lastSeenAt)}`,
90
+ ...context.map((line) => escapeHtml(line)),
22
91
  "Suggested checks:",
23
92
  actionText,
24
93
  ].join("\n");
@@ -33,17 +102,18 @@ export function formatTelegramAlert(alert) {
33
102
  let actions = alert.suggestedActions
34
103
  .slice(0, MAX_ACTIONS)
35
104
  .map((action) => limit(action, MAX_ACTION_LENGTH));
36
- let formatted = render(title, service, environment, summary, actions, firstSeenAt, lastSeenAt, alert.occurrences);
105
+ const context = details(alert);
106
+ let formatted = render(title, service, environment, summary, actions, context, firstSeenAt, lastSeenAt, alert.occurrences);
37
107
  while (formatted.length > MAX_MESSAGE_LENGTH && actions.length > 0) {
38
108
  actions = actions.slice(0, -1);
39
- formatted = render(title, service, environment, summary, actions, firstSeenAt, lastSeenAt, alert.occurrences);
109
+ formatted = render(title, service, environment, summary, actions, context, firstSeenAt, lastSeenAt, alert.occurrences);
40
110
  }
41
111
  while (formatted.length > MAX_MESSAGE_LENGTH && summary.length > 0) {
42
112
  summary = summary.slice(0, Math.max(0, summary.length - 200));
43
- formatted = render(title, service, environment, summary, actions, firstSeenAt, lastSeenAt, alert.occurrences);
113
+ formatted = render(title, service, environment, summary, actions, context, firstSeenAt, lastSeenAt, alert.occurrences);
44
114
  }
45
115
  if (formatted.length > MAX_MESSAGE_LENGTH) {
46
- formatted = render(limit(title, 100), limit(service, 100), limit(environment, 100), "Incident details were truncated.", [], firstSeenAt, lastSeenAt, alert.occurrences);
116
+ formatted = render(limit(title, 100), limit(service, 100), limit(environment, 100), "Incident details were truncated.", [], [], firstSeenAt, lastSeenAt, alert.occurrences);
47
117
  }
48
118
  return formatted;
49
119
  }