@zintrust/trace 2.0.0 → 2.1.3
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 +24 -0
- package/dist/TraceConnection.d.ts +1 -1
- package/dist/build-manifest.json +71 -59
- package/dist/dashboard/handlers.d.ts +1 -1
- package/dist/dashboard/routes.d.ts +1 -6
- package/dist/dashboard/routes.js +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/ingest/TraceIngestGateway.d.ts +1 -1
- package/dist/ingest/TraceIngestGateway.js +5 -1
- package/dist/migrations/20260331000001_create_zin_trace_entries_table.d.ts +1 -1
- package/dist/migrations/20260331000001_create_zin_trace_entries_table.js +1 -1
- package/dist/migrations/20260331000002_create_zin_trace_entries_tags_table.d.ts +1 -1
- package/dist/migrations/20260331000002_create_zin_trace_entries_tags_table.js +1 -1
- package/dist/migrations/20260331000003_create_zin_trace_monitoring_table.d.ts +1 -1
- package/dist/migrations/20260331000003_create_zin_trace_monitoring_table.js +1 -1
- package/dist/migrations/20260407193000_widen_trace_created_at_for_sql.d.ts +1 -1
- package/dist/migrations/20260407193000_widen_trace_created_at_for_sql.js +1 -1
- package/dist/register.js +9 -1
- package/dist/runtime/BackgroundTaskScheduler.d.ts +38 -0
- package/dist/runtime/BackgroundTaskScheduler.js +105 -0
- package/dist/storage/ProxyTraceStorage.js +2 -1
- package/dist/storage/TraceServiceTag.js +1 -1
- package/dist/storage/TraceStorage.d.ts +1 -1
- package/dist/types.d.ts +3 -1
- package/dist/watchers/ExceptionWatcher.js +10 -2
- package/dist/watchers/HttpClientWatcher.js +10 -2
- package/dist/watchers/HttpWatcher.js +12 -4
- package/dist/watchers/LogWatcher.js +8 -3
- package/dist/watchers/QueryWatcher.js +10 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -91,6 +91,30 @@ import serviceManifest from './bootstrap/service-manifest';
|
|
|
91
91
|
ProjectRuntime.set({ serviceManifest });
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
+
#### Cloudflare Workers waitUntil Support
|
|
95
|
+
|
|
96
|
+
For reliable trace persistence in Cloudflare Workers, set the execution context in your Worker fetch handler:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// src/worker.ts or your Worker entrypoint
|
|
100
|
+
import { BackgroundTaskScheduler } from '@zintrust/trace';
|
|
101
|
+
|
|
102
|
+
export default {
|
|
103
|
+
async fetch(request, env, ctx) {
|
|
104
|
+
// Enable reliable trace persistence via ctx.waitUntil()
|
|
105
|
+
BackgroundTaskScheduler.setExecutionContext(ctx);
|
|
106
|
+
|
|
107
|
+
// Initialize and run your ZinTrust app
|
|
108
|
+
const app = createApp();
|
|
109
|
+
await app.boot();
|
|
110
|
+
|
|
111
|
+
return app.handle(request);
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
This ensures trace writes complete reliably even after the HTTP response is sent. Without this, trace writes may be lost in Workers environments. See [docs/WORKER_WAITUNTIL_SUPPORT.md](./docs/WORKER_WAITUNTIL_SUPPORT.md) for details.
|
|
117
|
+
|
|
94
118
|
Why this is the preferred path:
|
|
95
119
|
|
|
96
120
|
- The plugin files are the framework-owned opt-in point that ZinTrust already auto-loads during boot.
|
package/dist/build-manifest.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zintrust/trace",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"buildDate": "2026-05-
|
|
3
|
+
"version": "2.1.3",
|
|
4
|
+
"buildDate": "2026-05-27T04:11:41.644Z",
|
|
5
5
|
"buildEnvironment": {
|
|
6
|
-
"node": "
|
|
7
|
-
"platform": "
|
|
8
|
-
"arch": "
|
|
6
|
+
"node": "v22.22.1",
|
|
7
|
+
"platform": "darwin",
|
|
8
|
+
"arch": "arm64"
|
|
9
9
|
},
|
|
10
10
|
"git": {
|
|
11
|
-
"commit": "
|
|
12
|
-
"branch": "
|
|
11
|
+
"commit": "c82f6263",
|
|
12
|
+
"branch": "release"
|
|
13
13
|
},
|
|
14
14
|
"package": {
|
|
15
15
|
"engines": {
|
|
@@ -22,13 +22,17 @@
|
|
|
22
22
|
},
|
|
23
23
|
"files": {
|
|
24
24
|
"TraceConnection.d.ts": {
|
|
25
|
-
"size":
|
|
26
|
-
"sha256": "
|
|
25
|
+
"size": 1509,
|
|
26
|
+
"sha256": "09a6d63712628d917cd6f64550ebf527c7b419da369983a57151808d32a5a666"
|
|
27
27
|
},
|
|
28
28
|
"TraceConnection.js": {
|
|
29
29
|
"size": 4640,
|
|
30
30
|
"sha256": "c51cc312046b6b2bbe1673f1ff9508425cc7140a1d2341907f67aa36069c09f9"
|
|
31
31
|
},
|
|
32
|
+
"build-manifest.json": {
|
|
33
|
+
"size": 15929,
|
|
34
|
+
"sha256": "be6384c7124a8d2d845db13d13bdbbafd294b758fb3f4a64af6b4bfdb26c4f86"
|
|
35
|
+
},
|
|
32
36
|
"cli-register.d.ts": {
|
|
33
37
|
"size": 255,
|
|
34
38
|
"sha256": "da8d689fe5ef32e97e755f28017e4d3cb1aa63489073a71907ea41ad5761ede9"
|
|
@@ -54,20 +58,20 @@
|
|
|
54
58
|
"sha256": "00a82a58e18fb33934f449a9fb379a2f90023570ceed1676eae0aed59b02ddeb"
|
|
55
59
|
},
|
|
56
60
|
"dashboard/handlers.d.ts": {
|
|
57
|
-
"size":
|
|
58
|
-
"sha256": "
|
|
61
|
+
"size": 998,
|
|
62
|
+
"sha256": "788a2c71fdd77adbcef3e41bffe7ddf07f04bc1bdd10e6f19cfcd3ba6b9e858b"
|
|
59
63
|
},
|
|
60
64
|
"dashboard/handlers.js": {
|
|
61
65
|
"size": 10011,
|
|
62
66
|
"sha256": "a5f7f3d624a844df27ddc9d4908e73d91a4aac9b2aac1aefff1f391913353897"
|
|
63
67
|
},
|
|
64
68
|
"dashboard/routes.d.ts": {
|
|
65
|
-
"size":
|
|
66
|
-
"sha256": "
|
|
69
|
+
"size": 797,
|
|
70
|
+
"sha256": "988be2431f7cde857403dcbd84949f425c1ed668b3c41faa3d104dd891d6c0dc"
|
|
67
71
|
},
|
|
68
72
|
"dashboard/routes.js": {
|
|
69
|
-
"size":
|
|
70
|
-
"sha256": "
|
|
73
|
+
"size": 2627,
|
|
74
|
+
"sha256": "b59d95e8bfe256b2904d6000d672036faa68983e296cd8c82ae782cb0b804fcb"
|
|
71
75
|
},
|
|
72
76
|
"dashboard/ui.d.ts": {
|
|
73
77
|
"size": 117,
|
|
@@ -78,52 +82,52 @@
|
|
|
78
82
|
"sha256": "6a19ca4b079944cf0070633a0694de28d034f78ff2d1f02af00707cd2434751f"
|
|
79
83
|
},
|
|
80
84
|
"index.d.ts": {
|
|
81
|
-
"size":
|
|
82
|
-
"sha256": "
|
|
85
|
+
"size": 2770,
|
|
86
|
+
"sha256": "83fdbdc30ce9c926c35cb5a95f480797cb7d12c8a52237dd7a19ec62dfe93dac"
|
|
83
87
|
},
|
|
84
88
|
"index.js": {
|
|
85
|
-
"size":
|
|
86
|
-
"sha256": "
|
|
89
|
+
"size": 3670,
|
|
90
|
+
"sha256": "cb7b0d00db90067076a82dbb339b942dee72803cfe730303081f5102008d1140"
|
|
87
91
|
},
|
|
88
92
|
"ingest/TraceIngestGateway.d.ts": {
|
|
89
|
-
"size":
|
|
90
|
-
"sha256": "
|
|
93
|
+
"size": 791,
|
|
94
|
+
"sha256": "7362cb112369bfa2091cb10ce3fc557bc9be5bf9e17abeba32e23bc58bbf9f57"
|
|
91
95
|
},
|
|
92
96
|
"ingest/TraceIngestGateway.js": {
|
|
93
|
-
"size":
|
|
94
|
-
"sha256": "
|
|
97
|
+
"size": 11105,
|
|
98
|
+
"sha256": "d4f07c03cb9e34293058255e8ad7d7d83e2b332256f6ca3a3d2011dcb857fb37"
|
|
95
99
|
},
|
|
96
100
|
"migrations/20260331000001_create_zin_trace_entries_table.d.ts": {
|
|
97
|
-
"size":
|
|
98
|
-
"sha256": "
|
|
101
|
+
"size": 313,
|
|
102
|
+
"sha256": "be62bd700b040e7df58dc76f8282fa1400b57d19f089a63b3f4677df16e203d4"
|
|
99
103
|
},
|
|
100
104
|
"migrations/20260331000001_create_zin_trace_entries_table.js": {
|
|
101
|
-
"size":
|
|
102
|
-
"sha256": "
|
|
105
|
+
"size": 964,
|
|
106
|
+
"sha256": "49c5df2a7caac1da82652db591914bfa00fde7591814a9f945f861f0a705fba7"
|
|
103
107
|
},
|
|
104
108
|
"migrations/20260331000002_create_zin_trace_entries_tags_table.d.ts": {
|
|
105
|
-
"size":
|
|
106
|
-
"sha256": "
|
|
109
|
+
"size": 313,
|
|
110
|
+
"sha256": "95d6824268fc69ec4a8cab4a551ca600d27983d0e0b13c51ffc5c9b4e55bec39"
|
|
107
111
|
},
|
|
108
112
|
"migrations/20260331000002_create_zin_trace_entries_tags_table.js": {
|
|
109
|
-
"size":
|
|
110
|
-
"sha256": "
|
|
113
|
+
"size": 746,
|
|
114
|
+
"sha256": "726375b0f404d2c6d55d195e52bf978e6f95b6ad4d241fa23ed7d8d1bb369412"
|
|
111
115
|
},
|
|
112
116
|
"migrations/20260331000003_create_zin_trace_monitoring_table.d.ts": {
|
|
113
|
-
"size":
|
|
114
|
-
"sha256": "
|
|
117
|
+
"size": 317,
|
|
118
|
+
"sha256": "d45a119cd221faaaf804fd466d2e5678e137751fc9ab3bc6bc6992b1190fb7fb"
|
|
115
119
|
},
|
|
116
120
|
"migrations/20260331000003_create_zin_trace_monitoring_table.js": {
|
|
117
|
-
"size":
|
|
118
|
-
"sha256": "
|
|
121
|
+
"size": 531,
|
|
122
|
+
"sha256": "fe52d93d169ae65e6092f05535b35c728d11253580b39bdd629a64fdd94c64ab"
|
|
119
123
|
},
|
|
120
124
|
"migrations/20260407193000_widen_trace_created_at_for_sql.d.ts": {
|
|
121
|
-
"size":
|
|
122
|
-
"sha256": "
|
|
125
|
+
"size": 344,
|
|
126
|
+
"sha256": "9243e0feab40bda7bfc1e5417d93c64b28166df5326adad30bdbcf9a03a7c7fb"
|
|
123
127
|
},
|
|
124
128
|
"migrations/20260407193000_widen_trace_created_at_for_sql.js": {
|
|
125
|
-
"size":
|
|
126
|
-
"sha256": "
|
|
129
|
+
"size": 1194,
|
|
130
|
+
"sha256": "ebb0d825d66b774f010f0fe689200ef7e3f1bcd74612d7090ff372daa9754a61"
|
|
127
131
|
},
|
|
128
132
|
"migrations/index.d.ts": {
|
|
129
133
|
"size": 280,
|
|
@@ -146,16 +150,24 @@
|
|
|
146
150
|
"sha256": "535869aa1bfcdf0ec26fad27d7f18bb8822bc586c38fcc5ffdde5274ecbea17e"
|
|
147
151
|
},
|
|
148
152
|
"register.js": {
|
|
149
|
-
"size":
|
|
150
|
-
"sha256": "
|
|
153
|
+
"size": 19896,
|
|
154
|
+
"sha256": "4516e527351016a9c2a0ea751a130cc3c42e19cd6cd817bf422663d2a613bf51"
|
|
155
|
+
},
|
|
156
|
+
"runtime/BackgroundTaskScheduler.d.ts": {
|
|
157
|
+
"size": 1182,
|
|
158
|
+
"sha256": "4ebb3549575f57210ffffce663a2f62e2d8a8ff0c56ad265259e559736ebccb2"
|
|
159
|
+
},
|
|
160
|
+
"runtime/BackgroundTaskScheduler.js": {
|
|
161
|
+
"size": 3254,
|
|
162
|
+
"sha256": "aa304666a000e6ccb0ae0a5e2bed701d610cf371955d4137a085c970fa11e230"
|
|
151
163
|
},
|
|
152
164
|
"storage/ProxyTraceStorage.d.ts": {
|
|
153
165
|
"size": 339,
|
|
154
166
|
"sha256": "9c724ff342dfe82da12e7cce95593f6623faa80c40f97593719b8e78e95f266d"
|
|
155
167
|
},
|
|
156
168
|
"storage/ProxyTraceStorage.js": {
|
|
157
|
-
"size":
|
|
158
|
-
"sha256": "
|
|
169
|
+
"size": 4379,
|
|
170
|
+
"sha256": "697db8971137d42804d9abd375839f45b443589ef72d368a1b9f715fd8c5b8af"
|
|
159
171
|
},
|
|
160
172
|
"storage/TraceContentBudget.d.ts": {
|
|
161
173
|
"size": 1306,
|
|
@@ -186,12 +198,12 @@
|
|
|
186
198
|
"sha256": "35d93c82d6db80071946adaa8e40d012408b469949f1002f85ae3fd20b0a70c8"
|
|
187
199
|
},
|
|
188
200
|
"storage/TraceServiceTag.js": {
|
|
189
|
-
"size":
|
|
190
|
-
"sha256": "
|
|
201
|
+
"size": 1923,
|
|
202
|
+
"sha256": "7e84325813e5399465e4794199895460aa7223c6a7b64339f0b241a7ca1295ee"
|
|
191
203
|
},
|
|
192
204
|
"storage/TraceStorage.d.ts": {
|
|
193
|
-
"size":
|
|
194
|
-
"sha256": "
|
|
205
|
+
"size": 526,
|
|
206
|
+
"sha256": "6de5990ca98178d909012e6678d95b30be1bfa29fdb0519974b37df60fbb3d20"
|
|
195
207
|
},
|
|
196
208
|
"storage/TraceStorage.js": {
|
|
197
209
|
"size": 15322,
|
|
@@ -214,8 +226,8 @@
|
|
|
214
226
|
"sha256": "ed5e83e108cd15f9a3976be71e966bdf0c44d8e0266d1d2dbad189f0885ad501"
|
|
215
227
|
},
|
|
216
228
|
"types.d.ts": {
|
|
217
|
-
"size":
|
|
218
|
-
"sha256": "
|
|
229
|
+
"size": 10214,
|
|
230
|
+
"sha256": "cc3510f7c6c859795d368a1fca2f3ec2322135387bb60bd189fd72ac833b9a7c"
|
|
219
231
|
},
|
|
220
232
|
"types.js": {
|
|
221
233
|
"size": 696,
|
|
@@ -330,8 +342,8 @@
|
|
|
330
342
|
"sha256": "0f58c50fd77704151399ca6cb6ec7890a9aef86afe28235951971f8cc9c1d600"
|
|
331
343
|
},
|
|
332
344
|
"watchers/ExceptionWatcher.js": {
|
|
333
|
-
"size":
|
|
334
|
-
"sha256": "
|
|
345
|
+
"size": 4253,
|
|
346
|
+
"sha256": "7d4c63675bb78857ba8a7be91e11a770201fd80d2fa7ee857d2e78dab3665f15"
|
|
335
347
|
},
|
|
336
348
|
"watchers/GateWatcher.d.ts": {
|
|
337
349
|
"size": 262,
|
|
@@ -346,16 +358,16 @@
|
|
|
346
358
|
"sha256": "dfb13bba526d5338e4dcd7a5aa0f72a61a03d9e2f6a250250c0bb8f0054c9712"
|
|
347
359
|
},
|
|
348
360
|
"watchers/HttpClientWatcher.js": {
|
|
349
|
-
"size":
|
|
350
|
-
"sha256": "
|
|
361
|
+
"size": 6335,
|
|
362
|
+
"sha256": "224b3cf85eba2e7f700a8cdcc61a0fe285ff0460a5c6550c83b0ea506e41c865"
|
|
351
363
|
},
|
|
352
364
|
"watchers/HttpWatcher.d.ts": {
|
|
353
365
|
"size": 96,
|
|
354
366
|
"sha256": "ce9a95a670f755193fd74ce721dbfa4b30f20c879a6566ebb35229b3b2435429"
|
|
355
367
|
},
|
|
356
368
|
"watchers/HttpWatcher.js": {
|
|
357
|
-
"size":
|
|
358
|
-
"sha256": "
|
|
369
|
+
"size": 7302,
|
|
370
|
+
"sha256": "3bfe58db8346474da1efb6a5a050786e2d70d43ab02d437c116510254387735e"
|
|
359
371
|
},
|
|
360
372
|
"watchers/JobWatcher.d.ts": {
|
|
361
373
|
"size": 441,
|
|
@@ -370,8 +382,8 @@
|
|
|
370
382
|
"sha256": "f3ddc5f8b58c6c86ac6b464dd48e5a55e79ab2bf2e735feacffc7480e4ccc0c4"
|
|
371
383
|
},
|
|
372
384
|
"watchers/LogWatcher.js": {
|
|
373
|
-
"size":
|
|
374
|
-
"sha256": "
|
|
385
|
+
"size": 3626,
|
|
386
|
+
"sha256": "6b9e86824141e170a731b44c2b3af6f3dff1e06a9d09b6a7e6c9c01453b8c6cf"
|
|
375
387
|
},
|
|
376
388
|
"watchers/MailWatcher.d.ts": {
|
|
377
389
|
"size": 244,
|
|
@@ -410,8 +422,8 @@
|
|
|
410
422
|
"sha256": "5d5046c65e5b683369c7709f1acd09b60aec3e7f44748fd1baeb35498836465b"
|
|
411
423
|
},
|
|
412
424
|
"watchers/QueryWatcher.js": {
|
|
413
|
-
"size":
|
|
414
|
-
"sha256": "
|
|
425
|
+
"size": 3293,
|
|
426
|
+
"sha256": "f2d02707308e091ef065f1d6e264b7f2d799ed4a60b90efe664f9d9f7ad6266a"
|
|
415
427
|
},
|
|
416
428
|
"watchers/RedisWatcher.d.ts": {
|
|
417
429
|
"size": 294,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* TraceDashboard handlers — pure handler functions wired to ITraceStorage.
|
|
3
3
|
* No auth in this layer — caller mounts middleware as needed.
|
|
4
4
|
*/
|
|
5
|
-
import type { IRequest, IResponse } from '@zintrust/core';
|
|
5
|
+
import type { IRequest, IResponse } from '@zintrust/core/http';
|
|
6
6
|
import type { ITraceStorage } from '../types';
|
|
7
7
|
export declare const setHandlerStorage: (s: ITraceStorage) => void;
|
|
8
8
|
export declare function listEntries(req: IRequest, res: IResponse): Promise<void>;
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Dashboard route registrar for @zintrust/trace.
|
|
3
|
-
* Mounts the SPA + all REST API endpoints under the configured basePath.
|
|
4
|
-
* Auth is NOT applied here — callers add middleware via routeOptions.
|
|
5
|
-
*/
|
|
6
|
-
import { type IRouter } from '@zintrust/core';
|
|
1
|
+
import { type IRouter } from '@zintrust/core/http';
|
|
7
2
|
import type { ITraceStorage } from '../types';
|
|
8
3
|
export type TraceDashboardOptions = {
|
|
9
4
|
/** Base path for the dashboard, e.g. '/trace'. Defaults to '/trace'. */
|
package/dist/dashboard/routes.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
* Mounts the SPA + all REST API endpoints under the configured basePath.
|
|
4
4
|
* Auth is NOT applied here — callers add middleware via routeOptions.
|
|
5
5
|
*/
|
|
6
|
-
import { appConfig
|
|
6
|
+
import { appConfig } from '@zintrust/core/config';
|
|
7
|
+
import { useDatabase } from '@zintrust/core/database';
|
|
8
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
9
|
+
import { Router } from '@zintrust/core/http';
|
|
7
10
|
import { TraceConfig } from '../config.js';
|
|
8
11
|
import { TraceStorage } from '../storage/index.js';
|
|
9
12
|
import { assertTraceConnectionResolved, resolveDashboardTraceConnectionName, } from '../TraceConnection.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export type { ITraceStorage } from './storage';
|
|
|
11
11
|
export { TraceContentBudget } from './storage/TraceContentBudget';
|
|
12
12
|
export { TraceContentRedaction } from './storage/TraceContentRedaction';
|
|
13
13
|
export { TraceContext } from './context';
|
|
14
|
+
export { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler';
|
|
14
15
|
export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes';
|
|
15
16
|
export type { TraceDashboardOptions, TraceDashboardRegistrationOptions } from './dashboard/routes';
|
|
16
17
|
export { registerTraceIngestGateway, TraceIngestGateway } from './ingest/TraceIngestGateway';
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,10 @@ export { TraceContentRedaction } from './storage/TraceContentRedaction.js';
|
|
|
21
21
|
// ---------------------------------------------------------------------------
|
|
22
22
|
export { TraceContext } from './context.js';
|
|
23
23
|
// ---------------------------------------------------------------------------
|
|
24
|
+
// Runtime
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
export { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler.js';
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
24
28
|
// Dashboard
|
|
25
29
|
// ---------------------------------------------------------------------------
|
|
26
30
|
export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes.js';
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { Env
|
|
1
|
+
import { Env } from '@zintrust/core/config';
|
|
2
|
+
import { useDatabase } from '@zintrust/core/database';
|
|
3
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
4
|
+
import { Router, } from '@zintrust/core/http';
|
|
5
|
+
import { SignedRequest } from '@zintrust/core/security';
|
|
2
6
|
import { TraceConfig } from '../config.js';
|
|
3
7
|
import { TraceStorage } from '../storage/index.js';
|
|
4
8
|
const nonces = new Map();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceEntriesTable
|
|
3
3
|
* Creates the main entries table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { type IDatabase } from '@zintrust/core';
|
|
5
|
+
import { type IDatabase } from '@zintrust/core/database';
|
|
6
6
|
export interface Migration {
|
|
7
7
|
up(db: IDatabase): Promise<void>;
|
|
8
8
|
down(db: IDatabase): Promise<void>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceEntriesTable
|
|
3
3
|
* Creates the main entries table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { MigrationSchema } from '@zintrust/core';
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core/database';
|
|
6
6
|
export const migration = {
|
|
7
7
|
async up(db) {
|
|
8
8
|
const schema = MigrationSchema.create(db);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceEntriesTagsTable
|
|
3
3
|
* Creates the tag join table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { type IDatabase } from '@zintrust/core';
|
|
5
|
+
import { type IDatabase } from '@zintrust/core/database';
|
|
6
6
|
export interface Migration {
|
|
7
7
|
up(db: IDatabase): Promise<void>;
|
|
8
8
|
down(db: IDatabase): Promise<void>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceEntriesTagsTable
|
|
3
3
|
* Creates the tag join table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { MigrationSchema } from '@zintrust/core';
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core/database';
|
|
6
6
|
export const migration = {
|
|
7
7
|
async up(db) {
|
|
8
8
|
const schema = MigrationSchema.create(db);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceMonitoringTable
|
|
3
3
|
* Creates the tag watchlist table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { type IDatabase } from '@zintrust/core';
|
|
5
|
+
import { type IDatabase } from '@zintrust/core/database';
|
|
6
6
|
export interface Migration {
|
|
7
7
|
up(db: IDatabase): Promise<void>;
|
|
8
8
|
down(db: IDatabase): Promise<void>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: CreateZinTraceMonitoringTable
|
|
3
3
|
* Creates the tag watchlist table for @zintrust/trace
|
|
4
4
|
*/
|
|
5
|
-
import { MigrationSchema } from '@zintrust/core';
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core/database';
|
|
6
6
|
export const migration = {
|
|
7
7
|
async up(db) {
|
|
8
8
|
const schema = MigrationSchema.create(db);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: WidenTraceCreatedAtForSql
|
|
3
3
|
* Ensures SQL engines that treat INTEGER as 32-bit can store millisecond timestamps.
|
|
4
4
|
*/
|
|
5
|
-
import { type IDatabase } from '@zintrust/core';
|
|
5
|
+
import { type IDatabase } from '@zintrust/core/database';
|
|
6
6
|
export interface Migration {
|
|
7
7
|
up(db: IDatabase): Promise<void>;
|
|
8
8
|
down(db: IDatabase): Promise<void>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Migration: WidenTraceCreatedAtForSql
|
|
3
3
|
* Ensures SQL engines that treat INTEGER as 32-bit can store millisecond timestamps.
|
|
4
4
|
*/
|
|
5
|
-
import { MigrationSchema } from '@zintrust/core';
|
|
5
|
+
import { MigrationSchema } from '@zintrust/core/database';
|
|
6
6
|
const alterCreatedAt = async (db) => {
|
|
7
7
|
const driver = db.getType?.() ?? 'sqlite';
|
|
8
8
|
if (driver === 'sqlite' || driver === 'd1' || driver === 'd1-remote')
|
package/dist/register.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
*/
|
|
22
22
|
import { TraceConfig } from './config.js';
|
|
23
23
|
import { TraceContext } from './context.js';
|
|
24
|
+
import { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler.js';
|
|
24
25
|
import { ProxyTraceStorage, TraceServiceTag, TraceStorage } from './storage/index.js';
|
|
25
26
|
import { TraceContentBudget } from './storage/TraceContentBudget.js';
|
|
26
27
|
import { TraceContentRedaction } from './storage/TraceContentRedaction.js';
|
|
@@ -319,7 +320,14 @@ const createTraceWatcherArgs = async (core, Env, config) => {
|
|
|
319
320
|
connectionName: resolvedConnectionName,
|
|
320
321
|
logger: core.Logger,
|
|
321
322
|
});
|
|
322
|
-
return {
|
|
323
|
+
return {
|
|
324
|
+
storage,
|
|
325
|
+
config,
|
|
326
|
+
db: observedDb,
|
|
327
|
+
scheduleBackgroundTask: (task) => {
|
|
328
|
+
BackgroundTaskScheduler.schedule(task);
|
|
329
|
+
},
|
|
330
|
+
};
|
|
323
331
|
};
|
|
324
332
|
const registerTraceWatchers = async (watcherArgs) => {
|
|
325
333
|
const [{ HttpWatcher }, { QueryWatcher }, { LogWatcher }, { ExceptionWatcher }, { JobWatcher }, { CacheWatcher }, { ScheduleWatcher }, { MailWatcher }, { AuthWatcher }, { EventWatcher }, { ModelWatcher }, { NotificationWatcher }, { RedisWatcher }, { GateWatcher }, { MiddlewareWatcher }, { CommandWatcher }, { BatchWatcher }, { DumpWatcher }, { ViewWatcher }, { HttpClientWatcher },] = await Promise.all([
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BackgroundTaskScheduler — runtime-agnostic background task scheduling.
|
|
3
|
+
* Maps to ctx.waitUntil() in Cloudflare Workers, normal promises in Node.
|
|
4
|
+
*/
|
|
5
|
+
type ExecutionContext = {
|
|
6
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
7
|
+
};
|
|
8
|
+
type BackgroundTaskScheduler = {
|
|
9
|
+
schedule(task: Promise<void>): void;
|
|
10
|
+
isAvailable(): boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare const BackgroundTaskScheduler: Readonly<{
|
|
13
|
+
/**
|
|
14
|
+
* Set the Worker execution context for waitUntil support.
|
|
15
|
+
* Call this from your Cloudflare Worker fetch handler:
|
|
16
|
+
* BackgroundTaskScheduler.setExecutionContext(ctx);
|
|
17
|
+
*/
|
|
18
|
+
setExecutionContext(ctx: ExecutionContext): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get the current scheduler instance.
|
|
21
|
+
*/
|
|
22
|
+
getScheduler(): BackgroundTaskScheduler;
|
|
23
|
+
/**
|
|
24
|
+
* Schedule a background task.
|
|
25
|
+
* In Workers: uses ctx.waitUntil() if context is set
|
|
26
|
+
* In Node: fire-and-forget with error suppression
|
|
27
|
+
*/
|
|
28
|
+
schedule(task: Promise<void>): void;
|
|
29
|
+
/**
|
|
30
|
+
* Check if a proper scheduler is available (i.e., Workers context is set).
|
|
31
|
+
*/
|
|
32
|
+
isAvailable(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Reset the scheduler (useful for testing).
|
|
35
|
+
*/
|
|
36
|
+
reset(): void;
|
|
37
|
+
}>;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BackgroundTaskScheduler — runtime-agnostic background task scheduling.
|
|
3
|
+
* Maps to ctx.waitUntil() in Cloudflare Workers, normal promises in Node.
|
|
4
|
+
*/
|
|
5
|
+
let _executionContext;
|
|
6
|
+
let _scheduler;
|
|
7
|
+
const isCloudflareRuntime = () => {
|
|
8
|
+
const globalRef = typeof globalThis === 'undefined' ? undefined : globalThis;
|
|
9
|
+
if (!globalRef)
|
|
10
|
+
return false;
|
|
11
|
+
// @ts-ignore - navigator is available in workers
|
|
12
|
+
if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
return (typeof globalRef.caches !== 'undefined' ||
|
|
16
|
+
typeof globalRef.WebSocketPair === 'function' ||
|
|
17
|
+
typeof globalRef.CF !== 'undefined');
|
|
18
|
+
};
|
|
19
|
+
const createWorkerScheduler = (ctx) => {
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
schedule(task) {
|
|
22
|
+
ctx.waitUntil(task);
|
|
23
|
+
},
|
|
24
|
+
isAvailable() {
|
|
25
|
+
return true;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const createNodeScheduler = () => {
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
schedule(task) {
|
|
32
|
+
// In Node, fire-and-forget is acceptable since the process doesn't terminate
|
|
33
|
+
// like Workers do. We still catch errors to prevent unhandled rejections.
|
|
34
|
+
task.catch(() => {
|
|
35
|
+
// Silently ignore background task errors to avoid noise
|
|
36
|
+
// The individual watchers handle their own error logging
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
isAvailable() {
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
const createFallbackScheduler = () => {
|
|
45
|
+
return Object.freeze({
|
|
46
|
+
schedule(task) {
|
|
47
|
+
// Fallback: fire-and-forget with error suppression
|
|
48
|
+
task.catch(() => undefined);
|
|
49
|
+
},
|
|
50
|
+
isAvailable() {
|
|
51
|
+
return false;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
export const BackgroundTaskScheduler = Object.freeze({
|
|
56
|
+
/**
|
|
57
|
+
* Set the Worker execution context for waitUntil support.
|
|
58
|
+
* Call this from your Cloudflare Worker fetch handler:
|
|
59
|
+
* BackgroundTaskScheduler.setExecutionContext(ctx);
|
|
60
|
+
*/
|
|
61
|
+
setExecutionContext(ctx) {
|
|
62
|
+
_executionContext = ctx;
|
|
63
|
+
_scheduler = createWorkerScheduler(ctx);
|
|
64
|
+
},
|
|
65
|
+
/**
|
|
66
|
+
* Get the current scheduler instance.
|
|
67
|
+
*/
|
|
68
|
+
getScheduler() {
|
|
69
|
+
if (_scheduler)
|
|
70
|
+
return _scheduler;
|
|
71
|
+
if (_executionContext) {
|
|
72
|
+
_scheduler = createWorkerScheduler(_executionContext);
|
|
73
|
+
return _scheduler;
|
|
74
|
+
}
|
|
75
|
+
if (isCloudflareRuntime()) {
|
|
76
|
+
// We're in Workers but no context was set - use fallback
|
|
77
|
+
_scheduler = createFallbackScheduler();
|
|
78
|
+
return _scheduler;
|
|
79
|
+
}
|
|
80
|
+
// Node.js or other runtime
|
|
81
|
+
_scheduler = createNodeScheduler();
|
|
82
|
+
return _scheduler;
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* Schedule a background task.
|
|
86
|
+
* In Workers: uses ctx.waitUntil() if context is set
|
|
87
|
+
* In Node: fire-and-forget with error suppression
|
|
88
|
+
*/
|
|
89
|
+
schedule(task) {
|
|
90
|
+
this.getScheduler().schedule(task);
|
|
91
|
+
},
|
|
92
|
+
/**
|
|
93
|
+
* Check if a proper scheduler is available (i.e., Workers context is set).
|
|
94
|
+
*/
|
|
95
|
+
isAvailable() {
|
|
96
|
+
return this.getScheduler().isAvailable();
|
|
97
|
+
},
|
|
98
|
+
/**
|
|
99
|
+
* Reset the scheduler (useful for testing).
|
|
100
|
+
*/
|
|
101
|
+
reset() {
|
|
102
|
+
_executionContext = undefined;
|
|
103
|
+
_scheduler = undefined;
|
|
104
|
+
},
|
|
105
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ErrorFactory
|
|
1
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
2
|
+
import { RemoteSignedJson } from '@zintrust/core/security';
|
|
2
3
|
const ensureConfigured = (settings) => {
|
|
3
4
|
if (settings.baseUrl.trim() === '') {
|
|
4
5
|
throw ErrorFactory.createConfigError('TRACE_PROXY_URL is required when TRACE_PROXY=true');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorFactory } from '@zintrust/core';
|
|
1
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
2
2
|
const appendServiceTag = (entry, serviceTag) => {
|
|
3
3
|
const normalizedTag = serviceTag?.trim() ?? '';
|
|
4
4
|
if (normalizedTag === '' || entry.tags.includes(normalizedTag)) {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Resolves the correct IDatabase from the app config, then delegates all
|
|
4
4
|
* read/write operations to the trace storage facade.
|
|
5
5
|
*/
|
|
6
|
-
import type { IDatabase } from '@zintrust/core';
|
|
6
|
+
import type { IDatabase } from '@zintrust/core/database';
|
|
7
7
|
import type { ITraceStorage } from '../types';
|
|
8
8
|
export declare const TraceStorage: Readonly<{
|
|
9
9
|
resolveStorage: (db: IDatabase) => ITraceStorage;
|
package/dist/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Types for @zintrust/trace
|
|
3
3
|
* Sealed type definitions — no side effects.
|
|
4
4
|
*/
|
|
5
|
-
import type { IDatabase } from '@zintrust/core';
|
|
5
|
+
import type { IDatabase } from '@zintrust/core/database';
|
|
6
6
|
export declare const EntryType: Readonly<{
|
|
7
7
|
readonly REQUEST: "request";
|
|
8
8
|
readonly QUERY: "query";
|
|
@@ -272,6 +272,8 @@ export interface ITraceWatcherConfig {
|
|
|
272
272
|
db?: IDatabase;
|
|
273
273
|
/** Optional: provide to allow HttpWatcher to register as global middleware. */
|
|
274
274
|
registerMiddleware?: (fn: (req: unknown, res: unknown, next: () => Promise<void>) => Promise<void>) => void;
|
|
275
|
+
/** Optional: schedule a background task (maps to ctx.waitUntil in Workers, normal promise in Node). */
|
|
276
|
+
scheduleBackgroundTask?: (task: Promise<void>) => void;
|
|
275
277
|
}
|
|
276
278
|
export interface ITraceWatcher {
|
|
277
279
|
register(opts: ITraceWatcherConfig): () => void;
|
|
@@ -35,6 +35,7 @@ const buildContent = (err, context) => {
|
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
37
|
let _storage = null;
|
|
38
|
+
let _scheduleBackgroundTask = null;
|
|
38
39
|
let _listenerRefCount = 0;
|
|
39
40
|
let _ignoreRoutes = [];
|
|
40
41
|
let _ignorePaths = [];
|
|
@@ -72,7 +73,7 @@ const captureException = (err, context) => {
|
|
|
72
73
|
const content = buildContent(err, context);
|
|
73
74
|
const hash = familyHash(`${content.class}:${content.file}:${content.line}`);
|
|
74
75
|
const uuid = crypto.randomUUID();
|
|
75
|
-
storage
|
|
76
|
+
const writePromise = storage
|
|
76
77
|
.writeEntry({
|
|
77
78
|
uuid,
|
|
78
79
|
batchId: context?.batchId ?? TraceContext.getBatchId(),
|
|
@@ -85,13 +86,19 @@ const captureException = (err, context) => {
|
|
|
85
86
|
})
|
|
86
87
|
.then(() => storage.markFamilyStale(hash, uuid))
|
|
87
88
|
.catch(() => undefined);
|
|
89
|
+
// Use background task scheduler if available (Workers waitUntil support)
|
|
90
|
+
if (_scheduleBackgroundTask) {
|
|
91
|
+
_scheduleBackgroundTask(writePromise);
|
|
92
|
+
}
|
|
93
|
+
// Otherwise, the promise is already fire-and-forget with error suppression
|
|
88
94
|
};
|
|
89
95
|
export const ExceptionWatcher = Object.freeze({
|
|
90
96
|
capture: captureException,
|
|
91
|
-
register({ storage, config }) {
|
|
97
|
+
register({ storage, config, scheduleBackgroundTask }) {
|
|
92
98
|
if (config.watchers.exception === false)
|
|
93
99
|
return () => undefined;
|
|
94
100
|
_storage = storage;
|
|
101
|
+
_scheduleBackgroundTask = scheduleBackgroundTask ?? null;
|
|
95
102
|
_ignoreRoutes = config.ignoreRoutes;
|
|
96
103
|
_ignorePaths = config.ignorePaths;
|
|
97
104
|
if (_listenerRefCount === 0) {
|
|
@@ -104,6 +111,7 @@ export const ExceptionWatcher = Object.freeze({
|
|
|
104
111
|
unregisterProcessListeners();
|
|
105
112
|
}
|
|
106
113
|
_storage = null;
|
|
114
|
+
_scheduleBackgroundTask = null;
|
|
107
115
|
_ignoreRoutes = [];
|
|
108
116
|
_ignorePaths = [];
|
|
109
117
|
};
|
|
@@ -4,6 +4,7 @@ import { AuthTag } from '../utils/authTag.js';
|
|
|
4
4
|
import { redactHeaders, redactUnknown } from '../utils/redact.js';
|
|
5
5
|
import { RequestFilter } from '../utils/requestFilter.js';
|
|
6
6
|
let _storage = null;
|
|
7
|
+
let _scheduleBackgroundTask = null;
|
|
7
8
|
let _redactHeaderNames = [];
|
|
8
9
|
let _redactBodyFields = [];
|
|
9
10
|
let _ignoreRoutes = [];
|
|
@@ -126,7 +127,7 @@ const emit = ({ source, method, url, requestHeaders, responseStatus, duration, r
|
|
|
126
127
|
responseBody,
|
|
127
128
|
error,
|
|
128
129
|
}, sourceRule, normalizedSource);
|
|
129
|
-
_storage
|
|
130
|
+
const writePromise = _storage
|
|
130
131
|
.writeEntry({
|
|
131
132
|
uuid: crypto.randomUUID(),
|
|
132
133
|
batchId: TraceContext.getBatchId(),
|
|
@@ -137,13 +138,19 @@ const emit = ({ source, method, url, requestHeaders, responseStatus, duration, r
|
|
|
137
138
|
createdAt: TraceContext.now(),
|
|
138
139
|
})
|
|
139
140
|
.catch(() => undefined);
|
|
141
|
+
// Use background task scheduler if available (Workers waitUntil support)
|
|
142
|
+
if (_scheduleBackgroundTask) {
|
|
143
|
+
_scheduleBackgroundTask(writePromise);
|
|
144
|
+
}
|
|
145
|
+
// Otherwise, the promise is already fire-and-forget with error suppression
|
|
140
146
|
};
|
|
141
147
|
export const HttpClientWatcher = Object.freeze({
|
|
142
148
|
emit,
|
|
143
|
-
register({ storage, config }) {
|
|
149
|
+
register({ storage, config, scheduleBackgroundTask }) {
|
|
144
150
|
if (!isWatcherEnabled(config.watchers.clientRequest))
|
|
145
151
|
return () => undefined;
|
|
146
152
|
_storage = storage;
|
|
153
|
+
_scheduleBackgroundTask = scheduleBackgroundTask ?? null;
|
|
147
154
|
_clientRequestWatcher =
|
|
148
155
|
typeof config.watchers.clientRequest === 'object' && config.watchers.clientRequest !== null
|
|
149
156
|
? config.watchers.clientRequest
|
|
@@ -154,6 +161,7 @@ export const HttpClientWatcher = Object.freeze({
|
|
|
154
161
|
_ignorePaths = config.ignorePaths;
|
|
155
162
|
return () => {
|
|
156
163
|
_storage = null;
|
|
164
|
+
_scheduleBackgroundTask = null;
|
|
157
165
|
_clientRequestWatcher = undefined;
|
|
158
166
|
_redactBodyFields = [];
|
|
159
167
|
_ignoreRoutes = [];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from '@zintrust/core';
|
|
1
|
+
import { Logger } from '@zintrust/core/logger';
|
|
2
2
|
import { TraceContext } from '../context.js';
|
|
3
3
|
import { EntryType } from '../types.js';
|
|
4
4
|
import { AuthTag } from '../utils/authTag.js';
|
|
@@ -130,7 +130,7 @@ const shouldIgnore = (req, config) => {
|
|
|
130
130
|
};
|
|
131
131
|
const isWatcherEnabled = (config) => config.watchers.request !== false;
|
|
132
132
|
export const HttpWatcher = Object.freeze({
|
|
133
|
-
register({ storage, config, registerMiddleware }) {
|
|
133
|
+
register({ storage, config, registerMiddleware, scheduleBackgroundTask, }) {
|
|
134
134
|
if (!isWatcherEnabled(config))
|
|
135
135
|
return () => undefined;
|
|
136
136
|
if (!registerMiddleware)
|
|
@@ -162,14 +162,22 @@ export const HttpWatcher = Object.freeze({
|
|
|
162
162
|
isLatest: true,
|
|
163
163
|
createdAt: TraceContext.now(),
|
|
164
164
|
};
|
|
165
|
-
storage.writeEntry(entry).catch((error) => {
|
|
165
|
+
const writePromise = storage.writeEntry(entry).catch((error) => {
|
|
166
166
|
Logger.warn('[trace] HttpWatcher writeEntry failed', {
|
|
167
167
|
method: content.method,
|
|
168
168
|
uri: content.uri,
|
|
169
169
|
entryUuid: entry.uuid,
|
|
170
170
|
error: error instanceof Error ? error.message : String(error),
|
|
171
171
|
});
|
|
172
|
-
});
|
|
172
|
+
});
|
|
173
|
+
// Use background task scheduler if available (Workers waitUntil support)
|
|
174
|
+
if (scheduleBackgroundTask) {
|
|
175
|
+
scheduleBackgroundTask(writePromise);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// Fallback to fire-and-forget for backward compatibility
|
|
179
|
+
writePromise.catch(() => undefined);
|
|
180
|
+
}
|
|
173
181
|
};
|
|
174
182
|
const completionHandler = registerCompletionHandler(response, persistEntry);
|
|
175
183
|
await next();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LogWatcher — captures Logger output via Logger.addSink().
|
|
3
3
|
*/
|
|
4
|
-
import { Logger } from '@zintrust/core';
|
|
4
|
+
import { Logger } from '@zintrust/core/logger';
|
|
5
5
|
import { TraceContext } from '../context.js';
|
|
6
6
|
import { EntryType } from '../types.js';
|
|
7
7
|
import { AuthTag } from '../utils/authTag.js';
|
|
@@ -54,7 +54,7 @@ const shouldSkipTraceInfrastructureLog = (message, context) => {
|
|
|
54
54
|
isTraceStorageQueryLog(message, context));
|
|
55
55
|
};
|
|
56
56
|
export const LogWatcher = Object.freeze({
|
|
57
|
-
register({ storage, config }) {
|
|
57
|
+
register({ storage, config, scheduleBackgroundTask }) {
|
|
58
58
|
if (config.watchers.log === false)
|
|
59
59
|
return () => undefined;
|
|
60
60
|
const minPriority = LEVEL_PRIORITY[config.logMinLevel] ?? 1;
|
|
@@ -75,7 +75,7 @@ export const LogWatcher = Object.freeze({
|
|
|
75
75
|
context: context ?? undefined,
|
|
76
76
|
hostname: TraceContext.getHostname(),
|
|
77
77
|
};
|
|
78
|
-
storage
|
|
78
|
+
const writePromise = storage
|
|
79
79
|
.writeEntry({
|
|
80
80
|
uuid: crypto.randomUUID(),
|
|
81
81
|
batchId: TraceContext.getBatchId(),
|
|
@@ -86,6 +86,11 @@ export const LogWatcher = Object.freeze({
|
|
|
86
86
|
createdAt: TraceContext.now(),
|
|
87
87
|
})
|
|
88
88
|
.catch(() => undefined);
|
|
89
|
+
// Use background task scheduler if available (Workers waitUntil support)
|
|
90
|
+
if (scheduleBackgroundTask) {
|
|
91
|
+
scheduleBackgroundTask(writePromise);
|
|
92
|
+
}
|
|
93
|
+
// Otherwise, the promise is already fire-and-forget with error suppression
|
|
89
94
|
});
|
|
90
95
|
return unsubscribe;
|
|
91
96
|
},
|
|
@@ -7,6 +7,7 @@ import { EntryType } from '../types.js';
|
|
|
7
7
|
import { AuthTag } from '../utils/authTag.js';
|
|
8
8
|
let _storage = null;
|
|
9
9
|
let _config = null;
|
|
10
|
+
let _scheduleBackgroundTask = null;
|
|
10
11
|
const bindingsInterpolated = (sql, params) => {
|
|
11
12
|
// Inline params for display only — safe, not for re-execution.
|
|
12
13
|
let i = 0;
|
|
@@ -51,7 +52,7 @@ const emit = (query, params, duration, connection = 'default') => {
|
|
|
51
52
|
const tags = AuthTag.append([]);
|
|
52
53
|
if (slow)
|
|
53
54
|
tags.push('slow');
|
|
54
|
-
_storage
|
|
55
|
+
const writePromise = _storage
|
|
55
56
|
.writeEntry({
|
|
56
57
|
uuid: crypto.randomUUID(),
|
|
57
58
|
batchId,
|
|
@@ -63,16 +64,22 @@ const emit = (query, params, duration, connection = 'default') => {
|
|
|
63
64
|
createdAt: TraceContext.now(),
|
|
64
65
|
})
|
|
65
66
|
.catch(() => undefined);
|
|
67
|
+
// Use background task scheduler if available (Workers waitUntil support)
|
|
68
|
+
if (_scheduleBackgroundTask) {
|
|
69
|
+
_scheduleBackgroundTask(writePromise);
|
|
70
|
+
}
|
|
71
|
+
// Otherwise, the promise is already fire-and-forget with error suppression
|
|
66
72
|
};
|
|
67
73
|
export const QueryWatcher = Object.freeze({
|
|
68
74
|
emit,
|
|
69
|
-
register({ storage, config, db: injectedDb }) {
|
|
75
|
+
register({ storage, config, db: injectedDb, scheduleBackgroundTask, }) {
|
|
70
76
|
if (config.watchers.query === false)
|
|
71
77
|
return () => undefined;
|
|
72
78
|
if (!injectedDb)
|
|
73
79
|
return () => undefined; // no db available
|
|
74
80
|
_storage = storage;
|
|
75
81
|
_config = config;
|
|
82
|
+
_scheduleBackgroundTask = scheduleBackgroundTask ?? null;
|
|
76
83
|
const db = injectedDb;
|
|
77
84
|
const handler = (query, params, duration) => {
|
|
78
85
|
emit(query, params, duration);
|
|
@@ -81,6 +88,7 @@ export const QueryWatcher = Object.freeze({
|
|
|
81
88
|
return () => {
|
|
82
89
|
_storage = null;
|
|
83
90
|
_config = null;
|
|
91
|
+
_scheduleBackgroundTask = null;
|
|
84
92
|
db.offAfterQuery?.(handler);
|
|
85
93
|
};
|
|
86
94
|
},
|