@zintrust/trace 0.4.75

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 (128) hide show
  1. package/README.md +288 -0
  2. package/dist/build-manifest.json +365 -0
  3. package/dist/cli-register.d.ts +9 -0
  4. package/dist/cli-register.js +32 -0
  5. package/dist/config.d.ts +9 -0
  6. package/dist/config.js +38 -0
  7. package/dist/context.d.ts +18 -0
  8. package/dist/context.js +86 -0
  9. package/dist/dashboard/handlers.d.ts +15 -0
  10. package/dist/dashboard/handlers.js +179 -0
  11. package/dist/dashboard/routes.d.ts +19 -0
  12. package/dist/dashboard/routes.js +50 -0
  13. package/dist/dashboard/ui.d.ts +2 -0
  14. package/dist/dashboard/ui.js +870 -0
  15. package/dist/index.d.ts +35 -0
  16. package/dist/index.js +50 -0
  17. package/dist/migrations/20260331000001_create_zin_debugger_entries_table.d.ts +10 -0
  18. package/dist/migrations/20260331000001_create_zin_debugger_entries_table.js +28 -0
  19. package/dist/migrations/20260331000002_create_zin_debugger_entries_tags_table.d.ts +10 -0
  20. package/dist/migrations/20260331000002_create_zin_debugger_entries_tags_table.js +21 -0
  21. package/dist/migrations/20260331000003_create_zin_debugger_monitoring_table.d.ts +10 -0
  22. package/dist/migrations/20260331000003_create_zin_debugger_monitoring_table.js +17 -0
  23. package/dist/migrations/index.d.ts +6 -0
  24. package/dist/migrations/index.js +4 -0
  25. package/dist/plugin.d.ts +1 -0
  26. package/dist/plugin.js +3 -0
  27. package/dist/register.d.ts +1 -0
  28. package/dist/register.js +140 -0
  29. package/dist/storage/DebuggerStorage.d.ts +13 -0
  30. package/dist/storage/DebuggerStorage.js +195 -0
  31. package/dist/storage/TraceStorage.d.ts +13 -0
  32. package/dist/storage/TraceStorage.js +195 -0
  33. package/dist/storage/index.d.ts +2 -0
  34. package/dist/storage/index.js +1 -0
  35. package/dist/types.d.ts +270 -0
  36. package/dist/types.js +25 -0
  37. package/dist/ui.d.ts +8 -0
  38. package/dist/ui.js +7 -0
  39. package/dist/utils/authTag.d.ts +5 -0
  40. package/dist/utils/authTag.js +18 -0
  41. package/dist/utils/familyHash.d.ts +1 -0
  42. package/dist/utils/familyHash.js +8 -0
  43. package/dist/utils/redact.d.ts +6 -0
  44. package/dist/utils/redact.js +49 -0
  45. package/dist/utils/requestFilter.d.ts +4 -0
  46. package/dist/utils/requestFilter.js +26 -0
  47. package/dist/utils/stackFrame.d.ts +6 -0
  48. package/dist/utils/stackFrame.js +38 -0
  49. package/dist/watchers/AuthWatcher.d.ts +6 -0
  50. package/dist/watchers/AuthWatcher.js +49 -0
  51. package/dist/watchers/BatchWatcher.d.ts +6 -0
  52. package/dist/watchers/BatchWatcher.js +46 -0
  53. package/dist/watchers/CacheWatcher.d.ts +6 -0
  54. package/dist/watchers/CacheWatcher.js +51 -0
  55. package/dist/watchers/CommandWatcher.d.ts +6 -0
  56. package/dist/watchers/CommandWatcher.js +49 -0
  57. package/dist/watchers/DumpWatcher.d.ts +7 -0
  58. package/dist/watchers/DumpWatcher.js +41 -0
  59. package/dist/watchers/EventWatcher.d.ts +6 -0
  60. package/dist/watchers/EventWatcher.js +42 -0
  61. package/dist/watchers/ExceptionWatcher.d.ts +4 -0
  62. package/dist/watchers/ExceptionWatcher.js +103 -0
  63. package/dist/watchers/GateWatcher.d.ts +6 -0
  64. package/dist/watchers/GateWatcher.js +45 -0
  65. package/dist/watchers/HttpClientWatcher.d.ts +6 -0
  66. package/dist/watchers/HttpClientWatcher.js +50 -0
  67. package/dist/watchers/HttpWatcher.d.ts +2 -0
  68. package/dist/watchers/HttpWatcher.js +71 -0
  69. package/dist/watchers/JobWatcher.d.ts +10 -0
  70. package/dist/watchers/JobWatcher.js +108 -0
  71. package/dist/watchers/LogWatcher.d.ts +2 -0
  72. package/dist/watchers/LogWatcher.js +50 -0
  73. package/dist/watchers/MailWatcher.d.ts +6 -0
  74. package/dist/watchers/MailWatcher.js +45 -0
  75. package/dist/watchers/MiddlewareWatcher.d.ts +6 -0
  76. package/dist/watchers/MiddlewareWatcher.js +41 -0
  77. package/dist/watchers/ModelWatcher.d.ts +6 -0
  78. package/dist/watchers/ModelWatcher.js +42 -0
  79. package/dist/watchers/NotificationWatcher.d.ts +6 -0
  80. package/dist/watchers/NotificationWatcher.js +42 -0
  81. package/dist/watchers/QueryWatcher.d.ts +2 -0
  82. package/dist/watchers/QueryWatcher.js +72 -0
  83. package/dist/watchers/RedisWatcher.d.ts +7 -0
  84. package/dist/watchers/RedisWatcher.js +38 -0
  85. package/dist/watchers/ScheduleWatcher.d.ts +6 -0
  86. package/dist/watchers/ScheduleWatcher.js +46 -0
  87. package/dist/watchers/ViewWatcher.d.ts +6 -0
  88. package/dist/watchers/ViewWatcher.js +36 -0
  89. package/package.json +59 -0
  90. package/src/cli-register.ts +63 -0
  91. package/src/config.ts +46 -0
  92. package/src/context.ts +101 -0
  93. package/src/dashboard/handlers.ts +197 -0
  94. package/src/dashboard/routes.ts +101 -0
  95. package/src/dashboard/ui.ts +879 -0
  96. package/src/dashboard/zintrust-debuger.svg +30 -0
  97. package/src/index.ts +88 -0
  98. package/src/plugin.ts +9 -0
  99. package/src/register.ts +219 -0
  100. package/src/storage/TraceStorage.ts +306 -0
  101. package/src/storage/index.ts +2 -0
  102. package/src/types.ts +317 -0
  103. package/src/ui.ts +9 -0
  104. package/src/utils/authTag.ts +20 -0
  105. package/src/utils/familyHash.ts +8 -0
  106. package/src/utils/redact.ts +64 -0
  107. package/src/utils/requestFilter.ts +33 -0
  108. package/src/utils/stackFrame.ts +44 -0
  109. package/src/watchers/AuthWatcher.ts +50 -0
  110. package/src/watchers/BatchWatcher.ts +52 -0
  111. package/src/watchers/CacheWatcher.ts +58 -0
  112. package/src/watchers/CommandWatcher.ts +55 -0
  113. package/src/watchers/DumpWatcher.ts +42 -0
  114. package/src/watchers/EventWatcher.ts +43 -0
  115. package/src/watchers/ExceptionWatcher.ts +114 -0
  116. package/src/watchers/GateWatcher.ts +50 -0
  117. package/src/watchers/HttpClientWatcher.ts +56 -0
  118. package/src/watchers/HttpWatcher.ts +94 -0
  119. package/src/watchers/JobWatcher.ts +121 -0
  120. package/src/watchers/LogWatcher.ts +61 -0
  121. package/src/watchers/MailWatcher.ts +47 -0
  122. package/src/watchers/MiddlewareWatcher.ts +42 -0
  123. package/src/watchers/ModelWatcher.ts +48 -0
  124. package/src/watchers/NotificationWatcher.ts +43 -0
  125. package/src/watchers/QueryWatcher.ts +85 -0
  126. package/src/watchers/RedisWatcher.ts +39 -0
  127. package/src/watchers/ScheduleWatcher.ts +54 -0
  128. package/src/watchers/ViewWatcher.ts +37 -0
