@zerotal/monitor 1.0.0
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +174 -0
- package/package.json +74 -0
- package/src/MonitorStore.ts +1471 -0
- package/src/alerting.ts +173 -0
- package/src/config.ts +171 -0
- package/src/facades/Monitor.ts +75 -0
- package/src/index.ts +62 -0
- package/src/instance.ts +16 -0
- package/src/middleware/MonitorAuthMiddleware.ts +20 -0
- package/src/middleware/MonitorPayloadMiddleware.ts +96 -0
- package/src/panel.ts +159 -0
- package/src/prometheus.ts +128 -0
- package/src/provider/MonitorProvider.ts +318 -0
- package/src/recorder/MonitorEventBridge.ts +157 -0
- package/src/recorder/ctxBuffers.ts +91 -0
- package/src/sources/live.ts +249 -0
- package/src/sources/realtime.ts +24 -0
- package/src/sources/system.ts +151 -0
- package/src/store/MonitorDb.ts +415 -0
- package/src/store/RingBuffer.ts +89 -0
- package/src/store/types.ts +580 -0
- package/src/support/time.ts +25 -0
- package/src/ui/MonitorLayout.tsx +42 -0
- package/src/ui/MonitorPage.tsx +3722 -0
- package/src/ui/charts.tsx +55 -0
- package/src/ui/icons.ts +32 -0
- package/src/ui/tones.ts +78 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog â @zerotal/monitor
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are documented here. The format is
|
|
4
|
+
based on [Keep a Changelog](https://keepachangelog.com/); this package
|
|
5
|
+
follows the Zerotal monorepo's unified versioning.
|
|
6
|
+
|
|
7
|
+
**Maturity: `experimental`**
|
|
8
|
+
|
|
9
|
+
## [Unreleased]
|
|
10
|
+
|
|
11
|
+
## [1.0.0] — 2026-08-05
|
|
12
|
+
|
|
13
|
+
_First public release._
|
|
14
|
+
|
|
15
|
+
### Notes
|
|
16
|
+
|
|
17
|
+
- Conforms to the Zerotal package conventions (provider in `src/provider/`, PascalCase config factory, `ZerotalError`-based errors, test coverage).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zerotal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# @zerotal/monitor
|
|
2
|
+
|
|
3
|
+
A production monitoring & queue dashboard for Zerotal — the "Super Panel". It
|
|
4
|
+
closes the _Prod monitoring dashboard_ gap in [`audit.md`](../../docs/audit.md): the
|
|
5
|
+
framework already collects the data (telemetry, health, queues), this package
|
|
6
|
+
gives it a UI.
|
|
7
|
+
|
|
8
|
+
It's a server-driven **Flow** panel: component classes render on the server,
|
|
9
|
+
interactions round-trip over the Flow WebSocket, no client store or API layer.
|
|
10
|
+
Eight tabs, faithful to `super-panel.html`:
|
|
11
|
+
|
|
12
|
+
**Overview · Requests · Exceptions · Queues · Mail · Database · Cache · System**
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
It's a workspace package. Add the provider after `FlowProvider`:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// bootstrap/providers.ts
|
|
20
|
+
import { FlowProvider } from "@zerotal/flow";
|
|
21
|
+
import { MonitorProvider } from "@zerotal/monitor";
|
|
22
|
+
|
|
23
|
+
export default [FlowProvider, MonitorProvider];
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Visit `/monitor`. That's it — every panel is populated out of the box.
|
|
27
|
+
|
|
28
|
+
## Configure
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// config/monitor.ts
|
|
32
|
+
import { MonitorConfig } from "@zerotal/monitor";
|
|
33
|
+
|
|
34
|
+
export default MonitorConfig({
|
|
35
|
+
path: "/monitor",
|
|
36
|
+
title: "Super Panel",
|
|
37
|
+
// Gate access. Default: allowed outside production only.
|
|
38
|
+
auth: (user) => user?.role === "admin",
|
|
39
|
+
record: true, // install the FrameworkEvents recorder
|
|
40
|
+
refreshMs: 3000, // live auto-refresh cadence
|
|
41
|
+
apdexTargetMs: 100, // Apdex satisfaction threshold (T)
|
|
42
|
+
slowQueryMs: 100, // "slow query" cut-off
|
|
43
|
+
slowRequestMs: 1000, // "slow request" cut-off (Slow Requests widget)
|
|
44
|
+
storage: "storage/monitor.sqlite", // bun:sqlite file (':memory:' for ephemeral)
|
|
45
|
+
retentionDays: 7, // history kept before pruning
|
|
46
|
+
retentionMode: "delete", // 'delete' or 'archive' past retention
|
|
47
|
+
metrics: true, // Prometheus endpoint
|
|
48
|
+
metricsPath: "/metrics",
|
|
49
|
+
alerts: true, // threshold alerts (see below)
|
|
50
|
+
alertThresholds: { errorRatePct: 5, p95Ms: 2000, queuePending: 500 },
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Persistence & retention
|
|
55
|
+
|
|
56
|
+
The panel persists every sample to **`bun:sqlite`**, so the **live / 1h / 24h / 7d**
|
|
57
|
+
ranges trace real history and **survive restarts** — not just whatever happened
|
|
58
|
+
since boot. Each event stream (requests, queries, exceptions, HTTP, cache, mail,
|
|
59
|
+
jobs) is a timestamped table; `snapshot(range)` reads the rows inside the selected
|
|
60
|
+
window, so every panel is range-consistent.
|
|
61
|
+
|
|
62
|
+
Data older than `retentionDays` is pruned hourly (and on boot). With
|
|
63
|
+
`retentionMode: 'archive'` the rows are moved to `*_archive` tables instead of being
|
|
64
|
+
deleted, for cold storage. The **System tab** shows live row counts and the oldest
|
|
65
|
+
record, plus two controls:
|
|
66
|
+
|
|
67
|
+
- **Clean up now** — prune past-retention data immediately.
|
|
68
|
+
- **Clear all** — wipe every recorded sample (with a confirm prompt).
|
|
69
|
+
|
|
70
|
+
Both are also available programmatically on the store: `store.prune()`,
|
|
71
|
+
`store.wipe()`, `store.storageInfo()`.
|
|
72
|
+
|
|
73
|
+
## How data flows
|
|
74
|
+
|
|
75
|
+
Everything is driven by core's `FrameworkEvents` bus — the same substrate the
|
|
76
|
+
logger and telemetry read. One subscriber (`installMonitorEventBridge`) feeds every
|
|
77
|
+
panel; there is **no sample data**. A quiet app shows honest zeros, never fabricated
|
|
78
|
+
traffic.
|
|
79
|
+
|
|
80
|
+
| Panel | Event mapped |
|
|
81
|
+
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
|
|
82
|
+
| Requests, latency p50/p95/p99, throughput, error rate, Apdex, slow routes | `RequestHandled` / `RequestFailed` |
|
|
83
|
+
| Per-request user, IP, memory; slow requests, top users, top-memory routes | `RequestHandled` (+ `ctx.user` / `ctx.ip()`) |
|
|
84
|
+
| Per-request SQL queries (correlated by HttpContext) + N+1 flag | `QueryExecuted` / `NPlusOneDetected` |
|
|
85
|
+
| Exceptions (grouped by type + route) | `RequestFailed` |
|
|
86
|
+
| Slow queries + DB stats | `QueryExecuted` |
|
|
87
|
+
| Cache hit-rate + hot keys | `CacheQueried` |
|
|
88
|
+
| Sent / queued / failed mail | `MessageSent` / `MessageFailed` |
|
|
89
|
+
| Outgoing HTTP — per-host calls, p95, error rate | `OutgoingRequestCompleted` |
|
|
90
|
+
| Job throughput + slowest jobs | `JobRan` |
|
|
91
|
+
| **Realtime** — active WS connections, actions/min, busiest/slowest components, and per-action **user / IP / SQL queries** (Flow re-runs middleware on each round-trip) | `WebSocketConnected` / `WebSocketDisconnected` / `FlowActionHandled` |
|
|
92
|
+
| **Security** — logins, logouts, denials, tokens | `LoginSucceeded` / `LoginFailed` / `LoggedOut` / `AuthorizationDenied` / `TokenIssued` |
|
|
93
|
+
| **Logs** — every entry, level-filterable, request-correlated | `LogManager.tap` (core logger) |
|
|
94
|
+
| **Database** — transactions, migrations, N+1 offenders | `TransactionCommitted` / `TransactionRolledBack` / `MigrationRan` / `NPlusOneDetected` |
|
|
95
|
+
| Cache evictions | `CacheEvicted` |
|
|
96
|
+
| Scheduled-task run history + check-ins | `TaskRan` / `TaskFailed` / `TaskSkipped` (+ `@zerotal/scheduler`) |
|
|
97
|
+
| Queues, workers, failed jobs (+ retry) | `@zerotal/queue` (if installed) |
|
|
98
|
+
| Health, CPU/memory gauges, uptime, deploy SHA | `Health` + process + git/env |
|
|
99
|
+
|
|
100
|
+
For anything outside the event bus (a deploy marker, a third-party call you make
|
|
101
|
+
without the `Http` client), the `Monitor` facade still works and takes precedence:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { Monitor } from "@zerotal/monitor";
|
|
105
|
+
|
|
106
|
+
Monitor.recordDeploy(gitSha);
|
|
107
|
+
Monitor.recordHttp({ host: "api.stripe.com", ms: 412, error: false });
|
|
108
|
+
|
|
109
|
+
// Attach metadata to the current request/action — shows on its trace:
|
|
110
|
+
Monitor.context({ tenant: tenant.id, plan: user.plan });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Every `Monitor.*` call is a no-op until the provider boots, so it's safe to call
|
|
114
|
+
unconditionally from anywhere.
|
|
115
|
+
|
|
116
|
+
## Prometheus
|
|
117
|
+
|
|
118
|
+
A Prometheus text-exposition endpoint is served at **`/metrics`** (on by default;
|
|
119
|
+
configure with `metrics` / `metricsPath`). It exports HTTP counters (cumulative
|
|
120
|
+
since boot), latency/Apdex/cache/queue/WS/exception gauges, system gauges, and
|
|
121
|
+
per-route latency — ready for Grafana and Alertmanager. Protect the path at the
|
|
122
|
+
network layer; it isn't behind the panel's auth so scrapers can reach it.
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
zerotal_http_requests_total 1284
|
|
126
|
+
zerotal_http_request_duration_ms_p95 318
|
|
127
|
+
zerotal_apdex 0.94
|
|
128
|
+
zerotal_ws_connections 12
|
|
129
|
+
zerotal_route_duration_ms_avg{method="GET",route="/dashboard"} 612
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Alerting
|
|
133
|
+
|
|
134
|
+
Threshold alerts (error-rate spike, slow p95, queue backlog, transaction
|
|
135
|
+
rollbacks) are evaluated every 15s, edge-triggered (each fires once when it
|
|
136
|
+
crosses, resets on recovery). A firing alert is logged, recorded (so it shows in
|
|
137
|
+
the panel), and dispatched to any handlers you register — wire those to
|
|
138
|
+
notifications:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { onAlert } from "@zerotal/monitor";
|
|
142
|
+
import { Notification } from "@zerotal/notifications";
|
|
143
|
+
|
|
144
|
+
onAlert((alert) => Notification.route("slack", SLACK_WEBHOOK).notify(new OpsAlert(alert)));
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Tune via config: `alerts: true`, `alertThresholds: { errorRatePct: 5, p95Ms: 2000, queuePending: 500 }`.
|
|
148
|
+
|
|
149
|
+
## Architecture
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
src/
|
|
153
|
+
MonitorStore.ts aggregation: reads windowed rows from SQLite → MonitorSnapshot
|
|
154
|
+
store/ MonitorDb (bun:sqlite persistence), percentile helpers, types
|
|
155
|
+
recorder/ MonitorEventBridge — the single FrameworkEvents → store subscriber
|
|
156
|
+
sources/ live adapters: queue, scheduler, health, system
|
|
157
|
+
facades/Monitor.ts app-facing record* API
|
|
158
|
+
ui/
|
|
159
|
+
MonitorLayout.tsx head (fonts, Tailwind), shell
|
|
160
|
+
MonitorPage.tsx the Flow component — 8 tabs, all interactivity
|
|
161
|
+
charts.tsx sparkline / area-chart SVG helpers
|
|
162
|
+
icons.ts, tones.ts nav icons + value→colour helpers
|
|
163
|
+
provider/ MonitorProvider — binds store, middleware, route
|
|
164
|
+
config.ts MonitorConfig()
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
> Tailwind is loaded from the Play CDN in `MonitorLayout` so the panel is styled
|
|
168
|
+
> with zero build setup — fine for an internal ops tool. Swap it for a compiled
|
|
169
|
+
> stylesheet in `MonitorLayout.head` for production hardening.
|
|
170
|
+
|
|
171
|
+
## Status
|
|
172
|
+
|
|
173
|
+
`maturity: experimental`. The live adapters degrade gracefully when an optional
|
|
174
|
+
peer (`queue`, `scheduler`, `telemetry`) isn't installed.
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zerotal/monitor",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"maturity": "experimental",
|
|
6
|
+
"private": false,
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./src/index.ts",
|
|
9
|
+
"types": "./src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./src/index.ts"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"CHANGELOG.md",
|
|
15
|
+
"src",
|
|
16
|
+
"!src/**/*.test.ts",
|
|
17
|
+
"!src/**/*.test.tsx",
|
|
18
|
+
"!src/**/*.spec.ts",
|
|
19
|
+
"!src/**/__fixtures__/**"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"bun": ">=1.3.14"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "bun test",
|
|
29
|
+
"typecheck": "tsc --noEmit"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@zerotal/core": "1.0.0",
|
|
33
|
+
"@zerotal/flow-ui": "1.0.0",
|
|
34
|
+
"@zerotal/flow": "1.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@zerotal/queue": "1.0.0",
|
|
38
|
+
"@zerotal/scheduler": "1.0.0",
|
|
39
|
+
"@zerotal/telemetry": "1.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@zerotal/queue": "^1.0.0",
|
|
43
|
+
"@zerotal/scheduler": "^1.0.0",
|
|
44
|
+
"@zerotal/telemetry": "^1.0.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"@zerotal/queue": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
50
|
+
"@zerotal/scheduler": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"@zerotal/telemetry": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"description": "Operational monitoring panel and metrics for Zerotal — requests, exceptions, queries, and alerts.",
|
|
58
|
+
"keywords": [
|
|
59
|
+
"zerotal",
|
|
60
|
+
"bun",
|
|
61
|
+
"typescript",
|
|
62
|
+
"framework",
|
|
63
|
+
"monitoring",
|
|
64
|
+
"metrics",
|
|
65
|
+
"prometheus"
|
|
66
|
+
],
|
|
67
|
+
"repository": {
|
|
68
|
+
"type": "git",
|
|
69
|
+
"url": "git+https://github.com/zerotaldev/zerotal.git",
|
|
70
|
+
"directory": "packages/monitor"
|
|
71
|
+
},
|
|
72
|
+
"homepage": "https://github.com/zerotaldev/zerotal/tree/main/packages/monitor#readme",
|
|
73
|
+
"bugs": "https://github.com/zerotaldev/zerotal/issues"
|
|
74
|
+
}
|