@kaanbiryol/crate-server 0.0.0-stage → 0.17.9
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/LICENSE +21 -0
- package/README.md +73 -2
- package/THIRD_PARTY_NOTICES.md +3664 -0
- package/assets/coordinator.mjs +116 -0
- package/assets/credential-revocation.mjs +18 -0
- package/assets/database-upgrade-rules.mjs +42 -0
- package/assets/migrations/README.md +19 -0
- package/assets/qrcode.mjs +1638 -0
- package/assets/schema.sql +368 -0
- package/assets/server-release.json +6 -0
- package/assets/worker-runtime.json +12 -0
- package/assets/worker.mjs +1458 -0
- package/npm-shrinkwrap.json +939 -0
- package/package.json +38 -4
- package/scripts/assets.mjs +33 -0
- package/scripts/commands/data.mjs +80 -0
- package/scripts/commands/devices.mjs +100 -0
- package/scripts/commands/doctor.mjs +127 -0
- package/scripts/commands/service.mjs +19 -0
- package/scripts/commands/setup.mjs +194 -0
- package/scripts/commands/start.mjs +171 -0
- package/scripts/commands/status.mjs +49 -0
- package/scripts/commands/update.mjs +138 -0
- package/scripts/crate-server.mjs +11 -0
- package/scripts/deployment-fingerprint.mjs +13 -0
- package/scripts/local-server-alerts.mjs +53 -0
- package/scripts/local-server-backup-schedule.mjs +153 -0
- package/scripts/local-server-backup.mjs +275 -0
- package/scripts/local-server-cloudflared.mjs +93 -0
- package/scripts/local-server-control.mjs +150 -0
- package/scripts/local-server-devices.mjs +71 -0
- package/scripts/local-server-host-status.mjs +72 -0
- package/scripts/local-server-http.mjs +120 -0
- package/scripts/local-server-install.mjs +22 -0
- package/scripts/local-server-lifecycle.mjs +54 -0
- package/scripts/local-server-lock.mjs +217 -0
- package/scripts/local-server-migrations.mjs +43 -0
- package/scripts/local-server-options.mjs +134 -0
- package/scripts/local-server-pairing.mjs +47 -0
- package/scripts/local-server-process.mjs +77 -0
- package/scripts/local-server-qr.mjs +34 -0
- package/scripts/local-server-quick.mjs +116 -0
- package/scripts/local-server-readiness.mjs +48 -0
- package/scripts/local-server-runtime.mjs +460 -0
- package/scripts/local-server-service.mjs +209 -0
- package/scripts/local-server-style.mjs +12 -0
- package/scripts/local-server-tailscale.mjs +122 -0
- package/scripts/local-server-tunnel.mjs +212 -0
- package/scripts/local-server-watchdog.mjs +37 -0
- package/scripts/local-server-wizard.mjs +100 -0
- package/scripts/local-server.mjs +100 -0
- package/vendor/miniflare/LICENSE +25 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/ArrowClockwise.es-Cs0EHtoR.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Breadcrumbs-BlzUUgNz.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CaretDown.es-r8EMGhHM.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CaretLeft.es-CCxvlwgr.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CaretRight.es-CWeeOO8C.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CaretUpDown.es-Bp73BWZN.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Check.es-B3dV_aVM.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Copy.es-DUyM7PbX.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CopyButton-DdMFsAgv.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CronTriggersContext-CtoQyHCH.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/CronTriggersPage-BC2FvZf5.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/DotsThree.es-B9u_qIrH.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Download.es-v8YTKdZh.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/List.es-GlowdvL8.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/ListBullets.es-DrJsrtCL.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/MagnifyingGlass.es-BzOo3pru.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/PaperPlaneTilt.es-DTLdWOnM.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Paperclip.es-6Ysj3lly.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Pencil.es-CDpuGAys.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Plus.es-xOjOjCs5.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/ResourceError-DNadec0F.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Spinner.es-oqUPe-aY.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/TableSelect-DgjN53ZF.js +25 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Trash.es-DTqwcb2_.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/Warning.es-CLa-n7LD.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/X.es-BiJFnG31.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_appId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_appId-TnuOHwOA.js +5 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_bucketName-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_bucketName-DMGTgdaZ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_bucketName-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_bucketName-DqCj-aYZ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_bucketName-uYCS7veQ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_captureId-Cb6ggQGG.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_captureId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_captureId-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-BW1__nAw.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-DCj9M_Uf.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-DMGTgdaZ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_className-uYCS7veQ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_databaseId-BEh2sOY5.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_databaseId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_instanceId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_instanceId-CxJm9jpK.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_instanceId-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_namespaceId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_namespaceId-HT9g0tMq.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_objectId-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_objectId-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_objectId-DdmD-NV1.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_workflowName-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_workflowName-DMGTgdaZ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_workflowName-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_workflowName-WgIBBs_w.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/_workflowName-uYCS7veQ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/ad-hoc-B9HL0oNk.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/async-t2_zA-v_.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/button-cdxnqcgzwko8ooha-CjoTKpgy.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/checkbox-kt1uojk2f9e0d0h1-D923QL-G.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/clipboard-text-vcbvmtne4zjk4b18-CEoJIuMe.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/configured-BGeRrlYf.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/cron-triggers-ZP5ClwAa.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/dialog-oqh8l3l3zutpibxx-nho54TMC.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/dropdown-ncwhcd912vmone8k-BAT0p_ep.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/email-B-IveBKB.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/events-ClnoiyGH.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/events-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/field-krp6z6vfbkrvufz2-DsLUBjzS.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/format-CjQ0gyEW.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/helpers-Bz2sloWm.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/index-BHW_FKpE.js +30 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/index-CiXL10aq.css +2 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/input-area-no30c09udyjxshu5-T786oEMj.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/input-group-lxdd09p60cf27pe1-BqVz8A4J.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/input-h48k3uagzrgb98au-BF-hE08e.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/label-latndvb1ngem7we8-mWoel0Kk.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/link-DadJAPnd.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/link-provider-mn2voeohon7cj9o4-CNosc_Ni.js +13 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/matchContext-C1eKDtty.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/object._-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/object._-DDwO6NT7.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/object._-Daanvg3W.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/observability-CC1mP6Xo.js +15 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/observability-CX-6-j6d.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/observability-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/react-BZY8YNV6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/redirect-CckYXsV1.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/routes-BlOyav-b.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/routing-CVthjHxr.js +2 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/routing-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/routing-DMGTgdaZ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/routing-uYCS7veQ.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/select-kva5ru5f673kah1m-DQuxH2_G.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/sending-BPjb3S3Y.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/sending-Cow8QNw6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/switch-ihaydbzem62bey4p-C9M-MPNS.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/table-inweecadl3her7pd-UWLv9e8D.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/text-nmyi1rkwdj37f30f-BLzIhmEO.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/types-DTx0hLYz.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/useCursorPaginatedList-BGUINn38.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/assets/useRouterState-BLq6Ai_6.js +1 -0
- package/vendor/miniflare/dist/local-explorer-ui/favicon.svg +3 -0
- package/vendor/miniflare/dist/local-explorer-ui/index.html +27 -0
- package/vendor/miniflare/dist/src/index.d.ts +14183 -0
- package/vendor/miniflare/dist/src/index.js +123738 -0
- package/vendor/miniflare/dist/src/mdream_edge_bg.wasm +0 -0
- package/vendor/miniflare/dist/src/plugins/artifacts/git-sidecar.js +21071 -0
- package/vendor/miniflare/dist/src/workers/access/access-identity.worker.js +10 -0
- package/vendor/miniflare/dist/src/workers/analytics-engine/analytics-engine.worker.js +21 -0
- package/vendor/miniflare/dist/src/workers/analytics-engine/remote.worker.js +2578 -0
- package/vendor/miniflare/dist/src/workers/artifacts/binding.worker.js +903 -0
- package/vendor/miniflare/dist/src/workers/artifacts/wrapped-binding.worker.js +52 -0
- package/vendor/miniflare/dist/src/workers/assets/assets-kv.worker.js +27 -0
- package/vendor/miniflare/dist/src/workers/assets/assets.worker.js +1525 -0
- package/vendor/miniflare/dist/src/workers/assets/router.worker.js +1883 -0
- package/vendor/miniflare/dist/src/workers/assets/rpc-proxy.worker.js +55 -0
- package/vendor/miniflare/dist/src/workers/browser-rendering/binding.worker.js +453 -0
- package/vendor/miniflare/dist/src/workers/cache/cache-entry-noop.worker.js +18 -0
- package/vendor/miniflare/dist/src/workers/cache/cache-entry.worker.js +24 -0
- package/vendor/miniflare/dist/src/workers/cache/cache.worker.js +652 -0
- package/vendor/miniflare/dist/src/workers/core/dev-control.worker.js +52 -0
- package/vendor/miniflare/dist/src/workers/core/dev-registry-proxy.worker.js +371 -0
- package/vendor/miniflare/dist/src/workers/core/do-wrapper.worker.js +67 -0
- package/vendor/miniflare/dist/src/workers/core/entry.worker.js +5516 -0
- package/vendor/miniflare/dist/src/workers/core/outbound.worker.js +147 -0
- package/vendor/miniflare/dist/src/workers/d1/database.worker.js +298 -0
- package/vendor/miniflare/dist/src/workers/d1/entry.worker.js +143 -0
- package/vendor/miniflare/dist/src/workers/dispatch-namespace/dispatch-namespace-proxy.worker.js +2574 -0
- package/vendor/miniflare/dist/src/workers/dispatch-namespace/dispatch-namespace.worker.js +11 -0
- package/vendor/miniflare/dist/src/workers/email/email-store.worker.js +606 -0
- package/vendor/miniflare/dist/src/workers/email/email.worker.js +26 -0
- package/vendor/miniflare/dist/src/workers/email/send_email.worker.js +3703 -0
- package/vendor/miniflare/dist/src/workers/flagship/binding.worker.js +394 -0
- package/vendor/miniflare/dist/src/workers/flagship/object.worker.js +349 -0
- package/vendor/miniflare/dist/src/workers/hello-world/binding.worker.js +18 -0
- package/vendor/miniflare/dist/src/workers/hello-world/object.worker.js +13 -0
- package/vendor/miniflare/dist/src/workers/images/images.worker.js +450 -0
- package/vendor/miniflare/dist/src/workers/k2/binding.worker.js +2583 -0
- package/vendor/miniflare/dist/src/workers/kv/namespace.worker.js +323 -0
- package/vendor/miniflare/dist/src/workers/kv/sites.worker.js +148 -0
- package/vendor/miniflare/dist/src/workers/local-explorer/explorer.worker.js +17275 -0
- package/vendor/miniflare/dist/src/workers/observability/collector.worker.js +725 -0
- package/vendor/miniflare/dist/src/workers/pipelines/pipeline.worker.js +9 -0
- package/vendor/miniflare/dist/src/workers/queues/broker.worker.js +391 -0
- package/vendor/miniflare/dist/src/workers/r2/bucket.worker.js +1171 -0
- package/vendor/miniflare/dist/src/workers/r2/public.worker.js +1319 -0
- package/vendor/miniflare/dist/src/workers/r2/s3/index.worker.js +11128 -0
- package/vendor/miniflare/dist/src/workers/ratelimit/ratelimit-object.worker.js +55 -0
- package/vendor/miniflare/dist/src/workers/ratelimit/ratelimit.worker.js +69 -0
- package/vendor/miniflare/dist/src/workers/secrets-store/secret.worker.js +64 -0
- package/vendor/miniflare/dist/src/workers/shared/index.worker.js +730 -0
- package/vendor/miniflare/dist/src/workers/shared/object-entry.worker.js +33 -0
- package/vendor/miniflare/dist/src/workers/shared/remote-proxy-client.worker.js +2698 -0
- package/vendor/miniflare/dist/src/workers/shared/zod.worker.js +15459 -0
- package/vendor/miniflare/dist/src/workers/stream/binding.worker.js +282 -0
- package/vendor/miniflare/dist/src/workers/stream/object.worker.js +479 -0
- package/vendor/miniflare/dist/src/workers/workflows/binding.worker.js +5137 -0
- package/vendor/miniflare/dist/src/workers/workflows/wrapped-binding.worker.js +111 -0
- package/vendor/miniflare/package.json +135 -0
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
// src/workers/observability/collector.worker.ts
|
|
2
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
3
|
+
|
|
4
|
+
// src/workers/observability/tail-to-store.ts
|
|
5
|
+
function toMs(timestamp) {
|
|
6
|
+
return typeof timestamp == "number" ? timestamp : timestamp.getTime();
|
|
7
|
+
}
|
|
8
|
+
function ids(event) {
|
|
9
|
+
return event.event.type === "onset" || event.event.type === "spanOpen" ? {
|
|
10
|
+
traceId: event.spanContext.traceId,
|
|
11
|
+
spanId: event.event.spanId,
|
|
12
|
+
parentId: event.spanContext.spanId
|
|
13
|
+
} : {
|
|
14
|
+
traceId: event.spanContext.traceId,
|
|
15
|
+
spanId: event.spanContext.spanId
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function friendlyKind(faasTrigger, name) {
|
|
19
|
+
if (faasTrigger)
|
|
20
|
+
switch (faasTrigger) {
|
|
21
|
+
case "http":
|
|
22
|
+
return "http";
|
|
23
|
+
case "timer":
|
|
24
|
+
return "scheduled";
|
|
25
|
+
case "pubsub":
|
|
26
|
+
return "queue";
|
|
27
|
+
case "email":
|
|
28
|
+
return "email";
|
|
29
|
+
case "jsrpc":
|
|
30
|
+
return "jsrpc";
|
|
31
|
+
case "websocket":
|
|
32
|
+
return "websocket";
|
|
33
|
+
case "trace":
|
|
34
|
+
return "trace";
|
|
35
|
+
default:
|
|
36
|
+
return "worker";
|
|
37
|
+
}
|
|
38
|
+
let n = name.toLowerCase();
|
|
39
|
+
return n.includes("kv") ? "kv" : n.includes("d1") ? "d1" : n.includes("r2") ? "r2" : n.includes("queue") ? "queue" : n.includes("durable") || n.includes("do_") ? "do" : n.includes("cache") ? "cache" : n.includes("fetch") ? "fetch" : "span";
|
|
40
|
+
}
|
|
41
|
+
function describeTrigger(info) {
|
|
42
|
+
switch (info.type) {
|
|
43
|
+
case "fetch":
|
|
44
|
+
return {
|
|
45
|
+
name: info.method,
|
|
46
|
+
attributes: {
|
|
47
|
+
"faas.trigger": "http",
|
|
48
|
+
"http.request.method": info.method,
|
|
49
|
+
"url.full": info.url
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
case "jsrpc":
|
|
53
|
+
return { name: "jsrpc", attributes: { "faas.trigger": "jsrpc" } };
|
|
54
|
+
case "scheduled":
|
|
55
|
+
return {
|
|
56
|
+
name: "scheduled",
|
|
57
|
+
attributes: { "faas.trigger": "timer", "faas.cron": info.cron }
|
|
58
|
+
};
|
|
59
|
+
case "alarm":
|
|
60
|
+
return { name: "alarm", attributes: { "faas.trigger": "timer" } };
|
|
61
|
+
case "queue":
|
|
62
|
+
return {
|
|
63
|
+
name: "queue",
|
|
64
|
+
attributes: {
|
|
65
|
+
"faas.trigger": "pubsub",
|
|
66
|
+
"cloudflare.queue.name": info.queueName
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
case "email":
|
|
70
|
+
return {
|
|
71
|
+
name: "email",
|
|
72
|
+
attributes: {
|
|
73
|
+
"faas.trigger": "email",
|
|
74
|
+
"cloudflare.email.to": info.rcptTo
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
case "trace":
|
|
78
|
+
return { name: "trace", attributes: { "faas.trigger": "trace" } };
|
|
79
|
+
case "hibernatableWebSocket":
|
|
80
|
+
return {
|
|
81
|
+
name: "hibernatableWebSocket",
|
|
82
|
+
attributes: { "faas.trigger": "websocket" }
|
|
83
|
+
};
|
|
84
|
+
default:
|
|
85
|
+
return { name: info.type, attributes: { "faas.trigger": "other" } };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function isErrorOutcome(outcome) {
|
|
89
|
+
return outcome !== "ok";
|
|
90
|
+
}
|
|
91
|
+
var TailToStoreHandler = class {
|
|
92
|
+
constructor(writer2, store, onset, worker) {
|
|
93
|
+
this.writer = writer2;
|
|
94
|
+
this.worker = worker;
|
|
95
|
+
this.#owner = this.writer.createOwner(store);
|
|
96
|
+
let { traceId, spanId, parentId } = ids(onset);
|
|
97
|
+
if (!spanId)
|
|
98
|
+
return;
|
|
99
|
+
this.#traceId = traceId, this.#startMs = toMs(onset.timestamp);
|
|
100
|
+
let { name, attributes: triggerAttributes } = describeTrigger(
|
|
101
|
+
onset.event.info
|
|
102
|
+
), attributes = {};
|
|
103
|
+
for (let attr of onset.event.attributes ?? [])
|
|
104
|
+
attributes[attr.name] = normalizeAttr(attr.value);
|
|
105
|
+
Object.assign(attributes, triggerAttributes), onset.invocationId && (attributes["faas.invocation_id"] = onset.invocationId), this.#invocationBody = onset.event.info.type === "fetch" ? `${onset.event.info.method} ${onset.event.info.url}` : name;
|
|
106
|
+
let pending = {
|
|
107
|
+
traceId,
|
|
108
|
+
startMs: this.#startMs,
|
|
109
|
+
name,
|
|
110
|
+
outcome: null,
|
|
111
|
+
error: null,
|
|
112
|
+
errored: !1,
|
|
113
|
+
closed: !1
|
|
114
|
+
};
|
|
115
|
+
this.#spans.set(spanId, pending), this.#open({
|
|
116
|
+
traceId,
|
|
117
|
+
spanId,
|
|
118
|
+
// A sub-invocation (e.g. downstream of a service binding) carries the
|
|
119
|
+
// caller's span as its parent, so it nests into one distributed trace;
|
|
120
|
+
// a true top-level invocation has no inherited parent (→ null root).
|
|
121
|
+
parentId: parentId ?? null,
|
|
122
|
+
service: this.worker ?? null,
|
|
123
|
+
name,
|
|
124
|
+
kind: friendlyKind(attributes["faas.trigger"], name),
|
|
125
|
+
startMs: this.#startMs,
|
|
126
|
+
durationMs: null,
|
|
127
|
+
outcome: null,
|
|
128
|
+
error: null,
|
|
129
|
+
attributes
|
|
130
|
+
}), this.#rootSpanId = spanId;
|
|
131
|
+
}
|
|
132
|
+
writer;
|
|
133
|
+
worker;
|
|
134
|
+
#spans = /* @__PURE__ */ new Map();
|
|
135
|
+
#rootSpanId = null;
|
|
136
|
+
#traceId = null;
|
|
137
|
+
#startMs = null;
|
|
138
|
+
#invocationBody = null;
|
|
139
|
+
#rows = /* @__PURE__ */ new Map();
|
|
140
|
+
#owner;
|
|
141
|
+
spanOpen(event) {
|
|
142
|
+
let { traceId, spanId, parentId } = ids(event);
|
|
143
|
+
if (!spanId)
|
|
144
|
+
return;
|
|
145
|
+
let startMs = toMs(event.timestamp), pending = {
|
|
146
|
+
traceId,
|
|
147
|
+
startMs,
|
|
148
|
+
name: event.event.name,
|
|
149
|
+
outcome: null,
|
|
150
|
+
error: null,
|
|
151
|
+
errored: !1,
|
|
152
|
+
closed: !1
|
|
153
|
+
};
|
|
154
|
+
this.#spans.set(spanId, pending), this.#open({
|
|
155
|
+
traceId,
|
|
156
|
+
spanId,
|
|
157
|
+
parentId: parentId ?? null,
|
|
158
|
+
service: this.worker ?? null,
|
|
159
|
+
name: event.event.name,
|
|
160
|
+
kind: friendlyKind(void 0, event.event.name),
|
|
161
|
+
startMs,
|
|
162
|
+
durationMs: null,
|
|
163
|
+
outcome: null,
|
|
164
|
+
error: null,
|
|
165
|
+
attributes: null
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
spanClose(event) {
|
|
169
|
+
let { traceId, spanId } = ids(event), pending = spanId ? this.#spans.get(spanId) : void 0;
|
|
170
|
+
spanId && pending && (isErrorOutcome(event.event.outcome) && (pending.errored = !0, pending.outcome ??= event.event.outcome), this.#close(traceId, spanId, pending, toMs(event.timestamp), null));
|
|
171
|
+
}
|
|
172
|
+
attributes(event) {
|
|
173
|
+
let { traceId, spanId } = ids(event);
|
|
174
|
+
if (!spanId || !this.#spans.has(spanId))
|
|
175
|
+
return;
|
|
176
|
+
let attrs = {};
|
|
177
|
+
for (let attr of event.event.info)
|
|
178
|
+
attrs[attr.name] = normalizeAttr(attr.value);
|
|
179
|
+
Object.keys(attrs).length > 0 && this.#merge(traceId, spanId, attrs);
|
|
180
|
+
}
|
|
181
|
+
return(event) {
|
|
182
|
+
let { traceId, spanId } = ids(event), pending = spanId ? this.#spans.get(spanId) : void 0;
|
|
183
|
+
spanId && pending && event.event.info?.type === "fetch" && this.#merge(traceId, spanId, {
|
|
184
|
+
"http.response.status_code": event.event.info.statusCode
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
log(event) {
|
|
188
|
+
let { traceId, spanId } = ids(event), level = event.event.level === "log" ? "info" : event.event.level;
|
|
189
|
+
this.#append({
|
|
190
|
+
traceId,
|
|
191
|
+
spanId: spanId ?? null,
|
|
192
|
+
tsMs: toMs(event.timestamp),
|
|
193
|
+
level,
|
|
194
|
+
message: serialize(event.event.message),
|
|
195
|
+
operation: null
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
exception(event) {
|
|
199
|
+
let { traceId, spanId } = ids(event), pending = spanId ? this.#spans.get(spanId) : void 0, type = event.event.name || "Error", message = event.event.message ?? "", head = `${type}: ${message}`, text = event.event.stack ? `${head}
|
|
200
|
+
${event.event.stack}` : head;
|
|
201
|
+
pending && (pending.errored = !0, pending.outcome = "error", pending.error = head), this.#append({
|
|
202
|
+
traceId,
|
|
203
|
+
spanId: spanId ?? null,
|
|
204
|
+
tsMs: toMs(event.timestamp),
|
|
205
|
+
level: "error",
|
|
206
|
+
message: serialize(text),
|
|
207
|
+
operation: pending?.name ?? null
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
async outcome(event) {
|
|
211
|
+
let endMs = toMs(event.timestamp), traceId = this.#traceId ?? event.spanContext.traceId, root = this.#rootSpanId ? this.#spans.get(this.#rootSpanId) : void 0, rootAccepted = !1;
|
|
212
|
+
root && this.#rootSpanId && (isErrorOutcome(event.event.outcome) && (root.errored = !0), root.outcome = event.event.outcome, rootAccepted = this.#close(traceId, this.#rootSpanId, root, endMs, {
|
|
213
|
+
"cloudflare.outcome": event.event.outcome,
|
|
214
|
+
cpu_time_ms: event.event.cpuTime,
|
|
215
|
+
wall_time_ms: event.event.wallTime
|
|
216
|
+
}));
|
|
217
|
+
for (let [spanId, pending] of this.#spans)
|
|
218
|
+
pending.closed || this.#close(pending.traceId, spanId, pending, endMs, null);
|
|
219
|
+
this.#rootSpanId && this.#invocationBody !== null && this.#append({
|
|
220
|
+
traceId,
|
|
221
|
+
spanId: rootAccepted ? this.#rootSpanId : null,
|
|
222
|
+
tsMs: this.#startMs ?? endMs,
|
|
223
|
+
level: root?.errored ? "error" : "info",
|
|
224
|
+
message: serialize(this.#invocationBody),
|
|
225
|
+
operation: null
|
|
226
|
+
}), await this.writer.drain(this.#owner);
|
|
227
|
+
}
|
|
228
|
+
#open(input) {
|
|
229
|
+
let key = rowKey(input.traceId, input.spanId);
|
|
230
|
+
this.#rows.set(key, input);
|
|
231
|
+
}
|
|
232
|
+
/** Fold attributes into a buffered row, so merging costs no round-trip. */
|
|
233
|
+
#merge(traceId, spanId, attributes) {
|
|
234
|
+
let key = rowKey(traceId, spanId), row = this.#rows.get(key);
|
|
235
|
+
row && (row.attributes = { ...row.attributes ?? {}, ...attributes });
|
|
236
|
+
}
|
|
237
|
+
#append(log) {
|
|
238
|
+
this.writer.enqueueLog(this.#owner, log);
|
|
239
|
+
}
|
|
240
|
+
/** Finish a span, setting its duration and outcome and adding any final
|
|
241
|
+
* attributes. Runs at most once per span (guarded by `closed`). */
|
|
242
|
+
#close(traceId, spanId, pending, endMs, attributes) {
|
|
243
|
+
if (pending.closed)
|
|
244
|
+
return !0;
|
|
245
|
+
pending.closed = !0;
|
|
246
|
+
let key = rowKey(traceId, spanId), row = this.#rows.get(key);
|
|
247
|
+
if (!row)
|
|
248
|
+
return !1;
|
|
249
|
+
row.durationMs = Math.max(0, endMs - pending.startMs), row.outcome = pending.outcome ?? (pending.errored ? "error" : "ok"), row.error = pending.error, attributes && (row.attributes = { ...row.attributes ?? {}, ...attributes });
|
|
250
|
+
let accepted = this.writer.enqueueCompletedSpan(this.#owner, row);
|
|
251
|
+
return this.#rows.delete(key), this.#spans.delete(spanId), accepted;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
function rowKey(traceId, spanId) {
|
|
255
|
+
return `${traceId}\0${spanId}`;
|
|
256
|
+
}
|
|
257
|
+
function normalizeAttr(value) {
|
|
258
|
+
return Array.isArray(value) ? value.map((v) => typeof v == "bigint" ? bigintToJson(v) : v) : typeof value == "bigint" ? bigintToJson(value) : value;
|
|
259
|
+
}
|
|
260
|
+
function bigintToJson(value) {
|
|
261
|
+
return value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER ? Number(value) : value.toString();
|
|
262
|
+
}
|
|
263
|
+
function serialize(value) {
|
|
264
|
+
try {
|
|
265
|
+
return JSON.stringify(value ?? "");
|
|
266
|
+
} catch {
|
|
267
|
+
return String(value);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/workers/observability/trace-store.ts
|
|
272
|
+
import { DurableObject } from "cloudflare:workers";
|
|
273
|
+
var SCHEMA = [
|
|
274
|
+
`CREATE TABLE IF NOT EXISTS spans (
|
|
275
|
+
trace_id TEXT NOT NULL,
|
|
276
|
+
span_id TEXT NOT NULL,
|
|
277
|
+
parent_id TEXT,
|
|
278
|
+
service TEXT,
|
|
279
|
+
name TEXT,
|
|
280
|
+
kind TEXT,
|
|
281
|
+
start_ms INTEGER,
|
|
282
|
+
duration_ms INTEGER, -- whole ms; NULL while the span is still running
|
|
283
|
+
outcome TEXT,
|
|
284
|
+
error TEXT,
|
|
285
|
+
attributes BLOB,
|
|
286
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
287
|
+
PRIMARY KEY (trace_id, span_id)
|
|
288
|
+
)`,
|
|
289
|
+
"CREATE INDEX IF NOT EXISTS spans_roots ON spans (start_ms) WHERE parent_id IS NULL",
|
|
290
|
+
`CREATE TABLE IF NOT EXISTS logs (
|
|
291
|
+
trace_id TEXT NOT NULL,
|
|
292
|
+
span_id TEXT,
|
|
293
|
+
seq INTEGER NOT NULL,
|
|
294
|
+
ts_ms INTEGER,
|
|
295
|
+
level TEXT,
|
|
296
|
+
message TEXT,
|
|
297
|
+
operation TEXT,
|
|
298
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
299
|
+
PRIMARY KEY (trace_id, seq)
|
|
300
|
+
)`,
|
|
301
|
+
"CREATE INDEX IF NOT EXISTS logs_by_level ON logs (level)"
|
|
302
|
+
], MAX_QUERY_ROWS = 1e4, SQL_WRITE_CHUNK_ROWS = 256, TraceStore = class extends DurableObject {
|
|
303
|
+
sql = this.ctx.storage.sql;
|
|
304
|
+
constructor(ctx, env) {
|
|
305
|
+
super(ctx, env), this.ctx.blockConcurrencyWhile(async () => {
|
|
306
|
+
for (let stmt of SCHEMA)
|
|
307
|
+
this.sql.exec(stmt);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
/** Persist one invocation's spans + logs. Called by the collector. */
|
|
311
|
+
persist(spans, logs) {
|
|
312
|
+
this.ctx.storage.transactionSync(() => {
|
|
313
|
+
for (let offset = 0; offset < spans.length; offset += SQL_WRITE_CHUNK_ROWS) {
|
|
314
|
+
let chunk = spans.slice(offset, offset + SQL_WRITE_CHUNK_ROWS);
|
|
315
|
+
this.sql.exec(
|
|
316
|
+
`INSERT INTO spans
|
|
317
|
+
(trace_id, span_id, parent_id, service, name, kind, start_ms, duration_ms, outcome, error, attributes)
|
|
318
|
+
SELECT
|
|
319
|
+
json_extract(value, '$.traceId'),
|
|
320
|
+
json_extract(value, '$.spanId'),
|
|
321
|
+
json_extract(value, '$.parentId'),
|
|
322
|
+
json_extract(value, '$.service'),
|
|
323
|
+
json_extract(value, '$.name'),
|
|
324
|
+
json_extract(value, '$.kind'),
|
|
325
|
+
json_extract(value, '$.startMs'),
|
|
326
|
+
json_extract(value, '$.durationMs'),
|
|
327
|
+
json_extract(value, '$.outcome'),
|
|
328
|
+
json_extract(value, '$.error'),
|
|
329
|
+
jsonb(json_extract(value, '$.attributes'))
|
|
330
|
+
FROM json_each(?)
|
|
331
|
+
WHERE true
|
|
332
|
+
ON CONFLICT (trace_id, span_id) DO UPDATE SET
|
|
333
|
+
parent_id = excluded.parent_id,
|
|
334
|
+
service = excluded.service,
|
|
335
|
+
name = excluded.name,
|
|
336
|
+
kind = excluded.kind,
|
|
337
|
+
start_ms = excluded.start_ms,
|
|
338
|
+
duration_ms = excluded.duration_ms,
|
|
339
|
+
outcome = excluded.outcome,
|
|
340
|
+
error = excluded.error,
|
|
341
|
+
attributes = excluded.attributes`,
|
|
342
|
+
JSON.stringify(
|
|
343
|
+
chunk.map((span) => ({
|
|
344
|
+
...span,
|
|
345
|
+
durationMs: span.durationMs === null ? null : Math.round(span.durationMs)
|
|
346
|
+
}))
|
|
347
|
+
)
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
let nextSeq = /* @__PURE__ */ new Map(), sequencedLogs = [];
|
|
351
|
+
for (let log of logs) {
|
|
352
|
+
let seq = nextSeq.get(log.traceId);
|
|
353
|
+
if (seq === void 0) {
|
|
354
|
+
let row = this.sql.exec(
|
|
355
|
+
"SELECT COALESCE(MAX(seq), -1) + 1 AS next FROM logs WHERE trace_id = ?",
|
|
356
|
+
log.traceId
|
|
357
|
+
).one();
|
|
358
|
+
seq = Number(row.next);
|
|
359
|
+
}
|
|
360
|
+
sequencedLogs.push({ ...log, seq }), nextSeq.set(log.traceId, seq + 1);
|
|
361
|
+
}
|
|
362
|
+
for (let offset = 0; offset < sequencedLogs.length; offset += SQL_WRITE_CHUNK_ROWS) {
|
|
363
|
+
let chunk = sequencedLogs.slice(
|
|
364
|
+
offset,
|
|
365
|
+
offset + SQL_WRITE_CHUNK_ROWS
|
|
366
|
+
);
|
|
367
|
+
this.sql.exec(
|
|
368
|
+
`INSERT INTO logs
|
|
369
|
+
(trace_id, span_id, seq, ts_ms, level, message, operation)
|
|
370
|
+
SELECT
|
|
371
|
+
json_extract(value, '$.traceId'),
|
|
372
|
+
json_extract(value, '$.spanId'),
|
|
373
|
+
json_extract(value, '$.seq'),
|
|
374
|
+
json_extract(value, '$.tsMs'),
|
|
375
|
+
json_extract(value, '$.level'),
|
|
376
|
+
json_extract(value, '$.message'),
|
|
377
|
+
json_extract(value, '$.operation')
|
|
378
|
+
FROM json_each(?)`,
|
|
379
|
+
JSON.stringify(chunk)
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Write-through capture (for long-running spans). A span is written across
|
|
386
|
+
* its lifetime instead of all at once on `outcome`, so the UI can show it
|
|
387
|
+
* in-flight: `openSpan` on start, `mergeAttributes` as they stream in, and
|
|
388
|
+
* `closeSpan` when it ends. `duration_ms IS NULL` marks a still-open span.
|
|
389
|
+
*/
|
|
390
|
+
/** Insert a span at open time (duration/outcome stay NULL until it closes). */
|
|
391
|
+
openSpan(s) {
|
|
392
|
+
this.sql.exec(
|
|
393
|
+
`INSERT INTO spans
|
|
394
|
+
(trace_id, span_id, parent_id, service, name, kind, start_ms, duration_ms, outcome, error, attributes)
|
|
395
|
+
VALUES (?,?,?,?,?,?,?,?,?,?, jsonb(?))
|
|
396
|
+
ON CONFLICT (trace_id, span_id) DO NOTHING`,
|
|
397
|
+
s.traceId,
|
|
398
|
+
s.spanId,
|
|
399
|
+
s.parentId,
|
|
400
|
+
s.service,
|
|
401
|
+
s.name,
|
|
402
|
+
s.kind,
|
|
403
|
+
s.startMs,
|
|
404
|
+
s.durationMs == null ? null : Math.round(s.durationMs),
|
|
405
|
+
s.outcome,
|
|
406
|
+
s.error,
|
|
407
|
+
s.attributes ? JSON.stringify(s.attributes) : null
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
/** Merge attributes onto an open span as the tail stream emits them. */
|
|
411
|
+
mergeAttributes(traceId, spanId, attributes) {
|
|
412
|
+
this.sql.exec(
|
|
413
|
+
`UPDATE spans
|
|
414
|
+
SET attributes = jsonb_patch(COALESCE(attributes, jsonb('{}')), jsonb(?))
|
|
415
|
+
WHERE trace_id = ? AND span_id = ?`,
|
|
416
|
+
JSON.stringify(attributes),
|
|
417
|
+
traceId,
|
|
418
|
+
spanId
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
/** Finalise a span: set duration/outcome/error and merge any final attributes. */
|
|
422
|
+
closeSpan(traceId, spanId, close) {
|
|
423
|
+
this.sql.exec(
|
|
424
|
+
`UPDATE spans
|
|
425
|
+
SET duration_ms = ?, outcome = ?, error = ?,
|
|
426
|
+
attributes = jsonb_patch(COALESCE(attributes, jsonb('{}')), jsonb(?))
|
|
427
|
+
WHERE trace_id = ? AND span_id = ?`,
|
|
428
|
+
Math.round(close.durationMs),
|
|
429
|
+
close.outcome,
|
|
430
|
+
close.error,
|
|
431
|
+
close.attributes ? JSON.stringify(close.attributes) : "{}",
|
|
432
|
+
traceId,
|
|
433
|
+
spanId
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
/** Append a single log, assigning the next per-trace `seq` (store-owned). */
|
|
437
|
+
appendLog(log) {
|
|
438
|
+
let { next } = this.sql.exec(
|
|
439
|
+
"SELECT COALESCE(MAX(seq), -1) + 1 AS next FROM logs WHERE trace_id = ?",
|
|
440
|
+
log.traceId
|
|
441
|
+
).one();
|
|
442
|
+
this.sql.exec(
|
|
443
|
+
`INSERT INTO logs
|
|
444
|
+
(trace_id, span_id, seq, ts_ms, level, message, operation)
|
|
445
|
+
VALUES (?,?,?,?,?,?,?)`,
|
|
446
|
+
log.traceId,
|
|
447
|
+
log.spanId,
|
|
448
|
+
Number(next),
|
|
449
|
+
log.tsMs,
|
|
450
|
+
log.level,
|
|
451
|
+
log.message,
|
|
452
|
+
log.operation
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* The only way to read the store: a single read-only SQL query. The
|
|
457
|
+
* Observability tab and coding agents both go through here (the UI has a set of
|
|
458
|
+
* built-in queries; agents write their own), so the `spans` and `logs` schema
|
|
459
|
+
* acts as the contract and is documented in the `/query` endpoint's OpenAPI
|
|
460
|
+
* description.
|
|
461
|
+
*
|
|
462
|
+
* Because this runs SQL we did not write, and workerd's DO SQLite has no
|
|
463
|
+
* read-only execution mode to rely on (`PRAGMA query_only` is rejected with
|
|
464
|
+
* SQLITE_AUTH), we guard it two ways. First a syntactic check: checking only
|
|
465
|
+
* the first keyword is not enough (`WITH … DELETE` is a single statement that
|
|
466
|
+
* still starts with `WITH`), so we remove comments and anything inside quotes —
|
|
467
|
+
* so a value like `'…delete…'` or a `;` inside a string cannot trip the checks —
|
|
468
|
+
* then require a single statement (no `;`) that starts with `SELECT`/`WITH` and
|
|
469
|
+
* contains no data- or schema-changing keyword. Second, and not relying on that
|
|
470
|
+
* regex, the statement runs inside a transaction that is always rolled back, so
|
|
471
|
+
* any write or DDL that slipped past the checks is discarded rather than
|
|
472
|
+
* persisted. Values are always passed as bound `params`, and at most
|
|
473
|
+
* `MAX_QUERY_ROWS` rows are returned.
|
|
474
|
+
*
|
|
475
|
+
* `attributes` is stored as JSONB; wrap it with `json(attributes)` to read it
|
|
476
|
+
* back as JSON (the built-in queries already do).
|
|
477
|
+
*/
|
|
478
|
+
query(sql, params = []) {
|
|
479
|
+
let statement = sql.trim().replace(/;\s*$/, ""), stripped = statement.replace(/--[^\n]*/g, " ").replace(/\/\*[\s\S]*?\*\//g, " ").replace(/'(?:[^']|'')*'/g, "''").replace(/"(?:[^"]|"")*"/g, '""').trim();
|
|
480
|
+
if (!/^(SELECT|WITH)\b/i.test(stripped))
|
|
481
|
+
throw new Error("Only read-only SELECT/WITH queries are allowed");
|
|
482
|
+
if (stripped.includes(";"))
|
|
483
|
+
throw new Error("Only a single statement is allowed");
|
|
484
|
+
if (/\b(INSERT|UPDATE|DELETE|DROP|ALTER|CREATE|TRUNCATE|PRAGMA|ATTACH|DETACH|VACUUM|REINDEX|ANALYZE|BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|LOAD_EXTENSION)\b|\bREPLACE\s+INTO\b/i.test(
|
|
485
|
+
stripped
|
|
486
|
+
))
|
|
487
|
+
throw new Error("Only read-only SELECT/WITH queries are allowed");
|
|
488
|
+
let columns = [], rows = [], rollback = /* @__PURE__ */ Symbol("rollback");
|
|
489
|
+
try {
|
|
490
|
+
this.ctx.storage.transactionSync(() => {
|
|
491
|
+
let cursor = this.sql.exec(statement, ...params);
|
|
492
|
+
columns.push(...cursor.columnNames);
|
|
493
|
+
for (let row of cursor.raw()) {
|
|
494
|
+
if (rows.length >= MAX_QUERY_ROWS)
|
|
495
|
+
break;
|
|
496
|
+
rows.push([...row]);
|
|
497
|
+
}
|
|
498
|
+
throw rollback;
|
|
499
|
+
});
|
|
500
|
+
} catch (e) {
|
|
501
|
+
if (e !== rollback)
|
|
502
|
+
throw e;
|
|
503
|
+
}
|
|
504
|
+
return { columns, rows };
|
|
505
|
+
}
|
|
506
|
+
/** Delete all captured data. An RPC method; nothing calls it yet. */
|
|
507
|
+
clear() {
|
|
508
|
+
this.sql.exec("DELETE FROM logs"), this.sql.exec("DELETE FROM spans");
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
// src/workers/observability/trace-writer.ts
|
|
513
|
+
var TraceWriter = class {
|
|
514
|
+
constructor(capacity = 20480) {
|
|
515
|
+
this.capacity = capacity;
|
|
516
|
+
}
|
|
517
|
+
capacity;
|
|
518
|
+
#active = emptyBatch();
|
|
519
|
+
#owners = /* @__PURE__ */ new Map();
|
|
520
|
+
#flushRequested = !1;
|
|
521
|
+
#inFlight;
|
|
522
|
+
#pendingPump = !1;
|
|
523
|
+
#pendingClearPumps = /* @__PURE__ */ new Set();
|
|
524
|
+
#clearInFlight;
|
|
525
|
+
createOwner(store) {
|
|
526
|
+
let owner = /* @__PURE__ */ Symbol("trace-writer-owner");
|
|
527
|
+
return this.#owners.set(owner, {
|
|
528
|
+
store,
|
|
529
|
+
nextVersion: 0,
|
|
530
|
+
completedVersion: 0,
|
|
531
|
+
failure: void 0,
|
|
532
|
+
waiters: []
|
|
533
|
+
}), owner;
|
|
534
|
+
}
|
|
535
|
+
/** Queue a completed span snapshot if the active batch has capacity. */
|
|
536
|
+
enqueueCompletedSpan(owner, row) {
|
|
537
|
+
if (row.durationMs === null)
|
|
538
|
+
throw new Error("TraceWriter only accepts completed spans");
|
|
539
|
+
let key = rowKey2(row.traceId, row.spanId);
|
|
540
|
+
if (!this.#active.spans.has(key) && this.#activeRows() >= this.capacity)
|
|
541
|
+
return !1;
|
|
542
|
+
let version = this.#nextVersion(owner);
|
|
543
|
+
return this.#active.spans.set(key, {
|
|
544
|
+
owner,
|
|
545
|
+
version,
|
|
546
|
+
row: cloneSpan(row)
|
|
547
|
+
}), this.#recordOwnerVersion(owner, version), this.#activeRows() >= this.capacity && this.flush(owner), !0;
|
|
548
|
+
}
|
|
549
|
+
enqueueLog(owner, row) {
|
|
550
|
+
if (this.#activeRows() >= this.capacity)
|
|
551
|
+
return !1;
|
|
552
|
+
let version = this.#nextVersion(owner);
|
|
553
|
+
return this.#active.logs.push({ owner, version, row: { ...row } }), this.#recordOwnerVersion(owner, version), this.flush(owner), !0;
|
|
554
|
+
}
|
|
555
|
+
flush(owner) {
|
|
556
|
+
this.#active.spans.size === 0 && this.#active.logs.length === 0 || (this.#flushRequested = !0, this.#pump(owner));
|
|
557
|
+
}
|
|
558
|
+
/** Flush and wait for all rows accepted for this invocation. */
|
|
559
|
+
async drain(owner) {
|
|
560
|
+
let targetVersion = this.#owner(owner).nextVersion;
|
|
561
|
+
this.flush(owner);
|
|
562
|
+
try {
|
|
563
|
+
await this.#waitFor(owner, targetVersion);
|
|
564
|
+
} finally {
|
|
565
|
+
this.#owners.delete(owner);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
/** Serialise a store clear with persistence without deleting newer rows. */
|
|
569
|
+
clear(store) {
|
|
570
|
+
let batch = this.#active;
|
|
571
|
+
this.#active = emptyBatch(), this.#flushRequested = !1;
|
|
572
|
+
let previous = this.#clearInFlight, tracked = (previous === void 0 ? this.#clear(store, batch) : previous.catch(() => {
|
|
573
|
+
}).then(() => this.#clear(store, batch))).finally(() => {
|
|
574
|
+
this.#clearInFlight === tracked && (this.#clearInFlight = void 0);
|
|
575
|
+
});
|
|
576
|
+
return this.#clearInFlight = tracked, tracked;
|
|
577
|
+
}
|
|
578
|
+
async #clear(store, batch) {
|
|
579
|
+
this.#inFlight !== void 0 && await this.#inFlight, (batch.spans.size > 0 || batch.logs.length > 0) && await this.#persistBatch(batch, store), await store.clear();
|
|
580
|
+
}
|
|
581
|
+
#pump(owner) {
|
|
582
|
+
if (!(!this.#flushRequested || this.#active.spans.size === 0 && this.#active.logs.length === 0) && this.#active.ownerVersions.has(owner)) {
|
|
583
|
+
if (this.#clearInFlight !== void 0) {
|
|
584
|
+
if (!this.#pendingClearPumps.has(owner)) {
|
|
585
|
+
this.#pendingClearPumps.add(owner);
|
|
586
|
+
let clearInFlight = this.#clearInFlight, resume = () => {
|
|
587
|
+
this.#pendingClearPumps.delete(owner), this.#pump(owner);
|
|
588
|
+
};
|
|
589
|
+
clearInFlight.then(resume, resume);
|
|
590
|
+
}
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
if (this.#inFlight !== void 0) {
|
|
594
|
+
if (!this.#pendingPump && this.#active.ownerVersions.has(owner)) {
|
|
595
|
+
this.#pendingPump = !0;
|
|
596
|
+
let inFlight = this.#inFlight, store = this.#owner(owner).store;
|
|
597
|
+
inFlight.then(() => {
|
|
598
|
+
this.#inFlight === inFlight && (this.#inFlight = void 0), this.#pendingPump = !1, this.#startBatch(store);
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
this.#startBatch(this.#owner(owner).store);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
#startBatch(store) {
|
|
607
|
+
if (this.#clearInFlight !== void 0 || !this.#flushRequested || this.#active.spans.size === 0 && this.#active.logs.length === 0)
|
|
608
|
+
return;
|
|
609
|
+
let batch = this.#active;
|
|
610
|
+
this.#active = emptyBatch(), this.#flushRequested = !1, this.#persistBatch(batch, store);
|
|
611
|
+
}
|
|
612
|
+
#persistBatch(batch, store) {
|
|
613
|
+
let spans = Array.from(batch.spans.values(), ({ row }) => row), logs = batch.logs.map(({ row }) => row), operation;
|
|
614
|
+
try {
|
|
615
|
+
operation = store.persist(spans, logs);
|
|
616
|
+
} catch (error) {
|
|
617
|
+
return this.#finishBatch(batch, error), Promise.resolve();
|
|
618
|
+
}
|
|
619
|
+
let inFlight = Promise.resolve(operation).then(
|
|
620
|
+
() => this.#finishBatch(batch),
|
|
621
|
+
(error) => this.#finishBatch(batch, error)
|
|
622
|
+
);
|
|
623
|
+
return this.#inFlight = inFlight, inFlight.then(() => {
|
|
624
|
+
this.#inFlight === inFlight && !this.#pendingPump && (this.#inFlight = void 0);
|
|
625
|
+
}), inFlight;
|
|
626
|
+
}
|
|
627
|
+
#finishBatch(batch, error) {
|
|
628
|
+
for (let [owner, version] of batch.ownerVersions) {
|
|
629
|
+
let state = this.#owners.get(owner);
|
|
630
|
+
state && (state.completedVersion = Math.max(state.completedVersion, version), error !== void 0 && state.failure === void 0 && (state.failure = error), this.#settleWaiters(state));
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
#nextVersion(owner) {
|
|
634
|
+
let state = this.#owner(owner);
|
|
635
|
+
return state.nextVersion++, state.nextVersion;
|
|
636
|
+
}
|
|
637
|
+
#recordOwnerVersion(owner, version) {
|
|
638
|
+
this.#active.ownerVersions.set(
|
|
639
|
+
owner,
|
|
640
|
+
Math.max(this.#active.ownerVersions.get(owner) ?? 0, version)
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
#waitFor(owner, targetVersion) {
|
|
644
|
+
let state = this.#owner(owner);
|
|
645
|
+
return state.completedVersion >= targetVersion ? state.failure === void 0 ? Promise.resolve() : Promise.reject(state.failure) : new Promise((resolve, reject) => {
|
|
646
|
+
state.waiters.push({ targetVersion, resolve, reject });
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
#settleWaiters(state) {
|
|
650
|
+
let pending = [];
|
|
651
|
+
for (let waiter of state.waiters)
|
|
652
|
+
state.completedVersion < waiter.targetVersion ? pending.push(waiter) : state.failure === void 0 ? waiter.resolve() : waiter.reject(state.failure);
|
|
653
|
+
state.waiters = pending;
|
|
654
|
+
}
|
|
655
|
+
#owner(owner) {
|
|
656
|
+
let state = this.#owners.get(owner);
|
|
657
|
+
if (!state)
|
|
658
|
+
throw new Error("Unknown trace writer owner");
|
|
659
|
+
return state;
|
|
660
|
+
}
|
|
661
|
+
#activeRows() {
|
|
662
|
+
return this.#active.spans.size + this.#active.logs.length;
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
function emptyBatch() {
|
|
666
|
+
return {
|
|
667
|
+
spans: /* @__PURE__ */ new Map(),
|
|
668
|
+
logs: [],
|
|
669
|
+
ownerVersions: /* @__PURE__ */ new Map()
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function cloneSpan(row) {
|
|
673
|
+
return {
|
|
674
|
+
...row,
|
|
675
|
+
attributes: row.attributes === null ? null : Object.fromEntries(
|
|
676
|
+
Object.entries(row.attributes).map(([name, value]) => [
|
|
677
|
+
name,
|
|
678
|
+
Array.isArray(value) ? [...value] : value
|
|
679
|
+
])
|
|
680
|
+
)
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
function rowKey2(traceId, spanId) {
|
|
684
|
+
return `${traceId}\0${spanId}`;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/workers/observability/collector.worker.ts
|
|
688
|
+
var writer, LocalObservabilityCollector = class extends WorkerEntrypoint {
|
|
689
|
+
tailStream(onset) {
|
|
690
|
+
let store = this.env.TRACE_STORE.get(
|
|
691
|
+
this.env.TRACE_STORE.idFromName("singleton")
|
|
692
|
+
);
|
|
693
|
+
writer ??= new TraceWriter(this.env.TRACE_BATCH_SIZE);
|
|
694
|
+
let worker = this.ctx.props?.worker;
|
|
695
|
+
return new TailToStoreHandler(writer, store, onset, worker);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Reads from the trace store. A single `POST /query` runs read-only SQL against
|
|
699
|
+
* the `spans` and `logs` tables. The Local Explorer's Observability API
|
|
700
|
+
* forwards requests here, so the UI's built-in views and any coding agent all
|
|
701
|
+
* go through this one endpoint. The query validation lives in
|
|
702
|
+
* `TraceStore.query`.
|
|
703
|
+
*/
|
|
704
|
+
async fetch(request) {
|
|
705
|
+
let url = new URL(request.url), store = this.env.TRACE_STORE.get(
|
|
706
|
+
this.env.TRACE_STORE.idFromName("singleton")
|
|
707
|
+
);
|
|
708
|
+
if (url.pathname === "/query" && request.method === "POST") {
|
|
709
|
+
let { sql, params } = await request.json();
|
|
710
|
+
if (typeof sql != "string")
|
|
711
|
+
return Response.json({ error: "missing 'sql'" }, { status: 400 });
|
|
712
|
+
try {
|
|
713
|
+
return Response.json(await store.query(sql, params ?? []));
|
|
714
|
+
} catch (err) {
|
|
715
|
+
let message = err instanceof Error ? err.message : String(err);
|
|
716
|
+
return Response.json({ error: message }, { status: 400 });
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return url.pathname === "/clear" && request.method === "POST" ? (writer ??= new TraceWriter(this.env.TRACE_BATCH_SIZE), await writer.clear(store), Response.json({ success: !0 })) : new Response("not found", { status: 404 });
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
export {
|
|
723
|
+
TraceStore,
|
|
724
|
+
LocalObservabilityCollector as default
|
|
725
|
+
};
|