Monitoring
megh monit opens a live dashboard for a running app. It shows:
- Current values: requests per second, p95 latency, 5xx rate, heap, CPU and event loop lag
- Charts of the last 5 minutes: throughput, latency, memory, CPU and event loop lag, each with a hover tooltip and a table view
- Services the app turned on (mail, cache, storage, messaging, queues, scheduler, Socket.IO, sessions, tracing and more)
- Health checks, refreshed every 5 seconds
- Routes with request counts, 5xx counts and average time
- Logs, streamed live: every line the Megh logger writes, plus one line per request. Filter by level or text, pause and clear.
Setup
Turn the monitor on in the app:
const app = Megh.createApplication();
app.useMonitor();
app.useControllers([OrderController]);
app.run(4000);Start the app, then in another terminal:
npx megh monit --openThe dashboard runs at http://127.0.0.1:4545 and watches http://localhost:4000 (or $PORT). Point it elsewhere with --url.
Watching a remote app
Without a token, the app answers the monitor only from its own machine, and useMonitor() refuses to start in production. Set the same token on both sides to watch an app elsewhere:
app.useMonitor({ token: process.env.MEGH_MONITOR_TOKEN });MEGH_MONITOR_TOKEN=... npx megh monit --url https://api.example.comThe dashboard itself listens on 127.0.0.1 and sends the token to the app for you, so the token never reaches the browser.
WARNING
The monitor shows routes, log lines and service settings. Use a long random token, and serve the app over HTTPS when you watch it remotely.
Options
app.useMonitor(options):
| Option | Default | Description |
|---|---|---|
path | /_megh/monitor | Where the endpoints live |
token | MEGH_MONITOR_TOKEN | Bearer token the dashboard must send |
history | 300 | Seconds of metrics kept for the charts |
logBuffer | 500 | Log lines kept for dashboards that connect later |
logRequests | true | Add one log line per request |
megh monit:
| Flag | Default | Description |
|---|---|---|
--url <url> | MEGH_MONITOR_URL, then http://localhost:$PORT (4000) | The app's address |
-p, --port <port> | 4545 | Dashboard port |
--host <host> | 127.0.0.1 | Dashboard interface |
--token <token> | MEGH_MONITOR_TOKEN | The app's monitor token |
--path <path> | /_megh/monitor | The app's monitor path |
-o, --open | Open the dashboard in the browser |
Endpoints
The app serves two routes, which you can also read from your own tools:
| Route | Returns |
|---|---|
GET /_megh/monitor | A snapshot: app info, services, routes, health, metric history and recent logs |
GET /_megh/monitor/stream | Server-Sent Events: metrics every second and log for each line |
Metrics and logs are kept in memory in each process, so with several replicas, each one reports only itself.