@@ -0,0 +1,42 @@
1
+ import { TraceContext } from '../context';
2
+ import type { ITraceWatcher, ITraceWatcherConfig, MiddlewareContent } from '../types';
3
+ import { EntryType } from '../types';
4
+ import { RequestFilter } from '../utils/requestFilter';
5
+
6
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
7
+ let _ignoreRoutes: string[] = [];
8
+
9
+ const emit = (name: string, event: MiddlewareContent['event'], duration?: number): void => {
10
+ if (!_storage) return;
11
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
12
+ const content: MiddlewareContent = {
13
+ name,
14
+ event,
15
+ duration,
16
+ hostname: TraceContext.getHostname(),
17
+ };
18
+ _storage
19
+ .writeEntry({
20
+ uuid: crypto.randomUUID(),
21
+ batchId: TraceContext.getBatchId(),
22
+ type: EntryType.MIDDLEWARE,
23
+ content,
24
+ tags: [name, event],
25
+ isLatest: true,
26
+ createdAt: TraceContext.now(),
27
+ })
28
+ .catch(() => undefined);
29
+ };
30
+
31
+ export const MiddlewareWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
32
+ emit,
33
+ register({ storage, config }: ITraceWatcherConfig): () => void {
34
+ if (config.watchers.middleware === false) return () => undefined;
35
+ _storage = storage;
36
+ _ignoreRoutes = config.ignoreRoutes;
37
+ return () => {
38
+ _storage = null;
39
+ _ignoreRoutes = [];
40
+ };
41
+ },
42
+ });
@@ -0,0 +1,48 @@
1
+ import { TraceContext } from '../context';
2
+ import type { ITraceWatcher, ITraceWatcherConfig, ModelContent } from '../types';
3
+ import { EntryType } from '../types';
4
+ import { RequestFilter } from '../utils/requestFilter';
5
+
6
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
7
+ let _ignoreRoutes: string[] = [];
8
+
9
+ const emit = (
10
+ action: ModelContent['action'],
11
+ model: string,
12
+ id?: string | number,
13
+ changes?: Record<string, unknown>
14
+ ): void => {
15
+ if (!_storage) return;
16
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
17
+ const content: ModelContent = {
18
+ action,
19
+ model,
20
+ id,
21
+ changes,
22
+ hostname: TraceContext.getHostname(),
23
+ };
24
+ _storage
25
+ .writeEntry({
26
+ uuid: crypto.randomUUID(),
27
+ batchId: TraceContext.getBatchId(),
28
+ type: EntryType.MODEL,
29
+ content,
30
+ tags: [model],
31
+ isLatest: true,
32
+ createdAt: TraceContext.now(),
33
+ })
34
+ .catch(() => undefined);
35
+ };
36
+
37
+ export const ModelWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
38
+ emit,
39
+ register({ storage, config }: ITraceWatcherConfig): () => void {
40
+ if (config.watchers.model === false) return () => undefined;
41
+ _storage = storage;
42
+ _ignoreRoutes = config.ignoreRoutes;
43
+ return () => {
44
+ _storage = null;
45
+ _ignoreRoutes = [];
46
+ };
47
+ },
48
+ });
@@ -0,0 +1,43 @@
1
+ import { TraceContext } from '../context';
2
+ import type { ITraceWatcher, ITraceWatcherConfig, NotificationContent } from '../types';
3
+ import { EntryType } from '../types';
4
+ import { AuthTag } from '../utils/authTag';
5
+ import { RequestFilter } from '../utils/requestFilter';
6
+
7
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
8
+ let _ignoreRoutes: string[] = [];
9
+
10
+ const emit = (notification: string, channels: string[], notifiable?: string): void => {
11
+ if (!_storage) return;
12
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
13
+ const content: NotificationContent = {
14
+ notification,
15
+ channels,
16
+ notifiable,
17
+ hostname: TraceContext.getHostname(),
18
+ };
19
+ _storage
20
+ .writeEntry({
21
+ uuid: crypto.randomUUID(),
22
+ batchId: TraceContext.getBatchId(),
23
+ type: EntryType.NOTIFICATION,
24
+ content,
25
+ tags: AuthTag.append([notification, ...channels]),
26
+ isLatest: true,
27
+ createdAt: TraceContext.now(),
28
+ })
29
+ .catch(() => undefined);
30
+ };
31
+
32
+ export const NotificationWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
33
+ emit,
34
+ register({ storage, config }: ITraceWatcherConfig): () => void {
35
+ if (config.watchers.notification === false) return () => undefined;
36
+ _storage = storage;
37
+ _ignoreRoutes = config.ignoreRoutes;
38
+ return () => {
39
+ _storage = null;
40
+ _ignoreRoutes = [];
41
+ };
42
+ },
43
+ });
@@ -0,0 +1,85 @@
1
+ /**
2
+ * QueryWatcher — hooks into Database.onAfterQuery to record SQL entries.
3
+ */
4
+ import { TraceContext } from '../context';
5
+ import { TraceStorage } from '../storage';
6
+ import type { ITraceWatcher, ITraceWatcherConfig, QueryContent } from '../types';
7
+ import { EntryType } from '../types';
8
+ import { AuthTag } from '../utils/authTag';
9
+ import { RequestFilter } from '../utils/requestFilter';
10
+
11
+ const bindingsInterpolated = (sql: string, params: unknown[]): string => {
12
+ // Inline params for display only — safe, not for re-execution.
13
+ let i = 0;
14
+ return sql.replaceAll('?', () => {
15
+ const val = params[i++];
16
+ if (val === null || val === undefined) return 'NULL';
17
+ if (typeof val === 'string') return `'${val.replaceAll("'", "''")}'`;
18
+ return String(val);
19
+ });
20
+ };
21
+
22
+ const isTraceStorageQuery = (sql: string): boolean => {
23
+ const normalized = sql.toLowerCase();
24
+ return normalized.includes('zin_trace_entries') || normalized.includes('zin_trace_monitoring');
25
+ };
26
+
27
+ export const QueryWatcher: ITraceWatcher = Object.freeze({
28
+ register({ storage, config, db: injectedDb }: ITraceWatcherConfig): () => void {
29
+ if (config.watchers.query === false) return () => undefined;
30
+ if (!injectedDb) return () => undefined; // no db available
31
+
32
+ const db = injectedDb;
33
+
34
+ const handler = (query: string, params: unknown[], duration: number): void => {
35
+ if (RequestFilter.shouldIgnoreCurrentRequest(config.ignoreRoutes)) return;
36
+ if (isTraceStorageQuery(query)) return;
37
+
38
+ const batchId = TraceContext.getBatchId();
39
+ const sql = bindingsInterpolated(query, params);
40
+ const roundedDuration = Math.round(duration * 100) / 100;
41
+ const hash = TraceStorage.familyHash(query);
42
+ const slow = roundedDuration >= config.slowQueryThreshold;
43
+
44
+ const content: QueryContent = {
45
+ connection: 'default',
46
+ sql,
47
+ time: roundedDuration,
48
+ duration: roundedDuration,
49
+ slow,
50
+ hash,
51
+ hostname: TraceContext.getHostname(),
52
+ };
53
+
54
+ const tags = AuthTag.append([]);
55
+ if (slow) tags.push('slow');
56
+
57
+ storage
58
+ .writeEntry({
59
+ uuid: crypto.randomUUID(),
60
+ batchId,
61
+ familyHash: hash,
62
+ type: EntryType.QUERY,
63
+ content,
64
+ tags,
65
+ isLatest: true,
66
+ createdAt: TraceContext.now(),
67
+ })
68
+ .catch(() => undefined);
69
+ };
70
+
71
+ (
72
+ db as {
73
+ onAfterQuery?: (h: (sql: string, params: unknown[], duration: number) => void) => void;
74
+ }
75
+ ).onAfterQuery?.(handler);
76
+
77
+ return () => {
78
+ (
79
+ db as {
80
+ offAfterQuery?: (h: (sql: string, params: unknown[], duration: number) => void) => void;
81
+ }
82
+ ).offAfterQuery?.(handler);
83
+ };
84
+ },
85
+ });
@@ -0,0 +1,39 @@
1
+ import { TraceContext } from '../context';
2
+ import type { ITraceWatcher, ITraceWatcherConfig, RedisContent } from '../types';
3
+ import { EntryType } from '../types';
4
+ import { AuthTag } from '../utils/authTag';
5
+ import { RequestFilter } from '../utils/requestFilter';
6
+
7
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
8
+ let _ignoreRoutes: string[] = [];
9
+
10
+ /** Emit a redis command trace. Key/value payload is intentionally omitted for security. */
11
+ const emit = (command: string, duration: number): void => {
12
+ if (!_storage) return;
13
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
14
+ const content: RedisContent = { command, duration, hostname: TraceContext.getHostname() };
15
+ _storage
16
+ .writeEntry({
17
+ uuid: crypto.randomUUID(),
18
+ batchId: TraceContext.getBatchId(),
19
+ type: EntryType.REDIS,
20
+ content,
21
+ tags: AuthTag.append([command.toUpperCase()]),
22
+ isLatest: true,
23
+ createdAt: TraceContext.now(),
24
+ })
25
+ .catch(() => undefined);
26
+ };
27
+
28
+ export const RedisWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
29
+ emit,
30
+ register({ storage, config }: ITraceWatcherConfig): () => void {
31
+ if (config.watchers.redis === false) return () => undefined;
32
+ _storage = storage;
33
+ _ignoreRoutes = config.ignoreRoutes;
34
+ return () => {
35
+ _storage = null;
36
+ _ignoreRoutes = [];
37
+ };
38
+ },
39
+ });
@@ -0,0 +1,54 @@
1
+ /**
2
+ * ScheduleWatcher — records scheduled task runs and outcomes.
3
+ */
4
+ import { TraceContext } from '../context';
5
+ import type { ITraceWatcher, ITraceWatcherConfig, ScheduleContent } from '../types';
6
+ import { EntryType } from '../types';
7
+ import { RequestFilter } from '../utils/requestFilter';
8
+
9
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
10
+ let _ignoreRoutes: string[] = [];
11
+
12
+ const emit = (
13
+ name: string,
14
+ expression: string,
15
+ status: ScheduleContent['status'],
16
+ duration: number,
17
+ output?: string
18
+ ): void => {
19
+ if (!_storage) return;
20
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
21
+ const content: ScheduleContent = {
22
+ name,
23
+ expression,
24
+ status,
25
+ duration,
26
+ output,
27
+ hostname: TraceContext.getHostname(),
28
+ };
29
+ _storage
30
+ .writeEntry({
31
+ uuid: crypto.randomUUID(),
32
+ batchId: TraceContext.getBatchId(),
33
+ type: EntryType.SCHEDULE,
34
+ content,
35
+ tags: status === 'failed' ? ['failed'] : [],
36
+ isLatest: true,
37
+ createdAt: TraceContext.now(),
38
+ })
39
+ .catch(() => undefined);
40
+ };
41
+
42
+ export const ScheduleWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
43
+ emit,
44
+
45
+ register({ storage, config }: ITraceWatcherConfig): () => void {
46
+ if (config.watchers.schedule === false) return () => undefined;
47
+ _storage = storage;
48
+ _ignoreRoutes = config.ignoreRoutes;
49
+ return () => {
50
+ _storage = null;
51
+ _ignoreRoutes = [];
52
+ };
53
+ },
54
+ });
@@ -0,0 +1,37 @@
1
+ import { TraceContext } from '../context';
2
+ import type { ITraceWatcher, ITraceWatcherConfig, ViewContent } from '../types';
3
+ import { EntryType } from '../types';
4
+ import { RequestFilter } from '../utils/requestFilter';
5
+
6
+ let _storage: ITraceWatcherConfig['storage'] | null = null;
7
+ let _ignoreRoutes: string[] = [];
8
+
9
+ const emit = (template: string, duration: number): void => {
10
+ if (!_storage) return;
11
+ if (RequestFilter.shouldIgnoreCurrentRequest(_ignoreRoutes)) return;
12
+ const content: ViewContent = { template, duration, hostname: TraceContext.getHostname() };
13
+ _storage
14
+ .writeEntry({
15
+ uuid: crypto.randomUUID(),
16
+ batchId: TraceContext.getBatchId(),
17
+ type: EntryType.VIEW,
18
+ content,
19
+ tags: [template],
20
+ isLatest: true,
21
+ createdAt: TraceContext.now(),
22
+ })
23
+ .catch(() => undefined);
24
+ };
25
+
26
+ export const ViewWatcher: ITraceWatcher & { emit: typeof emit } = Object.freeze({
27
+ emit,
28
+ register({ storage, config }: ITraceWatcherConfig): () => void {
29
+ if (config.watchers.view === false) return () => undefined;
30
+ _storage = storage;
31
+ _ignoreRoutes = config.ignoreRoutes;
32
+ return () => {
33
+ _storage = null;
34
+ _ignoreRoutes = [];
35
+ };
36
+ },
37
+ });