@zintrust/trace 1.6.4 → 1.6.6

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 (52) hide show
  1. package/dist/build-manifest.json +14 -14
  2. package/dist/storage/TraceContentBudget.js +2 -1
  3. package/dist/watchers/HttpWatcher.js +23 -8
  4. package/package.json +3 -2
  5. package/src/TraceConnection.ts +182 -0
  6. package/src/cli-register.ts +63 -0
  7. package/src/config.ts +383 -0
  8. package/src/context.ts +101 -0
  9. package/src/dashboard/handlers.ts +353 -0
  10. package/src/dashboard/routes.ts +114 -0
  11. package/src/dashboard/ui.ts +1262 -0
  12. package/src/dashboard/zintrust-debuger.svg +30 -0
  13. package/src/index.ts +102 -0
  14. package/src/ingest/TraceIngestGateway.ts +414 -0
  15. package/src/plugin.ts +9 -0
  16. package/src/register.ts +702 -0
  17. package/src/storage/ProxyTraceStorage.ts +190 -0
  18. package/src/storage/TraceContentBudget.ts +493 -0
  19. package/src/storage/TraceContentRedaction.ts +44 -0
  20. package/src/storage/TraceEntryFiltering.ts +50 -0
  21. package/src/storage/TraceServiceTag.ts +56 -0
  22. package/src/storage/TraceStorage.ts +543 -0
  23. package/src/storage/TraceWriteDiagnostics.ts +289 -0
  24. package/src/storage/index.ts +4 -0
  25. package/src/types.ts +430 -0
  26. package/src/ui.ts +9 -0
  27. package/src/utils/authTag.ts +20 -0
  28. package/src/utils/entryFilter.ts +131 -0
  29. package/src/utils/familyHash.ts +8 -0
  30. package/src/utils/redact.ts +112 -0
  31. package/src/utils/requestFilter.ts +79 -0
  32. package/src/utils/stackFrame.ts +44 -0
  33. package/src/watchers/AuthWatcher.ts +53 -0
  34. package/src/watchers/BatchWatcher.ts +55 -0
  35. package/src/watchers/CacheWatcher.ts +72 -0
  36. package/src/watchers/CommandWatcher.ts +58 -0
  37. package/src/watchers/DumpWatcher.ts +45 -0
  38. package/src/watchers/EventWatcher.ts +46 -0
  39. package/src/watchers/ExceptionWatcher.ts +130 -0
  40. package/src/watchers/GateWatcher.ts +53 -0
  41. package/src/watchers/HttpClientWatcher.ts +219 -0
  42. package/src/watchers/HttpWatcher.ts +249 -0
  43. package/src/watchers/JobWatcher.ts +124 -0
  44. package/src/watchers/LogWatcher.ts +120 -0
  45. package/src/watchers/MailWatcher.ts +65 -0
  46. package/src/watchers/MiddlewareWatcher.ts +54 -0
  47. package/src/watchers/ModelWatcher.ts +60 -0
  48. package/src/watchers/NotificationWatcher.ts +60 -0
  49. package/src/watchers/QueryWatcher.ts +105 -0
  50. package/src/watchers/RedisWatcher.ts +42 -0
  51. package/src/watchers/ScheduleWatcher.ts +57 -0
  52. package/src/watchers/ViewWatcher.ts +40 -0
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@zintrust/trace",
3
- "version": "1.5.1",
4
- "buildDate": "2026-04-28T16:08:58.961Z",
3
+ "version": "1.6.4",
4
+ "buildDate": "2026-04-30T15:30:37.643Z",
5
5
  "buildEnvironment": {
6
- "node": "v22.22.1",
6
+ "node": "v25.6.1",
7
7
  "platform": "darwin",
8
8
  "arch": "arm64"
9
9
  },
10
10
  "git": {
11
- "commit": "6c6a3f59",
11
+ "commit": "f235df8b",
12
12
  "branch": "release"
13
13
  },
14
14
  "package": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "build-manifest.json": {
33
33
  "size": 15912,
34
- "sha256": "b7ce0234e13707d03336dbad905b5c1403a910880469504ec6cde78ad67fe9a9"
34
+ "sha256": "6732e16044ffbcb77bf9873cf30a671007b5acc4aa2f3edf5071da667d4df534"
35
35
  },
