Skip to content

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:

ts
const app = Megh.createApplication();
app.useMonitor();
app.useControllers([OrderController]);
app.run(4000);

Start the app, then in another terminal:

bash
npx megh monit --open

The 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:

ts
app.useMonitor({ token: process.env.MEGH_MONITOR_TOKEN });
bash
MEGH_MONITOR_TOKEN=... npx megh monit --url https://api.example.com

The 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):

OptionDefaultDescription
path/_megh/monitorWhere the endpoints live
tokenMEGH_MONITOR_TOKENBearer token the dashboard must send
history300Seconds of metrics kept for the charts
logBuffer500Log lines kept for dashboards that connect later
logRequeststrueAdd one log line per request

megh monit:

FlagDefaultDescription
--url <url>MEGH_MONITOR_URL, then http://localhost:$PORT (4000)The app's address
-p, --port <port>4545Dashboard port
--host <host>127.0.0.1Dashboard interface
--token <token>MEGH_MONITOR_TOKENThe app's monitor token
--path <path>/_megh/monitorThe app's monitor path
-o, --openOpen the dashboard in the browser

Endpoints ​

The app serves two routes, which you can also read from your own tools:

RouteReturns
GET /_megh/monitorA snapshot: app info, services, routes, health, metric history and recent logs
GET /_megh/monitor/streamServer-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.

Released under the MIT License.