@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.
- package/README.md +288 -0
- package/dist/build-manifest.json +365 -0
- package/dist/cli-register.d.ts +9 -0
- package/dist/cli-register.js +32 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +38 -0
- package/dist/context.d.ts +18 -0
- package/dist/context.js +86 -0
- package/dist/dashboard/handlers.d.ts +15 -0
- package/dist/dashboard/handlers.js +179 -0
- package/dist/dashboard/routes.d.ts +19 -0
- package/dist/dashboard/routes.js +50 -0
- package/dist/dashboard/ui.d.ts +2 -0
- package/dist/dashboard/ui.js +870 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +50 -0
- package/dist/migrations/20260331000001_create_zin_debugger_entries_table.d.ts +10 -0
- package/dist/migrations/20260331000001_create_zin_debugger_entries_table.js +28 -0
- package/dist/migrations/20260331000002_create_zin_debugger_entries_tags_table.d.ts +10 -0
- package/dist/migrations/20260331000002_create_zin_debugger_entries_tags_table.js +21 -0
- package/dist/migrations/20260331000003_create_zin_debugger_monitoring_table.d.ts +10 -0
- package/dist/migrations/20260331000003_create_zin_debugger_monitoring_table.js +17 -0
- package/dist/migrations/index.d.ts +6 -0
- package/dist/migrations/index.js +4 -0
- package/dist/plugin.d.ts +1 -0
- package/dist/plugin.js +3 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +140 -0
- package/dist/storage/DebuggerStorage.d.ts +13 -0
- package/dist/storage/DebuggerStorage.js +195 -0
- package/dist/storage/TraceStorage.d.ts +13 -0
- package/dist/storage/TraceStorage.js +195 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.js +1 -0
- package/dist/types.d.ts +270 -0
- package/dist/types.js +25 -0
- package/dist/ui.d.ts +8 -0
- package/dist/ui.js +7 -0
- package/dist/utils/authTag.d.ts +5 -0
- package/dist/utils/authTag.js +18 -0
- package/dist/utils/familyHash.d.ts +1 -0
- package/dist/utils/familyHash.js +8 -0
- package/dist/utils/redact.d.ts +6 -0
- package/dist/utils/redact.js +49 -0
- package/dist/utils/requestFilter.d.ts +4 -0
- package/dist/utils/requestFilter.js +26 -0
- package/dist/utils/stackFrame.d.ts +6 -0
- package/dist/utils/stackFrame.js +38 -0
- package/dist/watchers/AuthWatcher.d.ts +6 -0
- package/dist/watchers/AuthWatcher.js +49 -0
- package/dist/watchers/BatchWatcher.d.ts +6 -0
- package/dist/watchers/BatchWatcher.js +46 -0
- package/dist/watchers/CacheWatcher.d.ts +6 -0
- package/dist/watchers/CacheWatcher.js +51 -0
- package/dist/watchers/CommandWatcher.d.ts +6 -0
- package/dist/watchers/CommandWatcher.js +49 -0
- package/dist/watchers/DumpWatcher.d.ts +7 -0
- package/dist/watchers/DumpWatcher.js +41 -0
- package/dist/watchers/EventWatcher.d.ts +6 -0
- package/dist/watchers/EventWatcher.js +42 -0
- package/dist/watchers/ExceptionWatcher.d.ts +4 -0
- package/dist/watchers/ExceptionWatcher.js +103 -0
- package/dist/watchers/GateWatcher.d.ts +6 -0
- package/dist/watchers/GateWatcher.js +45 -0
- package/dist/watchers/HttpClientWatcher.d.ts +6 -0
- package/dist/watchers/HttpClientWatcher.js +50 -0
- package/dist/watchers/HttpWatcher.d.ts +2 -0
- package/dist/watchers/HttpWatcher.js +71 -0
- package/dist/watchers/JobWatcher.d.ts +10 -0
- package/dist/watchers/JobWatcher.js +108 -0
- package/dist/watchers/LogWatcher.d.ts +2 -0
- package/dist/watchers/LogWatcher.js +50 -0
- package/dist/watchers/MailWatcher.d.ts +6 -0
- package/dist/watchers/MailWatcher.js +45 -0
- package/dist/watchers/MiddlewareWatcher.d.ts +6 -0
- package/dist/watchers/MiddlewareWatcher.js +41 -0
- package/dist/watchers/ModelWatcher.d.ts +6 -0
- package/dist/watchers/ModelWatcher.js +42 -0
- package/dist/watchers/NotificationWatcher.d.ts +6 -0
- package/dist/watchers/NotificationWatcher.js +42 -0
- package/dist/watchers/QueryWatcher.d.ts +2 -0
- package/dist/watchers/QueryWatcher.js +72 -0
- package/dist/watchers/RedisWatcher.d.ts +7 -0
- package/dist/watchers/RedisWatcher.js +38 -0
- package/dist/watchers/ScheduleWatcher.d.ts +6 -0
- package/dist/watchers/ScheduleWatcher.js +46 -0
- package/dist/watchers/ViewWatcher.d.ts +6 -0
- package/dist/watchers/ViewWatcher.js +36 -0
- package/package.json +59 -0
- package/src/cli-register.ts +63 -0
- package/src/config.ts +46 -0
- package/src/context.ts +101 -0
- package/src/dashboard/handlers.ts +197 -0
- package/src/dashboard/routes.ts +101 -0
- package/src/dashboard/ui.ts +879 -0
- package/src/dashboard/zintrust-debuger.svg +30 -0
- package/src/index.ts +88 -0
- package/src/plugin.ts +9 -0
- package/src/register.ts +219 -0
- package/src/storage/TraceStorage.ts +306 -0
- package/src/storage/index.ts +2 -0
- package/src/types.ts +317 -0
- package/src/ui.ts +9 -0
- package/src/utils/authTag.ts +20 -0
- package/src/utils/familyHash.ts +8 -0
- package/src/utils/redact.ts +64 -0
- package/src/utils/requestFilter.ts +33 -0
- package/src/utils/stackFrame.ts +44 -0
- package/src/watchers/AuthWatcher.ts +50 -0
- package/src/watchers/BatchWatcher.ts +52 -0
- package/src/watchers/CacheWatcher.ts +58 -0
- package/src/watchers/CommandWatcher.ts +55 -0
- package/src/watchers/DumpWatcher.ts +42 -0
- package/src/watchers/EventWatcher.ts +43 -0
- package/src/watchers/ExceptionWatcher.ts +114 -0
- package/src/watchers/GateWatcher.ts +50 -0
- package/src/watchers/HttpClientWatcher.ts +56 -0
- package/src/watchers/HttpWatcher.ts +94 -0
- package/src/watchers/JobWatcher.ts +121 -0
- package/src/watchers/LogWatcher.ts +61 -0
- package/src/watchers/MailWatcher.ts +47 -0
- package/src/watchers/MiddlewareWatcher.ts +42 -0
- package/src/watchers/ModelWatcher.ts +48 -0
- package/src/watchers/NotificationWatcher.ts +43 -0
- package/src/watchers/QueryWatcher.ts +85 -0
- package/src/watchers/RedisWatcher.ts +39 -0
- package/src/watchers/ScheduleWatcher.ts +54 -0
- package/src/watchers/ViewWatcher.ts +37 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zintrust/trace — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* Zero side-effects. Import watchers, storage, dashboard, and config
|
|
5
|
+
* individually. For full auto-initialisation, use:
|
|
6
|
+
* import '@zintrust/trace/register';
|
|
7
|
+
*/
|
|
8
|
+
export { TraceConfig } from './config';
|
|
9
|
+
export { TraceStorage } from './storage';
|
|
10
|
+
export type { ITraceStorage } from './storage';
|
|
11
|
+
export { TraceContext } from './context';
|
|
12
|
+
export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes';
|
|
13
|
+
export type { TraceDashboardOptions, TraceDashboardRegistrationOptions } from './dashboard/routes';
|
|
14
|
+
export { AuthWatcher } from './watchers/AuthWatcher';
|
|
15
|
+
export { BatchWatcher } from './watchers/BatchWatcher';
|
|
16
|
+
export { CacheWatcher } from './watchers/CacheWatcher';
|
|
17
|
+
export { CommandWatcher } from './watchers/CommandWatcher';
|
|
18
|
+
export { DumpWatcher } from './watchers/DumpWatcher';
|
|
19
|
+
export { EventWatcher } from './watchers/EventWatcher';
|
|
20
|
+
export { ExceptionWatcher } from './watchers/ExceptionWatcher';
|
|
21
|
+
export { GateWatcher } from './watchers/GateWatcher';
|
|
22
|
+
export { HttpClientWatcher } from './watchers/HttpClientWatcher';
|
|
23
|
+
export { HttpWatcher } from './watchers/HttpWatcher';
|
|
24
|
+
export { JobWatcher } from './watchers/JobWatcher';
|
|
25
|
+
export { LogWatcher } from './watchers/LogWatcher';
|
|
26
|
+
export { MailWatcher } from './watchers/MailWatcher';
|
|
27
|
+
export { MiddlewareWatcher } from './watchers/MiddlewareWatcher';
|
|
28
|
+
export { ModelWatcher } from './watchers/ModelWatcher';
|
|
29
|
+
export { NotificationWatcher } from './watchers/NotificationWatcher';
|
|
30
|
+
export { QueryWatcher } from './watchers/QueryWatcher';
|
|
31
|
+
export { RedisWatcher } from './watchers/RedisWatcher';
|
|
32
|
+
export { ScheduleWatcher } from './watchers/ScheduleWatcher';
|
|
33
|
+
export { ViewWatcher } from './watchers/ViewWatcher';
|
|
34
|
+
export { EntryType } from './types';
|
|
35
|
+
export type { AuthContent, BatchContent, CacheContent, ClientRequestContent, CommandContent, DumpContent, EntryTypeValue, EventContent, ExceptionContent, GateContent, ITraceConfig, ITraceEntry, ITraceWatcher, ITraceWatcherConfig, JobContent, LogContent, MailContent, MiddlewareContent, ModelContent, NotificationContent, QueryContent, RedactionConfig, RedisContent, RequestContent, ScheduleContent, TraceConfigOverrides, ViewContent, WatcherToggles, } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zintrust/trace — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* Zero side-effects. Import watchers, storage, dashboard, and config
|
|
5
|
+
* individually. For full auto-initialisation, use:
|
|
6
|
+
* import '@zintrust/trace/register';
|
|
7
|
+
*/
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// Config
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
export { TraceConfig } from './config';
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Storage
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
export { TraceStorage } from './storage';
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Context
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
export { TraceContext } from './context';
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Dashboard
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes';
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Watchers (named re-exports for use with custom wiring)
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
export { AuthWatcher } from './watchers/AuthWatcher';
|
|
28
|
+
export { BatchWatcher } from './watchers/BatchWatcher';
|
|
29
|
+
export { CacheWatcher } from './watchers/CacheWatcher';
|
|
30
|
+
export { CommandWatcher } from './watchers/CommandWatcher';
|
|
31
|
+
export { DumpWatcher } from './watchers/DumpWatcher';
|
|
32
|
+
export { EventWatcher } from './watchers/EventWatcher';
|
|
33
|
+
export { ExceptionWatcher } from './watchers/ExceptionWatcher';
|
|
34
|
+
export { GateWatcher } from './watchers/GateWatcher';
|
|
35
|
+
export { HttpClientWatcher } from './watchers/HttpClientWatcher';
|
|
36
|
+
export { HttpWatcher } from './watchers/HttpWatcher';
|
|
37
|
+
export { JobWatcher } from './watchers/JobWatcher';
|
|
38
|
+
export { LogWatcher } from './watchers/LogWatcher';
|
|
39
|
+
export { MailWatcher } from './watchers/MailWatcher';
|
|
40
|
+
export { MiddlewareWatcher } from './watchers/MiddlewareWatcher';
|
|
41
|
+
export { ModelWatcher } from './watchers/ModelWatcher';
|
|
42
|
+
export { NotificationWatcher } from './watchers/NotificationWatcher';
|
|
43
|
+
export { QueryWatcher } from './watchers/QueryWatcher';
|
|
44
|
+
export { RedisWatcher } from './watchers/RedisWatcher';
|
|
45
|
+
export { ScheduleWatcher } from './watchers/ScheduleWatcher';
|
|
46
|
+
export { ViewWatcher } from './watchers/ViewWatcher';
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Types
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
export { EntryType } from './types';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerEntriesTable
|
|
3
|
+
* Creates the main entries table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { type IDatabase } from '@zintrust/core';
|
|
6
|
+
export interface Migration {
|
|
7
|
+
up(db: IDatabase): Promise<void>;
|
|
8
|
+
down(db: IDatabase): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export declare const migration: Migration;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerEntriesTable
|
|
3
|
+
* Creates the main entries table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core';
|
|
6
|
+
export const migration = {
|
|
7
|
+
async up(db) {
|
|
8
|
+
const schema = MigrationSchema.create(db);
|
|
9
|
+
await schema.create('zin_debugger_entries', (table) => {
|
|
10
|
+
table.id();
|
|
11
|
+
table.uuid('uuid').unique();
|
|
12
|
+
table.string('batch_id');
|
|
13
|
+
table.string('family_hash').nullable();
|
|
14
|
+
table.string('type');
|
|
15
|
+
table.text('content');
|
|
16
|
+
table.boolean('is_latest').default(true);
|
|
17
|
+
table.integer('created_at');
|
|
18
|
+
table.index('batch_id');
|
|
19
|
+
table.index('family_hash');
|
|
20
|
+
table.index('created_at');
|
|
21
|
+
table.index(['type', 'is_latest']);
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
async down(db) {
|
|
25
|
+
const schema = MigrationSchema.create(db);
|
|
26
|
+
await schema.dropIfExists('zin_debugger_entries');
|
|
27
|
+
},
|
|
28
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerEntriesTagsTable
|
|
3
|
+
* Creates the tag join table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { type IDatabase } from '@zintrust/core';
|
|
6
|
+
export interface Migration {
|
|
7
|
+
up(db: IDatabase): Promise<void>;
|
|
8
|
+
down(db: IDatabase): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export declare const migration: Migration;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerEntriesTagsTable
|
|
3
|
+
* Creates the tag join table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core';
|
|
6
|
+
export const migration = {
|
|
7
|
+
async up(db) {
|
|
8
|
+
const schema = MigrationSchema.create(db);
|
|
9
|
+
await schema.create('zin_debugger_entries_tags', (table) => {
|
|
10
|
+
table.string('entry_uuid');
|
|
11
|
+
table.string('tag');
|
|
12
|
+
table.unique(['entry_uuid', 'tag']);
|
|
13
|
+
table.index('tag');
|
|
14
|
+
table.foreign('entry_uuid').references('uuid').on('zin_debugger_entries').onDelete('CASCADE');
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
async down(db) {
|
|
18
|
+
const schema = MigrationSchema.create(db);
|
|
19
|
+
await schema.dropIfExists('zin_debugger_entries_tags');
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerMonitoringTable
|
|
3
|
+
* Creates the tag watchlist table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { type IDatabase } from '@zintrust/core';
|
|
6
|
+
export interface Migration {
|
|
7
|
+
up(db: IDatabase): Promise<void>;
|
|
8
|
+
down(db: IDatabase): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export declare const migration: Migration;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration: CreateZinDebuggerMonitoringTable
|
|
3
|
+
* Creates the tag watchlist table for @zintrust/system-debugger
|
|
4
|
+
*/
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core';
|
|
6
|
+
export const migration = {
|
|
7
|
+
async up(db) {
|
|
8
|
+
const schema = MigrationSchema.create(db);
|
|
9
|
+
await schema.create('zin_debugger_monitoring', (table) => {
|
|
10
|
+
table.string('tag').primary();
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
async down(db) {
|
|
14
|
+
const schema = MigrationSchema.create(db);
|
|
15
|
+
await schema.dropIfExists('zin_debugger_monitoring');
|
|
16
|
+
},
|
|
17
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migrations index for @zintrust/system-debugger
|
|
3
|
+
* Export all migrations as an ordered array.
|
|
4
|
+
*/
|
|
5
|
+
export type { Migration } from './20260331000001_create_zin_debugger_entries_table';
|
|
6
|
+
export declare const migrations: import("./20260331000001_create_zin_debugger_entries_table").Migration[];
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { migration as createEntries } from './20260331000001_create_zin_debugger_entries_table.js';
|
|
2
|
+
import { migration as createEntriesTags } from './20260331000002_create_zin_debugger_entries_tags_table.js';
|
|
3
|
+
import { migration as createMonitoring } from './20260331000003_create_zin_debugger_monitoring_table.js';
|
|
4
|
+
export const migrations = [createEntries, createEntriesTags, createMonitoring];
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type {};
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type {};
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zintrust/trace register side-effect module.
|
|
3
|
+
*
|
|
4
|
+
* For plugin-file activation, prefer:
|
|
5
|
+
* import '@zintrust/trace/plugin';
|
|
6
|
+
*
|
|
7
|
+
* The framework boot layer will lazy-load this register module once the app
|
|
8
|
+
* runtime is ready. Importing this file directly is still supported for
|
|
9
|
+
* advanced manual bootstrap flows that intentionally activate the trace
|
|
10
|
+
* after databases and the kernel are available.
|
|
11
|
+
*
|
|
12
|
+
* Config is read from environment variables (TRACE_* keys) matching
|
|
13
|
+
* the defaults in TraceConfig. For custom overrides supply them via
|
|
14
|
+
* calling `initTrace(overrides)` instead.
|
|
15
|
+
*
|
|
16
|
+
* Routes are NOT auto-mounted here. Wire the dashboard into your router:
|
|
17
|
+
* import { registerTraceDashboard } from '@zintrust/trace/ui';
|
|
18
|
+
* registerTraceDashboard(router, {
|
|
19
|
+
* middleware: ['admin'],
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
import { TraceConfig } from './config';
|
|
23
|
+
import { TraceContext } from './context';
|
|
24
|
+
import { TraceStorage } from './storage';
|
|
25
|
+
const globalTraceRegisterState = globalThis;
|
|
26
|
+
globalTraceRegisterState.__zintrust_system_trace_plugin_requested__ = true;
|
|
27
|
+
const traceAlreadyInitialized = globalTraceRegisterState.__zintrust_system_trace_register_initialized__ === true;
|
|
28
|
+
if (!traceAlreadyInitialized) {
|
|
29
|
+
globalTraceRegisterState.__zintrust_system_trace_register_initialized__ = true;
|
|
30
|
+
}
|
|
31
|
+
const importCore = async () => {
|
|
32
|
+
try {
|
|
33
|
+
return await import('@zintrust/core');
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const resolveRegisterMiddleware = () => {
|
|
40
|
+
const globalMiddlewareRegistrarState = globalThis;
|
|
41
|
+
return (middleware) => {
|
|
42
|
+
const registerMiddleware = globalMiddlewareRegistrarState.__zintrust_register_global_middleware__;
|
|
43
|
+
if (typeof registerMiddleware === 'function') {
|
|
44
|
+
registerMiddleware(middleware);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
globalMiddlewareRegistrarState.__zintrust_pending_global_middlewares__ ??= [];
|
|
48
|
+
globalMiddlewareRegistrarState.__zintrust_pending_global_middlewares__.push(middleware);
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const resolveTraceConnectionName = (env, configuredConnection) => {
|
|
52
|
+
const resolveDefaultConnection = () => {
|
|
53
|
+
const defaultConnection = env?.get('DB_CONNECTION', '').trim() ?? '';
|
|
54
|
+
if (defaultConnection === '' || defaultConnection === 'default')
|
|
55
|
+
return 'default';
|
|
56
|
+
return defaultConnection;
|
|
57
|
+
};
|
|
58
|
+
const explicitConnection = configuredConnection?.trim();
|
|
59
|
+
if (explicitConnection !== undefined && explicitConnection !== '') {
|
|
60
|
+
return explicitConnection === 'default' ? resolveDefaultConnection() : explicitConnection;
|
|
61
|
+
}
|
|
62
|
+
return resolveDefaultConnection();
|
|
63
|
+
};
|
|
64
|
+
const core = (await importCore());
|
|
65
|
+
const Env = core.Env;
|
|
66
|
+
if (!traceAlreadyInitialized && Env) {
|
|
67
|
+
const enabled = Env.getBool('TRACE_ENABLED', false);
|
|
68
|
+
if (enabled) {
|
|
69
|
+
const connection = Env.get('TRACE_DB_CONNECTION', '') || undefined;
|
|
70
|
+
const pruneAfterHours = Env.getInt('TRACE_PRUNE_HOURS', 24);
|
|
71
|
+
const slowQueryThreshold = Env.getInt('TRACE_SLOW_QUERY_MS', 100);
|
|
72
|
+
const logMinLevel = Env.get('TRACE_LOG_LEVEL', 'info');
|
|
73
|
+
const config = TraceConfig.merge({
|
|
74
|
+
enabled,
|
|
75
|
+
connection,
|
|
76
|
+
pruneAfterHours,
|
|
77
|
+
slowQueryThreshold,
|
|
78
|
+
logMinLevel,
|
|
79
|
+
});
|
|
80
|
+
const db = core.useDatabase?.(undefined, resolveTraceConnectionName(Env, connection));
|
|
81
|
+
if (db) {
|
|
82
|
+
const storage = TraceStorage.resolveStorage(db);
|
|
83
|
+
if (core.RequestContext) {
|
|
84
|
+
TraceContext.setRequestContextImpl(core.RequestContext);
|
|
85
|
+
}
|
|
86
|
+
const [{ HttpWatcher }, { QueryWatcher }, { LogWatcher }, { ExceptionWatcher }, { JobWatcher }, { CacheWatcher }, { ScheduleWatcher }, { MailWatcher }, { AuthWatcher }, { EventWatcher }, { ModelWatcher }, { NotificationWatcher }, { RedisWatcher }, { GateWatcher }, { MiddlewareWatcher }, { CommandWatcher }, { BatchWatcher }, { DumpWatcher }, { ViewWatcher }, { HttpClientWatcher },] = await Promise.all([
|
|
87
|
+
import('./watchers/HttpWatcher'),
|
|
88
|
+
import('./watchers/QueryWatcher'),
|
|
89
|
+
import('./watchers/LogWatcher'),
|
|
90
|
+
import('./watchers/ExceptionWatcher'),
|
|
91
|
+
import('./watchers/JobWatcher'),
|
|
92
|
+
import('./watchers/CacheWatcher'),
|
|
93
|
+
import('./watchers/ScheduleWatcher'),
|
|
94
|
+
import('./watchers/MailWatcher'),
|
|
95
|
+
import('./watchers/AuthWatcher'),
|
|
96
|
+
import('./watchers/EventWatcher'),
|
|
97
|
+
import('./watchers/ModelWatcher'),
|
|
98
|
+
import('./watchers/NotificationWatcher'),
|
|
99
|
+
import('./watchers/RedisWatcher'),
|
|
100
|
+
import('./watchers/GateWatcher'),
|
|
101
|
+
import('./watchers/MiddlewareWatcher'),
|
|
102
|
+
import('./watchers/CommandWatcher'),
|
|
103
|
+
import('./watchers/BatchWatcher'),
|
|
104
|
+
import('./watchers/DumpWatcher'),
|
|
105
|
+
import('./watchers/ViewWatcher'),
|
|
106
|
+
import('./watchers/HttpClientWatcher'),
|
|
107
|
+
]);
|
|
108
|
+
const watcherArgs = { storage, config, db };
|
|
109
|
+
HttpWatcher.register({ ...watcherArgs, registerMiddleware: resolveRegisterMiddleware() });
|
|
110
|
+
QueryWatcher.register(watcherArgs);
|
|
111
|
+
LogWatcher.register(watcherArgs);
|
|
112
|
+
ExceptionWatcher.register(watcherArgs);
|
|
113
|
+
JobWatcher.register(watcherArgs);
|
|
114
|
+
CacheWatcher.register(watcherArgs);
|
|
115
|
+
ScheduleWatcher.register(watcherArgs);
|
|
116
|
+
MailWatcher.register(watcherArgs);
|
|
117
|
+
AuthWatcher.register(watcherArgs);
|
|
118
|
+
EventWatcher.register(watcherArgs);
|
|
119
|
+
ModelWatcher.register(watcherArgs);
|
|
120
|
+
NotificationWatcher.register(watcherArgs);
|
|
121
|
+
RedisWatcher.register(watcherArgs);
|
|
122
|
+
GateWatcher.register(watcherArgs);
|
|
123
|
+
MiddlewareWatcher.register(watcherArgs);
|
|
124
|
+
CommandWatcher.register(watcherArgs);
|
|
125
|
+
BatchWatcher.register(watcherArgs);
|
|
126
|
+
DumpWatcher.register(watcherArgs);
|
|
127
|
+
ViewWatcher.register(watcherArgs);
|
|
128
|
+
HttpClientWatcher.register(watcherArgs);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// eslint-disable-next-line no-console
|
|
132
|
+
console.warn('[trace] Could not resolve database connection - skipping init.');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (!traceAlreadyInitialized) {
|
|
137
|
+
// Running outside a ZinTrust project - skip init silently.
|
|
138
|
+
// eslint-disable-next-line no-console
|
|
139
|
+
console.warn('[trace] @zintrust/core not found - trace will not be activated.');
|
|
140
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TraceStorage — sealed namespace wrapping the D1/SQLite driver.
|
|
3
|
+
* Resolves the correct IDatabase from the app config, then delegates all
|
|
4
|
+
* read/write operations to the trace storage facade.
|
|
5
|
+
*/
|
|
6
|
+
import type { IDatabase } from '@zintrust/core';
|
|
7
|
+
import type { ITraceStorage } from '../types';
|
|
8
|
+
export declare const TraceStorage: Readonly<{
|
|
9
|
+
resolveStorage: (db: IDatabase) => ITraceStorage;
|
|
10
|
+
reset: () => void;
|
|
11
|
+
familyHash: (input: string) => string;
|
|
12
|
+
}>;
|
|
13
|
+
export { type ITraceStorage } from '../types';
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { familyHash } from '../utils/familyHash';
|
|
2
|
+
const TABLE_ENTRIES = 'zin_trace_entries';
|
|
3
|
+
const TABLE_TAGS = 'zin_trace_entries_tags';
|
|
4
|
+
const TABLE_MONITORING = 'zin_trace_monitoring';
|
|
5
|
+
const generateUuid = () => crypto.randomUUID();
|
|
6
|
+
const rowToEntry = (row, tags) => ({
|
|
7
|
+
uuid: row.uuid,
|
|
8
|
+
batchId: row.batch_id,
|
|
9
|
+
familyHash: row.family_hash ?? undefined,
|
|
10
|
+
type: row.type,
|
|
11
|
+
content: JSON.parse(row.content),
|
|
12
|
+
tags,
|
|
13
|
+
isLatest: Boolean(row.is_latest),
|
|
14
|
+
createdAt: row.created_at,
|
|
15
|
+
});
|
|
16
|
+
const insertTags = async (db, uuid, tags) => {
|
|
17
|
+
if (tags.length === 0)
|
|
18
|
+
return;
|
|
19
|
+
await Promise.all(tags.map(async (tag) => {
|
|
20
|
+
await db.execute(`INSERT OR IGNORE INTO ${TABLE_TAGS} (entry_uuid, tag) VALUES (?, ?)`, [
|
|
21
|
+
uuid,
|
|
22
|
+
tag,
|
|
23
|
+
]);
|
|
24
|
+
}));
|
|
25
|
+
};
|
|
26
|
+
const buildEntryFilters = (opts) => {
|
|
27
|
+
const conditions = [];
|
|
28
|
+
const params = [];
|
|
29
|
+
if (opts.type) {
|
|
30
|
+
conditions.push('e.type = ?');
|
|
31
|
+
params.push(opts.type);
|
|
32
|
+
}
|
|
33
|
+
if (opts.batchId) {
|
|
34
|
+
conditions.push('e.batch_id = ?');
|
|
35
|
+
params.push(opts.batchId);
|
|
36
|
+
}
|
|
37
|
+
if (opts.from) {
|
|
38
|
+
conditions.push('e.created_at >= ?');
|
|
39
|
+
params.push(opts.from);
|
|
40
|
+
}
|
|
41
|
+
if (opts.to) {
|
|
42
|
+
conditions.push('e.created_at <= ?');
|
|
43
|
+
params.push(opts.to);
|
|
44
|
+
}
|
|
45
|
+
let joinClause = '';
|
|
46
|
+
if (opts.tag) {
|
|
47
|
+
joinClause = `INNER JOIN ${TABLE_TAGS} t ON t.entry_uuid = e.uuid AND t.tag = ?`;
|
|
48
|
+
params.unshift(opts.tag);
|
|
49
|
+
}
|
|
50
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
51
|
+
const countParams = opts.tag ? [opts.tag, ...params.slice(1)] : [...params];
|
|
52
|
+
return { joinClause, whereClause, params, countParams };
|
|
53
|
+
};
|
|
54
|
+
const loadTagsByUuid = async (db, uuids) => {
|
|
55
|
+
const tagsByUuid = new Map();
|
|
56
|
+
if (uuids.length === 0)
|
|
57
|
+
return tagsByUuid;
|
|
58
|
+
const tagRows = (await db.query(`SELECT entry_uuid, tag FROM ${TABLE_TAGS} WHERE entry_uuid IN (${uuids.map(() => '?').join(',')})`, uuids));
|
|
59
|
+
for (const tagRow of tagRows) {
|
|
60
|
+
const tags = tagsByUuid.get(tagRow.entry_uuid) ?? [];
|
|
61
|
+
tags.push(tagRow.tag);
|
|
62
|
+
tagsByUuid.set(tagRow.entry_uuid, tags);
|
|
63
|
+
}
|
|
64
|
+
return tagsByUuid;
|
|
65
|
+
};
|
|
66
|
+
// The storage facade intentionally groups related DB operations in one factory.
|
|
67
|
+
// eslint-disable-next-line max-lines-per-function
|
|
68
|
+
const createStorage = (db) => {
|
|
69
|
+
const writeEntry = async (entry) => {
|
|
70
|
+
const uuid = entry.uuid || generateUuid();
|
|
71
|
+
await db.execute(`INSERT INTO ${TABLE_ENTRIES} (uuid, batch_id, family_hash, type, content, is_latest, created_at)
|
|
72
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
|
73
|
+
uuid,
|
|
74
|
+
entry.batchId,
|
|
75
|
+
entry.familyHash ?? null,
|
|
76
|
+
entry.type,
|
|
77
|
+
JSON.stringify(entry.content),
|
|
78
|
+
entry.isLatest ? 1 : 0,
|
|
79
|
+
entry.createdAt,
|
|
80
|
+
]);
|
|
81
|
+
await insertTags(db, uuid, entry.tags);
|
|
82
|
+
};
|
|
83
|
+
const updateEntry = async (uuid, patch) => {
|
|
84
|
+
const sets = [];
|
|
85
|
+
const params = [];
|
|
86
|
+
if (patch.content !== undefined) {
|
|
87
|
+
sets.push('content = ?');
|
|
88
|
+
params.push(JSON.stringify(patch.content));
|
|
89
|
+
}
|
|
90
|
+
if (patch.isLatest !== undefined) {
|
|
91
|
+
sets.push('is_latest = ?');
|
|
92
|
+
params.push(patch.isLatest ? 1 : 0);
|
|
93
|
+
}
|
|
94
|
+
if (sets.length === 0)
|
|
95
|
+
return;
|
|
96
|
+
params.push(uuid);
|
|
97
|
+
await db.execute(`UPDATE ${TABLE_ENTRIES} SET ${sets.join(', ')} WHERE uuid = ?`, params);
|
|
98
|
+
};
|
|
99
|
+
const markFamilyStale = async (hash, exceptUuid) => {
|
|
100
|
+
await db.execute(`UPDATE ${TABLE_ENTRIES} SET is_latest = 0
|
|
101
|
+
WHERE family_hash = ? AND uuid != ? AND is_latest = 1`, [hash, exceptUuid]);
|
|
102
|
+
};
|
|
103
|
+
const queryEntries = async (opts) => {
|
|
104
|
+
const page = opts.page ?? 1;
|
|
105
|
+
const perPage = opts.perPage ?? 50;
|
|
106
|
+
const offset = (page - 1) * perPage;
|
|
107
|
+
const { joinClause, whereClause, params, countParams } = buildEntryFilters(opts);
|
|
108
|
+
const countResult = (await db.queryOne(`SELECT COUNT(*) as cnt FROM ${TABLE_ENTRIES} e ${joinClause} ${whereClause}`, countParams));
|
|
109
|
+
const total = countResult?.cnt ?? 0;
|
|
110
|
+
const rows = (await db.query(`SELECT e.id, e.uuid, e.batch_id, e.family_hash, e.type, e.content, e.is_latest, e.created_at
|
|
111
|
+
FROM ${TABLE_ENTRIES} e ${joinClause} ${whereClause}
|
|
112
|
+
ORDER BY e.created_at DESC, e.id DESC
|
|
113
|
+
LIMIT ? OFFSET ?`, [...params, perPage, offset]));
|
|
114
|
+
const tagsByUuid = await loadTagsByUuid(db, rows.map((row) => row.uuid));
|
|
115
|
+
return {
|
|
116
|
+
data: rows.map((row) => rowToEntry(row, tagsByUuid.get(row.uuid) ?? [])),
|
|
117
|
+
total,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const getEntry = async (uuid) => {
|
|
121
|
+
const row = (await db.queryOne(`SELECT id, uuid, batch_id, family_hash, type, content, is_latest, created_at
|
|
122
|
+
FROM ${TABLE_ENTRIES}
|
|
123
|
+
WHERE uuid = ?`, [uuid]));
|
|
124
|
+
if (!row)
|
|
125
|
+
return null;
|
|
126
|
+
const tags = (await db.query(`SELECT tag FROM ${TABLE_TAGS} WHERE entry_uuid = ?`, [
|
|
127
|
+
uuid,
|
|
128
|
+
]));
|
|
129
|
+
return rowToEntry(row, tags.map((tag) => tag.tag));
|
|
130
|
+
};
|
|
131
|
+
const getBatch = async (batchId) => {
|
|
132
|
+
const rows = (await db.query(`SELECT id, uuid, batch_id, family_hash, type, content, is_latest, created_at
|
|
133
|
+
FROM ${TABLE_ENTRIES}
|
|
134
|
+
WHERE batch_id = ?
|
|
135
|
+
ORDER BY created_at ASC, id ASC`, [batchId]));
|
|
136
|
+
if (rows.length === 0)
|
|
137
|
+
return [];
|
|
138
|
+
const tagsByUuid = await loadTagsByUuid(db, rows.map((row) => row.uuid));
|
|
139
|
+
return rows.map((row) => rowToEntry(row, tagsByUuid.get(row.uuid) ?? []));
|
|
140
|
+
};
|
|
141
|
+
const prune = async (olderThanMs, keepExceptions = false) => {
|
|
142
|
+
const countResult = (await db.queryOne(`SELECT COUNT(*) as cnt FROM ${TABLE_ENTRIES}
|
|
143
|
+
WHERE created_at < ?
|
|
144
|
+
${keepExceptions ? "AND type != 'exception'" : ''}`, [olderThanMs]));
|
|
145
|
+
const deleted = countResult?.cnt ?? 0;
|
|
146
|
+
if (deleted === 0)
|
|
147
|
+
return 0;
|
|
148
|
+
await db.execute(`DELETE FROM ${TABLE_ENTRIES}
|
|
149
|
+
WHERE created_at < ?
|
|
150
|
+
${keepExceptions ? "AND type != 'exception'" : ''}`, [olderThanMs]);
|
|
151
|
+
return deleted;
|
|
152
|
+
};
|
|
153
|
+
const clear = async () => {
|
|
154
|
+
await db.execute(`DELETE FROM ${TABLE_ENTRIES}`, []);
|
|
155
|
+
};
|
|
156
|
+
const getMonitoring = async () => {
|
|
157
|
+
const rows = (await db.query(`SELECT tag FROM ${TABLE_MONITORING}`, []));
|
|
158
|
+
return rows.map((row) => row.tag);
|
|
159
|
+
};
|
|
160
|
+
const addMonitoring = async (tag) => {
|
|
161
|
+
await db.execute(`INSERT OR IGNORE INTO ${TABLE_MONITORING} (tag) VALUES (?)`, [tag]);
|
|
162
|
+
};
|
|
163
|
+
const removeMonitoring = async (tag) => {
|
|
164
|
+
await db.execute(`DELETE FROM ${TABLE_MONITORING} WHERE tag = ?`, [tag]);
|
|
165
|
+
};
|
|
166
|
+
const stats = async () => {
|
|
167
|
+
const rows = (await db.query(`SELECT type, COUNT(*) as cnt FROM ${TABLE_ENTRIES} GROUP BY type`, []));
|
|
168
|
+
const output = {};
|
|
169
|
+
for (const row of rows) {
|
|
170
|
+
output[row.type] = row.cnt;
|
|
171
|
+
}
|
|
172
|
+
return output;
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
writeEntry,
|
|
176
|
+
updateEntry,
|
|
177
|
+
markFamilyStale,
|
|
178
|
+
queryEntries,
|
|
179
|
+
getEntry,
|
|
180
|
+
getBatch,
|
|
181
|
+
prune,
|
|
182
|
+
clear,
|
|
183
|
+
getMonitoring,
|
|
184
|
+
addMonitoring,
|
|
185
|
+
removeMonitoring,
|
|
186
|
+
stats,
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
const resolveStorage = (db) => {
|
|
190
|
+
return createStorage(db);
|
|
191
|
+
};
|
|
192
|
+
const reset = () => {
|
|
193
|
+
return;
|
|
194
|
+
};
|
|
195
|
+
export const TraceStorage = Object.freeze({ resolveStorage, reset, familyHash });
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TraceStorage — sealed namespace wrapping the D1/SQLite driver.
|
|
3
|
+
* Resolves the correct IDatabase from the app config, then delegates all
|
|
4
|
+
* read/write operations to the trace storage facade.
|
|
5
|
+
*/
|
|
6
|
+
import type { IDatabase } from '@zintrust/core';
|
|
7
|
+
import type { ITraceStorage } from '../types';
|
|
8
|
+
export declare const TraceStorage: Readonly<{
|
|
9
|
+
resolveStorage: (db: IDatabase) => ITraceStorage;
|
|
10
|
+
reset: () => void;
|
|
11
|
+
familyHash: (input: string) => string;
|
|
12
|
+
}>;
|
|
13
|
+
export { type ITraceStorage } from '../types';
|