36
36
  "cli-register.d.ts": {
37
37
  "size": 255,
@@ -87,7 +87,7 @@
87
87
  },
88
88
  "index.js": {
89
89
  "size": 3421,
90
- "sha256": "6cab0d02424c088c23dce13c349f177993fd55f21864eea1842c2276a81ad526"
90
+ "sha256": "ee5aca20d1e6d980fa0b2c02e4832c35e8a18fff9fe63a7bf6a26ae8f734d377"
91
91
  },
92
92
  "ingest/TraceIngestGateway.d.ts": {
93
93
  "size": 786,
@@ -150,8 +150,8 @@
150
150
  "sha256": "535869aa1bfcdf0ec26fad27d7f18bb8822bc586c38fcc5ffdde5274ecbea17e"
151
151
  },
152
152
  "register.js": {
153
- "size": 20927,
154
- "sha256": "026fd79a98596d2be108b350668f4345b11020953f24369866c0aad42cd7ba0f"
153
+ "size": 19680,
154
+ "sha256": "042a7585e68a8a676e25f33c9a176c6f263bccb4a63090b4d2cea52b9bcdd193"
155
155
  },
156
156
  "storage/DebuggerStorage.d.ts": {
157
157
  "size": 517,
@@ -174,8 +174,8 @@
174
174
  "sha256": "606a37af0a4aef4866c22cc727f67f485c43181b40eb831e1920b8b90fdaf503"
175
175
  },
176
176
  "storage/TraceContentBudget.js": {
177
- "size": 11434,
178
- "sha256": "f53229e7233cf13b095780451b68c912a95dc3f44e17c49289446219231e7e06"
177
+ "size": 11495,
178
+ "sha256": "8ed670773814f7b86b916c43ac43b048defc6a102593d808dfb4c8cf7606edf3"
179
179
  },
180
180
  "storage/TraceContentRedaction.d.ts": {
181
181
  "size": 207,
@@ -190,8 +190,8 @@
190
190
  "sha256": "a1158cedb4e4e749658127086e138e47886422366a697619d6ea9bd3338066e6"
191
191
  },
192
192
  "storage/TraceEntryFiltering.js": {
193
- "size": 2465,
194
- "sha256": "ace7e492373d2dccdbefe20040d30fffd7ad9f8e71113b7a044bddf92dcdf6fb"
193
+ "size": 1579,
194
+ "sha256": "a096b0fadf21cd692e554e6ca7789f08759cffe1c690845cb33c5876c8587cc5"
195
195
  },
196
196
  "storage/TraceServiceTag.d.ts": {
197
197
  "size": 224,
@@ -422,8 +422,8 @@
422
422
  "sha256": "5d5046c65e5b683369c7709f1acd09b60aec3e7f44748fd1baeb35498836465b"
423
423
  },
424
424
  "watchers/QueryWatcher.js": {
425
- "size": 3023,
426
- "sha256": "edede157915aa7832d602c92f4c2220daaf1d799210eae931b1638da883742ca"
425
+ "size": 2855,
426
+ "sha256": "dcb9fe2049e71b5fc850286758737d80738a07dd470d96f36e418d4f54e16263"
427
427
  },
428
428
  "watchers/RedisWatcher.d.ts": {
429
429
  "size": 294,
@@ -162,6 +162,7 @@ const hasQueueDispatch = (config) => {
162
162
  const driver = config.contentDispatch.driver?.trim();
163
163
  return typeof driver === 'string' && driver !== '';
164
164
  };
165
+ const WORKERS_PACKAGE_SPECIFIER = '@zintrust/workers';
165
166
  const getCoreRuntime = async () => {
166
167
  try {
167
168
  const mod = (await import('@zintrust/core'));
@@ -180,7 +181,7 @@ const getCoreRuntime = async () => {
180
181
  const getQueueWorkerApi = async () => {
181
182
  try {
182
183
  // @ts-ignore
183
- const mod = (await import('@zintrust/workers'));
184
+ const mod = (await import(WORKERS_PACKAGE_SPECIFIER));
184
185
  return typeof mod.createQueueWorker === 'function' ? mod : null;
185
186
  }
186
187
  catch {
@@ -1,3 +1,4 @@
1
+ import { Logger } from '@zintrust/core';
1
2
  import { TraceContext } from '../context.js';
2
3
  import { EntryType } from '../types.js';
3
4
  import { AuthTag } from '../utils/authTag.js';
@@ -35,8 +36,9 @@ const resolveRequestPayload = (req, config) => {
35
36
  };
36
37
  const registerCompletionHandler = (response, onComplete) => {
37
38
  const raw = response.getRaw();
38
- if (typeof raw.once !== 'function')
39
- return () => undefined;
39
+ if (typeof raw.once !== 'function') {
40
+ return { attached: false, cleanup: () => undefined };
41
+ }
40
42
  let completed = false;
41
43
  const cleanup = () => {
42
44
  if (typeof raw.off === 'function') {
@@ -53,7 +55,11 @@ const registerCompletionHandler = (response, onComplete) => {
53
55
  };
54
56
  raw.once('finish', markCompleted);
55
57
  raw.once('close', markCompleted);
56
- return cleanup;
58
+ return { attached: true, cleanup };
59
+ };
60
+ const isResponseComplete = (response) => {
61
+ const raw = response.getRaw();
62
+ return raw.writableEnded === true || raw.finished === true;
57
63
  };
58
64
  const captureResponse = (response, config) => {
59
65
  const headers = {};
@@ -147,8 +153,7 @@ export const HttpWatcher = Object.freeze({
147
153
  if (content.responseStatus >= 500)
148
154
  tags.push('failed');
149
155
  responseCapture.restore();
150
- storage
151
- .writeEntry({
156
+ const entry = {
152
157
  uuid: crypto.randomUUID(),
153
158
  batchId,
154
159
  type: EntryType.REQUEST,
@@ -156,11 +161,21 @@ export const HttpWatcher = Object.freeze({
156
161
  tags,
157
162
  isLatest: true,
158
163
  createdAt: TraceContext.now(),
159
- })
160
- .catch(() => undefined); // fire-and-forget
164
+ };
165
+ storage.writeEntry(entry).catch((error) => {
166
+ Logger.warn('[trace] HttpWatcher writeEntry failed', {
167
+ method: content.method,
168
+ uri: content.uri,
169
+ entryUuid: entry.uuid,
170
+ error: error instanceof Error ? error.message : String(error),
171
+ });
172
+ }); // fire-and-forget
161
173
  };
162
- registerCompletionHandler(response, persistEntry);
174
+ const completionHandler = registerCompletionHandler(response, persistEntry);
163
175
  await next();
176
+ if (!completionHandler.attached || isResponseComplete(response)) {
177
+ persistEntry();
178
+ }
164
179
  };
165
180
  registerMiddleware(middleware);
166
181
  return () => undefined;
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@zintrust/trace",
3
- "version": "1.6.4",
3
+ "version": "1.6.6",
4
4
  "description": "Trace assistant for ZinTrust: logs requests, queries, exceptions, jobs, and more.",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
9
  "files": [
10
- "dist"
10
+ "dist",
11
+ "src"
11
12
  ],
12
13
  "exports": {
13
14
  ".": {
@@ -0,0 +1,182 @@
1
+ import type { IDatabase } from '@zintrust/core';
2
+
3
+ type TraceErrorFactory = {
4
+ createConfigError?(message: string, details?: unknown): Error;
5
+ };
6
+
7
+ type TraceErrorApi = {
8
+ ErrorFactory?: TraceErrorFactory;
9
+ };
10
+
11
+ type TraceEnvApi = {
12
+ get(key: string, fallback: string): string;
13
+ };
14
+
15
+ type GlobalTraceConnectionState = {
16
+ __zintrust_system_trace_connection_name__?: string;
17
+ __zintrust_system_trace_plugin_requested__?: boolean;
18
+ };
19
+
20
+ export const TRACE_REQUIRED_TABLES = [
21
+ 'zin_trace_entries',
22
+ 'zin_trace_entries_tags',
23
+ 'zin_trace_monitoring',
24
+ ] as const;
25
+
26
+ const createFallbackTraceConfigError = (message: string, details?: unknown): Error => {
27
+ const error = new globalThis.Error(message) as Error & {
28
+ code?: string;
29
+ details?: unknown;
30
+ name?: string;
31
+ statusCode?: number;
32
+ };
33
+ error.name = 'ConfigError';
34
+ error.code = 'CONFIG_ERROR';
35
+ error.statusCode = 500;
36
+ error.details = details;
37
+ return error;
38
+ };
39
+
40
+ export const createTraceConfigError = (
41
+ coreApi: TraceErrorApi,
42
+ message: string,
43
+ details?: unknown
44
+ ): Error => {
45
+ if (coreApi.ErrorFactory?.createConfigError !== undefined) {
46
+ return coreApi.ErrorFactory.createConfigError(message, details);
47
+ }
48
+
49
+ return createFallbackTraceConfigError(message, details);
50
+ };
51
+
52
+ export const getRuntimeTraceConnectionName = (): string | undefined => {
53
+ const runtimeConnection = (
54
+ globalThis as GlobalTraceConnectionState
55
+ ).__zintrust_system_trace_connection_name__?.trim();
56
+
57
+ return runtimeConnection === undefined || runtimeConnection === ''
58
+ ? undefined
59
+ : runtimeConnection;
60
+ };
61
+
62
+ export const resolveDashboardTraceConnectionName = (
63
+ coreApi: TraceErrorApi,
64
+ input: {
65
+ explicitConnectionName?: string;
66
+ configuredConnectionName?: string;
67
+ }
68
+ ): string => {
69
+ const explicitConnection = input.explicitConnectionName?.trim();
70
+ if (explicitConnection !== undefined && explicitConnection !== '') {
71
+ return explicitConnection;
72
+ }
73
+
74
+ const runtimeConnection = getRuntimeTraceConnectionName();
75
+ if (runtimeConnection !== undefined) {
76
+ return runtimeConnection;
77
+ }
78
+
79
+ const configuredConnection = input.configuredConnectionName?.trim();
80
+ if (configuredConnection !== undefined && configuredConnection !== '') {
81
+ return configuredConnection;
82
+ }
83
+
84
+ throw createTraceConfigError(coreApi, 'Trace dashboard connection is not configured.', {
85
+ envKey: 'TRACE_DB_CONNECTION',
86
+ hint: 'Import @zintrust/trace/register before mounting the dashboard, pass connectionName explicitly, or set TRACE_DB_CONNECTION to the trace storage connection.',
87
+ });
88
+ };
89
+
90
+ export const resolveTraceConnectionName = (
91
+ env: Pick<TraceEnvApi, 'get'> | undefined,
92
+ configuredConnection?: string
93
+ ): string => {
94
+ const resolveDefaultConnection = (): string => {
95
+ const defaultConnection = env?.get('DB_CONNECTION', '').trim() ?? '';
96
+ if (defaultConnection === '' || defaultConnection === 'default') return 'default';
97
+ return defaultConnection;
98
+ };
99
+
100
+ const explicitConnection = configuredConnection?.trim();
101
+ if (explicitConnection !== undefined && explicitConnection !== '') {
102
+ return explicitConnection === 'default' ? resolveDefaultConnection() : explicitConnection;
103
+ }
104
+
105
+ return resolveDefaultConnection();
106
+ };
107
+
108
+ export const resolveObservedConnectionName = (
109
+ env: Pick<TraceEnvApi, 'get'> | undefined,
110
+ configuredObservedConnection: string | undefined,
111
+ storageConnectionName: string
112
+ ): string => {
113
+ if (
114
+ typeof configuredObservedConnection === 'string' &&
115
+ configuredObservedConnection.trim() !== ''
116
+ ) {
117
+ return resolveTraceConnectionName(env, configuredObservedConnection);
118
+ }
119
+
120
+ const defaultConnectionName = resolveTraceConnectionName(env);
121
+ if (storageConnectionName !== defaultConnectionName) {
122
+ return defaultConnectionName;
123
+ }
124
+
125
+ return storageConnectionName;
126
+ };
127
+
128
+ export function assertTraceConnectionResolved(
129
+ coreApi: TraceErrorApi,
130
+ db: IDatabase | undefined,
131
+ params: { connectionName: string; envKey: 'TRACE_DB_CONNECTION' | 'TRACE_QUERY_CONNECTION' }
132
+ ): asserts db is IDatabase {
133
+ if (db !== undefined) {
134
+ return;
135
+ }
136
+
137
+ const pluginRequested =
138
+ (globalThis as GlobalTraceConnectionState).__zintrust_system_trace_plugin_requested__ === true;
139
+ let hint =
140
+ 'Configure TRACE_QUERY_CONNECTION, or ensure DB_CONNECTION resolves to an existing database connection.';
141
+
142
+ if (params.envKey === 'TRACE_DB_CONNECTION') {
143
+ hint = pluginRequested
144
+ ? 'Configure TRACE_DB_CONNECTION to an existing database connection before enabling TRACE_ENABLED.'
145
+ : 'If this module is being imported from zintrust.plugins.*, switch that import to @zintrust/trace/plugin so trace registration runs after database runtime registration. Otherwise configure TRACE_DB_CONNECTION to an existing database connection before enabling TRACE_ENABLED.';
146
+ }
147
+
148
+ throw createTraceConfigError(
149
+ coreApi,
150
+ `Trace connection "${params.connectionName}" could not be resolved.`,
151
+ {
152
+ connectionName: params.connectionName,
153
+ envKey: params.envKey,
154
+ hint,
155
+ }
156
+ );
157
+ }
158
+
159
+ export const assertTraceStorageReady = async (
160
+ coreApi: TraceErrorApi,
161
+ db: IDatabase,
162
+ connectionName: string,
163
+ operation = 'Trace storage connection'
164
+ ): Promise<void> => {
165
+ try {
166
+ await Promise.all(
167
+ TRACE_REQUIRED_TABLES.map(async (table) => {
168
+ await db.queryOne(`SELECT 1 AS ok FROM ${table} LIMIT 1`, []);
169
+ })
170
+ );
171
+ } catch (error) {
172
+ throw createTraceConfigError(
173
+ coreApi,
174
+ `${operation} "${connectionName}" is not ready. Create the database if needed and run \`zin migrate:trace\` before enabling TRACE_ENABLED.`,
175
+ {
176
+ connectionName,
177
+ error,
178
+ requiredTables: [...TRACE_REQUIRED_TABLES],
179
+ }
180
+ );
181
+ }
182
+ };
@@ -0,0 +1,63 @@
1
+ type Registry = {
2
+ register: (id: string, provider: CliCommandProvider) => void;
3
+ };
4
+
5
+ type CliCommandProvider = {
6
+ getCommand: () => unknown;
7
+ name?: string;
8
+ };
9
+
10
+ type TraceCommandsModule = {
11
+ TraceCommands: {
12
+ createTracePruneProvider: () => CliCommandProvider;
13
+ createTraceClearProvider: () => CliCommandProvider;
14
+ createTraceStatusProvider: () => CliCommandProvider;
15
+ createTraceMigrateProvider: () => CliCommandProvider;
16
+ };
17
+ };
18
+
19
+ const commandModule = (await import('@zintrust/core/cli')) as unknown as TraceCommandsModule;
20
+
21
+ const getTraceProviders = (): Array<[string, CliCommandProvider]> => {
22
+ const { TraceCommands } = commandModule;
23
+
24
+ return [
25
+ ['trace:prune', TraceCommands.createTracePruneProvider()],
26
+ ['trace:clear', TraceCommands.createTraceClearProvider()],
27
+ ['trace:status', TraceCommands.createTraceStatusProvider()],
28
+ ['migrate:trace', TraceCommands.createTraceMigrateProvider()],
29
+ ];
30
+ };
31
+
32
+ export function registerTraceCliCommands(registry: Registry): void {
33
+ for (const [id, provider] of getTraceProviders()) {
34
+ registry.register(id, provider);
35
+ }
36
+ }
37
+
38
+ type GlobalWithRegistry = {
39
+ __zintrust_cli_command_registry__?: Map<string, CliCommandProvider>;
40
+ };
41
+
42
+ const globalWithRegistry = globalThis as unknown as GlobalWithRegistry;
43
+ const globalRegistry =
44
+ globalWithRegistry.__zintrust_cli_command_registry__ ??
45
+ (globalWithRegistry.__zintrust_cli_command_registry__ = new Map<string, CliCommandProvider>());
46
+
47
+ registerTraceCliCommands({
48
+ register: (id, provider) => {
49
+ globalRegistry.set(id, provider);
50
+ },
51
+ });
52
+
53
+ try {
54
+ const coreCli = (await import('@zintrust/core/cli')) as unknown as {
55
+ OptionalCliCommandRegistry?: Registry;
56
+ };
57
+
58
+ if (coreCli.OptionalCliCommandRegistry !== undefined) {
59
+ registerTraceCliCommands(coreCli.OptionalCliCommandRegistry);
60
+ }
61
+ } catch {
62
+ // no-op
63
+ }