node-server-lens 0.1.0 → 0.1.2
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 +222 -184
- package/package.json +4 -4
- package/public/index.html +102 -47
package/README.md
CHANGED
|
@@ -1,138 +1,150 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- [Connecting servers](#connecting-servers)
|
|
20
|
-
- [Express / Connect / plain `http`](#express--connect--plain-http)
|
|
21
|
-
- [Next.js](#nextjs)
|
|
22
|
-
- [Fastify, Koa, NestJS, Hapi (any Node server)](#fastify-koa-nestjs-hapi-any-node-server)
|
|
23
|
-
- [Any URL, no code (uptime checks)](#any-url-no-code-uptime-checks)
|
|
24
|
-
- [Python, Go, PHP, Java, Ruby… (HTTP ingest API)](#python-go-php-java-ruby-http-ingest-api)
|
|
25
|
-
- [Custom loggers (pino, winston)](#custom-loggers-pino-winston)
|
|
26
|
-
- [Hosting the hub](#hosting-the-hub)
|
|
27
|
-
- [Docker](#docker)
|
|
28
|
-
- [VPS with PM2 + Nginx (HTTPS)](#vps-with-pm2--nginx-https)
|
|
29
|
-
- [Render / Railway / Fly.io](#render--railway--flyio)
|
|
30
|
-
- [Configuration reference](#configuration-reference)
|
|
31
|
-
- [HTTP API](#http-api)
|
|
32
|
-
- [Security](#security)
|
|
33
|
-
- [Limitations](#limitations)
|
|
34
|
-
- [Development](#development)
|
|
35
|
-
- [License](#license)
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="docs/images/banner.svg" alt="Server Lens: real-time monitoring for all your servers" width="100%">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/node-server-lens"><img src="https://img.shields.io/npm/v/node-server-lens?color=c2ec44&labelColor=1b1b1e&label=npm" alt="npm version"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/node-server-lens"><img src="https://img.shields.io/npm/dm/node-server-lens?color=d37ff6&labelColor=1b1b1e" alt="npm downloads"></a>
|
|
8
|
+
<img src="https://img.shields.io/node/v/node-server-lens?color=4fd16b&labelColor=1b1b1e" alt="node version">
|
|
9
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-8c8c93?labelColor=1b1b1e" alt="MIT license"></a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p align="center">
|
|
13
|
+
<b><a href="#-quick-start">Quick start</a></b> ·
|
|
14
|
+
<b><a href="#-set-up-with-an-ai-coding-agent">AI agent setup</a></b> ·
|
|
15
|
+
<b><a href="#-connect-your-servers">Connect servers</a></b> ·
|
|
16
|
+
<b><a href="#-host-the-hub">Hosting</a></b> ·
|
|
17
|
+
<b><a href="#-reference">Reference</a></b>
|
|
18
|
+
</p>
|
|
36
19
|
|
|
37
20
|
---
|
|
38
21
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
| Page | What you get |
|
|
42
|
-
| --- | --- |
|
|
43
|
-
| **Servers** (hub) | Every connected server in one grid: status (online / degraded / offline / down), requests in the last hour, average latency, error rate. Add or remove servers from the UI. |
|
|
44
|
-
| **Overview** | KPI cards (total requests, average response, 5xx error rate, uptime) with hour-over-hour change, the traffic chart and the status-code breakdown. |
|
|
45
|
-
| **Server Info** | Hostname, IPv4/IPv6 addresses (one-click copy), port, PID, runtime, OS, CPU, memory, environment, process and system uptime, load average. Live sparklines for process CPU, RSS memory, event-loop lag and system memory. |
|
|
46
|
-
| **Traffic** | Request volume chart with **1h · 24h · Week · Month · Year** filters. Hover any bar for count, average latency and errors. History is persisted by the hub. |
|
|
47
|
-
| **Status Codes** | Share of 2xx/3xx vs 4xx vs 5xx responses, plus recent requests. |
|
|
48
|
-
| **Live Logs** | Everything your app writes with `console.log/info/warn/error/debug`, streamed over WebSocket. Search with highlighting, level filters with counts, pause/resume, clear and download. |
|
|
49
|
-
| **API Endpoints** | Every route with calls, share of traffic, avg / p95 / max latency, errors and last call. Sorted by **most called** by default; also slowest (avg or p95), most errors, recently called, A–Z. IDs are grouped (`/users/42` → `/users/:id`). |
|
|
50
|
-
| **Requests** | The latest requests with method, path, status, duration and client IP. Search and "errors only" filter. |
|
|
22
|
+
**Server Lens** gives any backend a live monitoring dashboard: server status and IPs, streaming logs, request traffic with week / month / year history, per-endpoint API stats and uptime checks.
|
|
51
23
|
|
|
52
|
-
|
|
24
|
+
Use it two ways:
|
|
53
25
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
├──────────────┤ │ stores history in │
|
|
61
|
-
│ Next.js app │ ───────────────────────────────▶│ a JSON file │
|
|
62
|
-
│ + instrument │ │ │
|
|
63
|
-
├──────────────┤ │ │
|
|
64
|
-
│ Go / Python │ ── same JSON over HTTP ────────▶│ │
|
|
65
|
-
└──────────────┘ └──────────┬───────────┘
|
|
66
|
-
│ GET every 30s
|
|
67
|
-
┌─────────▼─────────┐
|
|
68
|
-
│ any URL (uptime) │
|
|
69
|
-
└───────────────────┘
|
|
70
|
-
```
|
|
26
|
+
| | **Embedded** | **Hub** |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| What | A dashboard inside your app at `/server-lens` | One dashboard for many servers, hosted anywhere |
|
|
29
|
+
| Setup | `app.use(serverLens())` | `npx node-server-lens` + point servers at it |
|
|
30
|
+
| History | Since the app started | Up to ~13 months, saved to disk |
|
|
31
|
+
| Best for | One app, local dev, quick checks | Teams, production, many services |
|
|
71
32
|
|
|
72
|
-
|
|
73
|
-
- **Hub**: a small standalone server that receives data from many agents, polls URLs, keeps up to ~13 months of hourly history on disk and serves the multi-server dashboard.
|
|
33
|
+
No database, no build step, one dependency (`ws`). Works with **Express, Next.js, Fastify, Koa, NestJS, Hapi, plain Node**, and **any other language** over HTTP.
|
|
74
34
|
|
|
75
|
-
## Quick start
|
|
35
|
+
## ⚡ Quick start
|
|
76
36
|
|
|
77
|
-
|
|
78
|
-
git clone https://github.com/Sathishram2000/Server-Lens.git
|
|
79
|
-
cd Server-Lens
|
|
80
|
-
npm install
|
|
81
|
-
npm run demo
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
Open http://localhost:4000. The demo starts a hub, two fake API servers that report to it, and a URL monitor, with a year of generated history so every chart has data. Each demo API also has its own embedded dashboard at http://localhost:3001/server-lens and http://localhost:3002/server-lens.
|
|
85
|
-
|
|
86
|
-
## Option A: embedded dashboard (one server)
|
|
37
|
+
**1. Install**
|
|
87
38
|
|
|
88
39
|
```bash
|
|
89
40
|
npm install node-server-lens
|
|
90
41
|
```
|
|
91
42
|
|
|
43
|
+
**2. Add one line before your routes**
|
|
44
|
+
|
|
92
45
|
```js
|
|
93
|
-
const express = require('express');
|
|
94
46
|
const serverLens = require('node-server-lens');
|
|
95
47
|
|
|
96
|
-
|
|
97
|
-
|
|
48
|
+
app.use(serverLens());
|
|
49
|
+
```
|
|
98
50
|
|
|
99
|
-
|
|
100
|
-
|
|
51
|
+
**3. Open** `http://localhost:3000/server-lens` (use your app's port).
|
|
52
|
+
|
|
53
|
+
That's it. Using Next.js, Fastify or Koa instead? See [Connect your servers](#-connect-your-servers).
|
|
54
|
+
|
|
55
|
+
> **Try it without your own project:** `git clone https://github.com/Sathishram2000/Node-Server-Lens.git && cd Node-Server-Lens && npm install && npm run demo`, then open http://localhost:4000. The demo runs a hub, two sample APIs and an uptime check, with a year of sample history.
|
|
56
|
+
|
|
57
|
+
## 🤖 Set up with an AI coding agent
|
|
58
|
+
|
|
59
|
+
Using Claude Code, Cursor, Copilot, Windsurf or another coding agent? Copy this prompt into it from your project folder. It detects your framework, installs the package and wires everything up.
|
|
60
|
+
|
|
61
|
+
```text
|
|
62
|
+
Add Server Lens monitoring (npm package "node-server-lens", docs: https://github.com/Sathishram2000/Node-Server-Lens) to this project.
|
|
63
|
+
|
|
64
|
+
1. Detect the backend framework and language from the project files.
|
|
65
|
+
2. If it runs on Node.js, install "node-server-lens" with the package manager that matches the lockfile (npm, pnpm, yarn or bun).
|
|
66
|
+
3. Wire it up, matching the project's module style (ESM import or CommonJS require) and TypeScript if used:
|
|
67
|
+
- Express, Connect, or NestJS on the Express adapter: in the file that creates the app, add
|
|
68
|
+
app.use(serverLens({ ...options })) BEFORE any routes.
|
|
69
|
+
- Next.js: create instrumentation.ts (in src/ if the project uses src/, otherwise the project root) with
|
|
70
|
+
export async function register() {
|
|
71
|
+
if (process.env.NEXT_RUNTIME === 'nodejs') {
|
|
72
|
+
const { instrument } = await import('node-server-lens');
|
|
73
|
+
instrument({ ...options });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
Then add 'node-server-lens' to serverExternalPackages in next.config
|
|
77
|
+
(Next.js 14: experimental.serverComponentsExternalPackages).
|
|
78
|
+
- Fastify, Koa, Hapi, plain http/https, or any other Node server: at the very top of the entry file,
|
|
79
|
+
before the server starts, call require('node-server-lens').instrument({ ...options })
|
|
80
|
+
(or the ESM import equivalent).
|
|
81
|
+
- Not Node.js (Python, Go, PHP, Ruby, Java, .NET...): do not install the npm package. Add a small
|
|
82
|
+
middleware that records method, route pattern, path, status, duration in ms, client IP and timestamp
|
|
83
|
+
for each request, batches them, and POSTs them every 5 seconds to `${SERVER_LENS_HUB_URL}/api/ingest`
|
|
84
|
+
with the header "Authorization: Bearer ${SERVER_LENS_KEY}", using the JSON format in the
|
|
85
|
+
"HTTP ingest API" section of the docs. Only run it when both variables are set, and never let a
|
|
86
|
+
failed POST break a request.
|
|
87
|
+
4. Options to pass (Node.js):
|
|
88
|
+
- name: this project's name
|
|
89
|
+
- auth: { username: process.env.SERVER_LENS_USER, password: process.env.SERVER_LENS_PASS },
|
|
90
|
+
only when both variables are set (otherwise leave auth out)
|
|
91
|
+
- hub: { url: process.env.SERVER_LENS_HUB_URL, key: process.env.SERVER_LENS_KEY },
|
|
92
|
+
only when SERVER_LENS_HUB_URL is set
|
|
93
|
+
5. Add SERVER_LENS_USER, SERVER_LENS_PASS, SERVER_LENS_HUB_URL and SERVER_LENS_KEY with empty values to
|
|
94
|
+
.env.example (create it if missing). Never commit real secrets.
|
|
95
|
+
6. Start the app, request /server-lens, and confirm it returns HTTP 200. Tell me the dashboard URL and
|
|
96
|
+
which files you changed.
|
|
101
97
|
```
|
|
102
98
|
|
|
103
|
-
|
|
99
|
+
<details>
|
|
100
|
+
<summary>What the prompt sets up</summary>
|
|
104
101
|
|
|
105
|
-
|
|
102
|
+
- **Node.js apps** get the embedded dashboard at `/server-lens`, plus hub reporting once you set `SERVER_LENS_HUB_URL` and `SERVER_LENS_KEY`.
|
|
103
|
+
- **Other languages** get a small reporter that sends data to your hub.
|
|
104
|
+
- Login is enabled as soon as you set `SERVER_LENS_USER` and `SERVER_LENS_PASS`.
|
|
105
|
+
- Nothing secret is written to the code; values come from environment variables.
|
|
106
106
|
|
|
107
|
-
|
|
107
|
+
Always review the changes before you commit them.
|
|
108
|
+
</details>
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
## 🖥 What you get
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
| Page | What it shows |
|
|
113
|
+
| --- | --- |
|
|
114
|
+
| **Server switcher** *(hub)* | Click the server name at the top right to see every connected server with its status (online, degraded, offline, down), requests in the last hour, latency and error rate. Pick one to switch; you stay on the page you're on. Add or remove servers from the same menu. |
|
|
115
|
+
| **Overview** | Total requests, average response time, 5xx error rate and uptime, each with hour-over-hour change, plus the traffic chart and status codes. |
|
|
116
|
+
| **Server Info** | Hostname, IPv4/IPv6 addresses with copy buttons, port, PID, runtime, OS, CPU, memory, environment, uptime and load average. Live charts for CPU, memory, event-loop lag and system memory. |
|
|
117
|
+
| **Traffic** | Request volume with **1h · 24h · Week · Month · Year** filters. Hover a bar for its count, average latency and errors. |
|
|
118
|
+
| **Status Codes** | Share of successful (2xx/3xx), client error (4xx) and server error (5xx) responses, plus recent requests. |
|
|
119
|
+
| **Live Logs** | Everything your app prints with `console.log / info / warn / error / debug`, streamed live. Search with highlighting, level filters, pause, clear and download. |
|
|
120
|
+
| **API Endpoints** | Every route with calls, share of traffic, avg / p95 / max latency, errors and last call. Sorted by **most called**; also by slowest, most errors, recently called or name. `/users/42` and `/users/43` are grouped as `/users/:id`. |
|
|
121
|
+
| **Requests** | The latest requests with method, path, status, duration and client IP. Search and an "errors only" filter. |
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
Also included: a `/health` endpoint for load balancers, optional login, uptime checks with down/recovery alerts in the logs, auto-reconnect, and a mobile layout.
|
|
116
124
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
125
|
+
## 🧭 How it works
|
|
126
|
+
|
|
127
|
+
<p align="center">
|
|
128
|
+
<img src="docs/images/architecture.svg" alt="Servers send data to the Server Lens hub; you watch it live in the dashboard" width="100%">
|
|
129
|
+
</p>
|
|
120
130
|
|
|
121
|
-
|
|
131
|
+
- The **agent** (this npm package) runs inside your Node app. It times every request, captures console output and samples CPU, memory and event-loop lag. It can show its own dashboard, report to a hub, or both.
|
|
132
|
+
- The **hub** is a small standalone server. It receives data from agents and other languages, checks URLs for uptime, saves history to a JSON file and serves the multi-server dashboard.
|
|
122
133
|
|
|
123
|
-
##
|
|
134
|
+
## 🔌 Connect your servers
|
|
124
135
|
|
|
125
|
-
|
|
136
|
+
Keep keys and passwords in environment variables, never in code. Every option is listed in the [reference](#agent-options).
|
|
126
137
|
|
|
127
|
-
|
|
138
|
+
<details open>
|
|
139
|
+
<summary><b>Express · Connect · NestJS (Express adapter) · plain Node http</b></summary>
|
|
128
140
|
|
|
129
141
|
```js
|
|
130
142
|
const serverLens = require('node-server-lens');
|
|
131
143
|
|
|
132
144
|
app.use(serverLens({
|
|
133
|
-
name: 'orders-api',
|
|
134
|
-
|
|
135
|
-
|
|
145
|
+
name: 'orders-api',
|
|
146
|
+
auth: { username: 'admin', password: process.env.SERVER_LENS_PASS }, // protect /server-lens
|
|
147
|
+
hub: { url: 'https://lens.example.com', key: process.env.SERVER_LENS_KEY }, // optional: report to a hub
|
|
136
148
|
}));
|
|
137
149
|
```
|
|
138
150
|
|
|
@@ -143,19 +155,20 @@ const http = require('http');
|
|
|
143
155
|
const lens = require('node-server-lens')();
|
|
144
156
|
|
|
145
157
|
http.createServer((req, res) => lens(req, res, () => {
|
|
146
|
-
// your handler
|
|
147
|
-
res.end('ok');
|
|
158
|
+
res.end('ok'); // your handler
|
|
148
159
|
})).listen(3000);
|
|
149
160
|
```
|
|
150
161
|
|
|
151
|
-
|
|
162
|
+
ESM works too: `import serverLens from 'node-server-lens'`.
|
|
163
|
+
</details>
|
|
152
164
|
|
|
153
|
-
|
|
165
|
+
<details>
|
|
166
|
+
<summary><b>Next.js</b></summary>
|
|
154
167
|
|
|
155
|
-
Works with `next start` and `next dev` (Node runtime, App or Pages router).
|
|
168
|
+
Works with `next start` and `next dev` (Node.js runtime, App or Pages router).
|
|
156
169
|
|
|
157
170
|
1. `npm install node-server-lens`
|
|
158
|
-
2. Create `instrumentation.ts`
|
|
171
|
+
2. Create `instrumentation.ts` in the project root (or in `src/` if you use it):
|
|
159
172
|
|
|
160
173
|
```ts
|
|
161
174
|
export async function register() {
|
|
@@ -163,8 +176,8 @@ export async function register() {
|
|
|
163
176
|
const { instrument } = await import('node-server-lens');
|
|
164
177
|
instrument({
|
|
165
178
|
name: 'web',
|
|
166
|
-
|
|
167
|
-
|
|
179
|
+
auth: { username: 'admin', password: process.env.SERVER_LENS_PASS! },
|
|
180
|
+
hub: { url: 'https://lens.example.com', key: process.env.SERVER_LENS_KEY! }, // optional
|
|
168
181
|
});
|
|
169
182
|
}
|
|
170
183
|
}
|
|
@@ -177,11 +190,13 @@ module.exports = { serverExternalPackages: ['node-server-lens'] };
|
|
|
177
190
|
// Next.js 14: module.exports = { experimental: { serverComponentsExternalPackages: ['node-server-lens'] } };
|
|
178
191
|
```
|
|
179
192
|
|
|
180
|
-
Pages and API routes are recorded
|
|
193
|
+
Pages and API routes are recorded and `/_next/*` assets are skipped. The dashboard is served at `/server-lens` on your Next.js app. Routes on the Edge runtime aren't captured, because they don't run in Node.
|
|
194
|
+
</details>
|
|
181
195
|
|
|
182
|
-
|
|
196
|
+
<details>
|
|
197
|
+
<summary><b>Fastify · Koa · Hapi · any other Node server</b></summary>
|
|
183
198
|
|
|
184
|
-
`instrument()` hooks into Node's `http`/`https` servers, so it works with any framework without touching your routes. Call it once, before
|
|
199
|
+
`instrument()` hooks into Node's own `http`/`https` servers, so it works with any framework without touching your routes. Call it once, before the server starts:
|
|
185
200
|
|
|
186
201
|
```js
|
|
187
202
|
require('node-server-lens').instrument({
|
|
@@ -189,18 +204,20 @@ require('node-server-lens').instrument({
|
|
|
189
204
|
hub: { url: 'https://lens.example.com', key: process.env.SERVER_LENS_KEY },
|
|
190
205
|
});
|
|
191
206
|
|
|
192
|
-
// ...then start Fastify / Koa /
|
|
207
|
+
// ...then start Fastify / Koa / Hapi as usual
|
|
193
208
|
```
|
|
209
|
+
</details>
|
|
194
210
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
### Any URL, no code (uptime checks)
|
|
211
|
+
<details>
|
|
212
|
+
<summary><b>Any website or API, no code (uptime checks)</b></summary>
|
|
198
213
|
|
|
199
|
-
In the hub,
|
|
214
|
+
In the hub, click the server name at the top right, then **+ Add server → Any URL** and enter a name and a health-check URL. The hub requests it every 30 seconds (change with `--check`) and tracks availability %, response time, status codes, and down/recovery events. Works for any stack, static sites and third-party APIs.
|
|
215
|
+
</details>
|
|
200
216
|
|
|
201
|
-
|
|
217
|
+
<details>
|
|
218
|
+
<summary><b>Python · Go · PHP · Ruby · Java (HTTP ingest API)</b></summary>
|
|
202
219
|
|
|
203
|
-
Send JSON to the hub
|
|
220
|
+
Send batches of JSON to the hub, for example every 5 seconds:
|
|
204
221
|
|
|
205
222
|
```bash
|
|
206
223
|
curl -X POST https://lens.example.com/api/ingest \
|
|
@@ -223,7 +240,7 @@ import os, time, threading, requests
|
|
|
223
240
|
from flask import Flask, request, g
|
|
224
241
|
|
|
225
242
|
app = Flask(__name__)
|
|
226
|
-
HUB, KEY, buf = "
|
|
243
|
+
HUB, KEY, buf = os.environ["SERVER_LENS_HUB_URL"] + "/api/ingest", os.environ["SERVER_LENS_KEY"], []
|
|
227
244
|
|
|
228
245
|
@app.before_request
|
|
229
246
|
def start(): g.t = time.time()
|
|
@@ -249,37 +266,47 @@ def flush():
|
|
|
249
266
|
threading.Thread(target=flush, daemon=True).start()
|
|
250
267
|
```
|
|
251
268
|
|
|
269
|
+
<a id="ingest-schema"></a>
|
|
270
|
+
|
|
252
271
|
| Field | Type | Notes |
|
|
253
272
|
| --- | --- | --- |
|
|
254
|
-
| `id` | string, required | `[A-Za-z0-9_.-]{1,64}`,
|
|
273
|
+
| `id` | string, required | `[A-Za-z0-9_.-]{1,64}`, the same on every report |
|
|
255
274
|
| `name` | string | display name |
|
|
256
275
|
| `info` | object | `hostname`, `runtime`, `platform`, `arch`, `cpuModel`, `env`, `port`, `pid`, `cpus`, `totalMem`, `startedAt`, `uptime`, `osUptime`, `ips[]` |
|
|
257
276
|
| `sample` | object | latest stats: `cpu` (%), `sysCpu` (%), `rss`, `heapUsed`, `freeMem` (bytes), `lag` (ms), `load` ([1, 5, 15]) |
|
|
258
|
-
| `requests` | array
|
|
259
|
-
| `logs` | array
|
|
277
|
+
| `requests` | array, ≤ 10,000 | `ts` (ms epoch), `method`, `route`, `path`, `status`, `ms`, `ip` |
|
|
278
|
+
| `logs` | array, ≤ 2,000 | `ts`, `level` (`debug` / `info` / `warn` / `error`), `msg` |
|
|
260
279
|
|
|
261
|
-
A server shows as **offline**
|
|
280
|
+
A server shows as **offline** when it hasn't reported for 20 seconds.
|
|
281
|
+
</details>
|
|
262
282
|
|
|
263
|
-
|
|
283
|
+
<details>
|
|
284
|
+
<summary><b>Other loggers (pino, winston…)</b></summary>
|
|
264
285
|
|
|
265
|
-
`console.*` is captured automatically. To forward another logger
|
|
286
|
+
`console.*` is captured automatically. To forward another logger, call `lens.log(level, ...args)`:
|
|
266
287
|
|
|
267
288
|
```js
|
|
268
289
|
const lens = serverLens();
|
|
269
290
|
app.use(lens);
|
|
270
291
|
lens.log('warn', 'disk usage at', 91, '%');
|
|
271
|
-
|
|
272
|
-
// pino: a tiny destination
|
|
273
|
-
const logger = require('pino')({}, { write: (line) => { const o = JSON.parse(line); lens.log(o.level >= 50 ? 'error' : o.level >= 40 ? 'warn' : 'info', o.msg); process.stdout.write(line); } });
|
|
274
292
|
```
|
|
293
|
+
</details>
|
|
275
294
|
|
|
276
|
-
##
|
|
295
|
+
## 🚀 Host the hub
|
|
277
296
|
|
|
278
|
-
The hub is
|
|
297
|
+
The hub is one Node process that saves history to one JSON file. Give it a **persistent disk**, an **ingest key** and a **login**, and put it behind **HTTPS**.
|
|
279
298
|
|
|
280
|
-
|
|
299
|
+
```bash
|
|
300
|
+
npx node-server-lens --port 4000 --key "$(openssl rand -hex 24)" --user admin --pass 'change-me'
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Click the server name at the top right, then **+ Add server**, at any time for copy-paste snippets with your hub URL filled in.
|
|
304
|
+
|
|
305
|
+
<details open>
|
|
306
|
+
<summary><b>Docker</b></summary>
|
|
281
307
|
|
|
282
308
|
```bash
|
|
309
|
+
git clone https://github.com/Sathishram2000/Node-Server-Lens.git && cd Node-Server-Lens
|
|
283
310
|
docker build -t server-lens .
|
|
284
311
|
docker run -d --name server-lens -p 4000:4000 \
|
|
285
312
|
-e SERVER_LENS_KEY=your-long-random-key \
|
|
@@ -304,11 +331,13 @@ services:
|
|
|
304
331
|
volumes:
|
|
305
332
|
lens-data:
|
|
306
333
|
```
|
|
334
|
+
</details>
|
|
307
335
|
|
|
308
|
-
|
|
336
|
+
<details>
|
|
337
|
+
<summary><b>VPS with PM2 + Nginx (HTTPS)</b></summary>
|
|
309
338
|
|
|
310
339
|
```bash
|
|
311
|
-
git clone https://github.com/Sathishram2000/Server-Lens.git && cd Server-Lens
|
|
340
|
+
git clone https://github.com/Sathishram2000/Node-Server-Lens.git && cd Node-Server-Lens
|
|
312
341
|
npm ci --omit=dev
|
|
313
342
|
npm install -g pm2
|
|
314
343
|
SERVER_LENS_KEY=... SERVER_LENS_USER=admin SERVER_LENS_PASS=... \
|
|
@@ -317,7 +346,7 @@ SERVER_LENS_KEY=... SERVER_LENS_USER=admin SERVER_LENS_PASS=... \
|
|
|
317
346
|
pm2 save && pm2 startup
|
|
318
347
|
```
|
|
319
348
|
|
|
320
|
-
Nginx
|
|
349
|
+
Nginx needs the WebSocket upgrade headers for live updates:
|
|
321
350
|
|
|
322
351
|
```nginx
|
|
323
352
|
server {
|
|
@@ -334,15 +363,17 @@ server {
|
|
|
334
363
|
}
|
|
335
364
|
```
|
|
336
365
|
|
|
337
|
-
Then `sudo certbot --nginx -d lens.example.com` for a free TLS certificate. Run only **one** hub process (
|
|
366
|
+
Then run `sudo certbot --nginx -d lens.example.com` for a free TLS certificate. Run only **one** hub process (not PM2 cluster mode), because history lives in that process.
|
|
367
|
+
</details>
|
|
338
368
|
|
|
339
|
-
|
|
369
|
+
<details>
|
|
370
|
+
<summary><b>Render · Railway · Fly.io · any container platform</b></summary>
|
|
340
371
|
|
|
341
372
|
Any platform that runs a Docker image or a Node process and supports WebSockets works:
|
|
342
373
|
|
|
343
374
|
| Setting | Value |
|
|
344
375
|
| --- | --- |
|
|
345
|
-
| Build | `npm ci --omit=dev
|
|
376
|
+
| Build | `npm ci --omit=dev`, or use the included `Dockerfile` |
|
|
346
377
|
| Start | `node bin/server-lens.js` |
|
|
347
378
|
| Env vars | `SERVER_LENS_KEY`, `SERVER_LENS_USER`, `SERVER_LENS_PASS`, `SERVER_LENS_DATA=/data/server-lens-data.json` |
|
|
348
379
|
| Disk / volume | mount at `/data` so history survives deploys |
|
|
@@ -350,90 +381,97 @@ Any platform that runs a Docker image or a Node process and supports WebSockets
|
|
|
350
381
|
|
|
351
382
|
`PORT` is read from the environment, which these platforms set for you. Without a persistent disk the hub still works, but history resets on each deploy.
|
|
352
383
|
|
|
353
|
-
Serverless platforms (Vercel functions, AWS Lambda)
|
|
384
|
+
Serverless platforms (Vercel functions, AWS Lambda) aren't a good fit for the **hub**, which needs a long-running process and a disk. Apps deployed there can still be watched with an uptime check or the ingest API.
|
|
385
|
+
</details>
|
|
354
386
|
|
|
355
|
-
##
|
|
387
|
+
## 📖 Reference
|
|
356
388
|
|
|
357
|
-
|
|
389
|
+
<a id="agent-options"></a>
|
|
390
|
+
|
|
391
|
+
<details>
|
|
392
|
+
<summary><b>Agent options</b>: <code>serverLens(options)</code> and <code>instrument(options)</code></summary>
|
|
358
393
|
|
|
359
394
|
| Option | Default | Description |
|
|
360
395
|
| --- | --- | --- |
|
|
361
396
|
| `path` | `'/server-lens'` | Where the embedded dashboard is served |
|
|
362
|
-
| `auth` | none | `{ username, password }` for
|
|
363
|
-
| `dashboard` | `true` | `false`
|
|
364
|
-
| `hub` | none | `{ url, key, id?, name?, interval? }
|
|
397
|
+
| `auth` | none | `{ username, password }` for a login, or `(req) => boolean` |
|
|
398
|
+
| `dashboard` | `true` | `false` turns off the embedded dashboard (hub reporting only) |
|
|
399
|
+
| `hub` | none | `{ url, key, id?, name?, interval? }` to report to a hub |
|
|
365
400
|
| `name` | hostname | Display name |
|
|
366
401
|
| `title` | `'Server Lens'` | Dashboard title |
|
|
367
402
|
| `captureConsole` | `true` | Capture `console.*` into live logs |
|
|
368
|
-
| `ignore` | `[]` | Path prefixes or RegExps not to record (`instrument` also
|
|
369
|
-
| `interval` | `2000` | Live update interval
|
|
403
|
+
| `ignore` | `[]` | Path prefixes or RegExps not to record (`instrument` also skips `/_next/` and `/favicon.ico`) |
|
|
404
|
+
| `interval` | `2000` | Live update interval in ms |
|
|
370
405
|
| `maxLogs` | `1000` | Log lines kept in memory |
|
|
371
|
-
| `maxRoutes` | `500` | Distinct endpoints tracked
|
|
406
|
+
| `maxRoutes` | `500` | Distinct endpoints tracked; extras are grouped as `METHOD *` |
|
|
372
407
|
| `maxRecent` | `100` | Recent requests kept |
|
|
373
408
|
|
|
374
|
-
|
|
409
|
+
The returned middleware also has `attach(server)`, `log(level, ...args)`, `snapshot()` and `close()`. TypeScript types are included.
|
|
410
|
+
</details>
|
|
375
411
|
|
|
376
|
-
|
|
412
|
+
<details>
|
|
413
|
+
<summary><b>Hub options</b>: <code>npx node-server-lens</code></summary>
|
|
377
414
|
|
|
378
|
-
| Flag |
|
|
415
|
+
| Flag | Environment variable | Default |
|
|
379
416
|
| --- | --- | --- |
|
|
380
417
|
| `--port` | `PORT` | `4000` |
|
|
381
418
|
| `--host` | `HOST` | `0.0.0.0` |
|
|
382
|
-
| `--key` | `SERVER_LENS_KEY` | random
|
|
419
|
+
| `--key` | `SERVER_LENS_KEY` | random for each run (printed at start) |
|
|
383
420
|
| `--user` / `--pass` | `SERVER_LENS_USER` / `SERVER_LENS_PASS` | none (open) |
|
|
384
421
|
| `--data` | `SERVER_LENS_DATA` | `./server-lens-data.json` |
|
|
385
|
-
| `--check` | `SERVER_LENS_CHECK` | `30`
|
|
422
|
+
| `--check` | `SERVER_LENS_CHECK` | `30` seconds between uptime checks |
|
|
386
423
|
| `--title` | `SERVER_LENS_TITLE` | `Server Lens` |
|
|
387
424
|
|
|
388
|
-
|
|
425
|
+
From code: `const { createHub } = require('node-server-lens'); createHub({ key, auth, dataFile }).listen(4000);`
|
|
426
|
+
</details>
|
|
389
427
|
|
|
390
|
-
|
|
428
|
+
<details>
|
|
429
|
+
<summary><b>HTTP API</b></summary>
|
|
391
430
|
|
|
392
|
-
Paths are relative to the dashboard
|
|
431
|
+
Paths are relative to the dashboard: `/server-lens` for the agent, `/` for the hub.
|
|
393
432
|
|
|
394
433
|
| Method & path | Auth | Description |
|
|
395
434
|
| --- | --- | --- |
|
|
396
435
|
| `GET /health` | none | `{ status, uptime, timestamp }` for load balancers |
|
|
397
|
-
| `GET /api/snapshot?server=<id>` |
|
|
398
|
-
| `GET /api/servers` |
|
|
399
|
-
| `GET /api/logs?server=<id>` |
|
|
400
|
-
| `POST /api/ingest` | `Bearer <key>` | Hub only: push data ([
|
|
401
|
-
| `POST /api/monitors` |
|
|
402
|
-
| `DELETE /api/servers/<id>` |
|
|
403
|
-
| `WS /ws?token=…` | page token |
|
|
436
|
+
| `GET /api/snapshot?server=<id>` | login | Everything the dashboard shows, as JSON |
|
|
437
|
+
| `GET /api/servers` | login | Server list with status and last-hour stats |
|
|
438
|
+
| `GET /api/logs?server=<id>` | login | Buffered log lines |
|
|
439
|
+
| `POST /api/ingest` | `Bearer <key>` | Hub only: push data ([format](#ingest-schema)) |
|
|
440
|
+
| `POST /api/monitors` | login | Hub only: `{ name, url }` adds an uptime check |
|
|
441
|
+
| `DELETE /api/servers/<id>` | login | Hub only: removes a server and its history |
|
|
442
|
+
| `WS /ws?token=…` | page token | The live stream the dashboard uses |
|
|
443
|
+
</details>
|
|
404
444
|
|
|
405
|
-
## Security
|
|
445
|
+
## 🔒 Security
|
|
406
446
|
|
|
407
|
-
- **
|
|
408
|
-
- **Use HTTPS** in front of the hub
|
|
409
|
-
- The ingest key is
|
|
410
|
-
- The live
|
|
411
|
-
-
|
|
447
|
+
- **Turn on the login** (`auth`, or `SERVER_LENS_USER` + `SERVER_LENS_PASS` on the hub). Logs and server details can contain sensitive data. The agent prints a warning when `NODE_ENV=production` and no login is set.
|
|
448
|
+
- **Use HTTPS** in front of the hub, because the login and the ingest key travel in request headers.
|
|
449
|
+
- The ingest key is checked in constant time. Payloads are size-limited (4 MB), validated and truncated, and the number of tracked routes is capped so URL scanners can't use up memory.
|
|
450
|
+
- The live connection needs a random per-process token that only appears in the logged-in page.
|
|
451
|
+
- Uptime checks make the hub request URLs that logged-in users enter. Only give dashboard access to people you trust.
|
|
412
452
|
|
|
413
|
-
## Limitations
|
|
453
|
+
## ⚠️ Limitations
|
|
414
454
|
|
|
415
|
-
-
|
|
416
|
-
-
|
|
417
|
-
- The hub stores data in
|
|
418
|
-
- p95 latency is
|
|
455
|
+
- The embedded dashboard keeps data in memory, so it starts fresh when the app restarts. Use the hub for long-term history.
|
|
456
|
+
- In Node `cluster` / PM2 cluster mode, each worker reports separately. Give each one its own `hub.id`, or use an uptime check.
|
|
457
|
+
- The hub stores data in one JSON file: fine for tens of servers, not built for thousands.
|
|
458
|
+
- p95 latency is calculated from the last 200 requests of each endpoint.
|
|
419
459
|
|
|
420
|
-
## Development
|
|
460
|
+
## 🛠 Development
|
|
421
461
|
|
|
422
462
|
```bash
|
|
423
463
|
npm install
|
|
424
|
-
npm run demo # hub + 2
|
|
425
|
-
npm test
|
|
464
|
+
npm run demo # hub + 2 sample APIs + uptime check at http://localhost:4000
|
|
465
|
+
npm test
|
|
426
466
|
```
|
|
427
467
|
|
|
428
|
-
Project layout:
|
|
429
|
-
|
|
430
468
|
```
|
|
431
|
-
bin/server-lens.js hub
|
|
469
|
+
bin/server-lens.js hub command line
|
|
432
470
|
src/index.js agent: middleware, instrument(), hub reporter
|
|
433
|
-
src/hub.js hub: ingest,
|
|
434
|
-
src/dashboard.js
|
|
435
|
-
src/metrics.js counters
|
|
436
|
-
src/source.js local
|
|
471
|
+
src/hub.js hub: ingest, uptime checks, saving history
|
|
472
|
+
src/dashboard.js dashboard page, JSON API and WebSocket
|
|
473
|
+
src/metrics.js counters and minute/hour history
|
|
474
|
+
src/source.js local, remote and URL servers
|
|
437
475
|
src/logs.js console capture and log buffer
|
|
438
476
|
public/index.html the whole dashboard UI (no build step)
|
|
439
477
|
example/ demo scripts
|
|
@@ -443,4 +481,4 @@ Issues and pull requests are welcome.
|
|
|
443
481
|
|
|
444
482
|
## License
|
|
445
483
|
|
|
446
|
-
[MIT](LICENSE)
|
|
484
|
+
[MIT](LICENSE) © Sathish Ramasamy
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-server-lens",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Self-hosted, real-time monitoring for all your servers: live logs over WebSocket, request traffic with week/month/year history, API stats, uptime checks. Express, Next.js, any Node server, or any language over HTTP.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
],
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
|
40
|
-
"url": "git+https://github.com/Sathishram2000/Server-Lens.git"
|
|
40
|
+
"url": "git+https://github.com/Sathishram2000/Node-Server-Lens.git"
|
|
41
41
|
},
|
|
42
42
|
"license": "MIT",
|
|
43
43
|
"engines": {
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"ws": "^8.18.0"
|
|
48
48
|
},
|
|
49
49
|
"author": "Sathish Ramasamy",
|
|
50
|
-
"homepage": "https://github.com/Sathishram2000/Server-Lens#readme",
|
|
50
|
+
"homepage": "https://github.com/Sathishram2000/Node-Server-Lens#readme",
|
|
51
51
|
"bugs": {
|
|
52
|
-
"url": "https://github.com/Sathishram2000/Server-Lens/issues"
|
|
52
|
+
"url": "https://github.com/Sathishram2000/Node-Server-Lens/issues"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/public/index.html
CHANGED
|
@@ -247,21 +247,29 @@ td.path{font-family:var(--mono);font-size:14px;max-width:380px;overflow:hidden;t
|
|
|
247
247
|
.slow{color:var(--amber)}
|
|
248
248
|
.tfoot{color:var(--muted);font-size:13px;margin-top:14px}
|
|
249
249
|
|
|
250
|
-
/* ----------
|
|
251
|
-
.
|
|
252
|
-
.
|
|
253
|
-
.
|
|
254
|
-
.
|
|
255
|
-
.
|
|
256
|
-
.
|
|
257
|
-
.
|
|
258
|
-
.
|
|
259
|
-
.
|
|
260
|
-
.
|
|
261
|
-
.
|
|
250
|
+
/* ---------- server switcher (hub mode) ---------- */
|
|
251
|
+
.switcher{position:relative;min-width:0}
|
|
252
|
+
button.who{cursor:default;text-align:left}
|
|
253
|
+
.hub button.who{cursor:pointer}
|
|
254
|
+
.hub button.who:hover,button.who[aria-expanded="true"]{border-color:rgba(255,255,255,.25);background:rgba(255,255,255,.05)}
|
|
255
|
+
.who .chev{width:14px;height:14px;color:var(--muted);flex:none;transition:transform .2s}
|
|
256
|
+
button.who[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
257
|
+
.menu{position:absolute;right:0;top:calc(100% + 10px);z-index:40;width:400px;max-width:calc(100vw - 32px);max-height:min(70vh,560px);overflow:auto;padding:8px;border-radius:22px;border:1px solid var(--line2);background:linear-gradient(180deg,#1c1c1f,#151517);box-shadow:0 30px 60px rgba(0,0,0,.6)}
|
|
258
|
+
.menu-h{display:flex;align-items:center;justify-content:space-between;padding:10px 14px 8px;color:var(--muted);font-size:13px;letter-spacing:.04em;text-transform:uppercase}
|
|
259
|
+
.srvrow{position:relative;display:grid;grid-template-columns:auto minmax(0,1fr) auto;gap:4px 12px;align-items:center;padding:12px 14px;border-radius:14px;cursor:pointer;border:1px solid transparent}
|
|
260
|
+
.srvrow:hover,.srvrow:focus-visible{background:rgba(255,255,255,.05)}
|
|
261
|
+
.srvrow.sel{background:rgba(211,127,246,.09);border-color:rgba(211,127,246,.35)}
|
|
262
|
+
.srvrow .nm{display:flex;align-items:center;gap:8px;font-size:15px;min-width:0}
|
|
263
|
+
.srvrow .nm span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
264
|
+
.srvrow .hs{grid-column:2;color:var(--muted);font-size:12.5px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
265
|
+
.srvrow .st{grid-column:3;grid-row:1/3;text-align:right;font-size:12px;color:var(--muted);line-height:1.5;padding:0;background:none;border:0}
|
|
266
|
+
.srvrow .st b{font-weight:400;color:var(--text);font-size:14px}
|
|
267
|
+
.kind{font-size:10.5px;letter-spacing:.05em;text-transform:uppercase;padding:2px 7px;border-radius:999px;background:rgba(255,255,255,.06);color:var(--muted);flex:none;font-style:normal}
|
|
268
|
+
.rm{position:absolute;top:6px;right:6px;color:var(--dim);padding:4px;border-radius:6px;line-height:0;opacity:0}
|
|
269
|
+
.srvrow:hover .rm,.rm:focus-visible{opacity:1}
|
|
262
270
|
.rm:hover{color:var(--red)}
|
|
263
|
-
.
|
|
264
|
-
.
|
|
271
|
+
.menu-add{display:block;width:100%;margin-top:6px;padding:12px;border-radius:14px;border:1px dashed var(--line2);color:var(--muted);font-size:15px}
|
|
272
|
+
.menu-add:hover{color:var(--text);border-color:rgba(255,255,255,.25)}
|
|
265
273
|
.dot.down,.dot.offline{background:var(--red)} .dot.pending{background:var(--dim)} .dot.degraded{background:var(--amber)}
|
|
266
274
|
dialog{margin:auto;width:min(680px,calc(100% - 32px));max-height:calc(100% - 32px);border-radius:24px;border:1px solid var(--line2);background:linear-gradient(180deg,#1b1b1e,#151517);color:var(--text);padding:30px 32px;box-shadow:0 40px 80px rgba(0,0,0,.6)}
|
|
267
275
|
dialog::backdrop{background:rgba(0,0,0,.6);backdrop-filter:blur(4px)}
|
|
@@ -274,6 +282,14 @@ dialog input.in{width:100%;padding:12px 16px;border-radius:14px;border:1px solid
|
|
|
274
282
|
dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
275
283
|
.btn.primary{background:linear-gradient(180deg,#e09cff,#b866e6);color:#160a1c;border:0;font-weight:500}
|
|
276
284
|
.dlg-foot{display:flex;justify-content:flex-end;gap:10px;margin-top:22px}
|
|
285
|
+
.onboard-only{display:none}
|
|
286
|
+
#addDlg.onboard .onboard-only{display:flex}
|
|
287
|
+
#addDlg.onboard [data-close]{display:none}
|
|
288
|
+
.welcome{align-items:center;gap:8px;color:var(--green);font-size:14px;margin-bottom:10px}
|
|
289
|
+
.onboard-foot{align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap;margin-top:24px;padding-top:20px;border-top:1px solid var(--line)}
|
|
290
|
+
.onboard-foot .muted{display:flex;align-items:center;gap:10px;font-size:14px}
|
|
291
|
+
.dot.wait{animation:blink 1.4s ease-in-out infinite}
|
|
292
|
+
@keyframes blink{50%{opacity:.25}}
|
|
277
293
|
.formerr{color:var(--red);font-size:14px;margin-top:10px;min-height:1em}
|
|
278
294
|
@media (max-width:1280px){.kpis{grid-template-columns:repeat(2,minmax(0,1fr))}.row2,.srv{grid-template-columns:minmax(0,1fr)}}
|
|
279
295
|
@media (max-width:900px){
|
|
@@ -316,7 +332,6 @@ dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
|
316
332
|
</div>
|
|
317
333
|
<nav class="nav">
|
|
318
334
|
<a href="#/dashboard" data-page="dashboard"><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><path d="M12 3a9 9 0 0 0-9 9v5a3 3 0 0 0 3 3h12a3 3 0 0 0 3-3v-5a9 9 0 0 0-9-9Zm-1 11h2v4h-2z"/></svg>Dashboard</a>
|
|
319
|
-
<a href="#/servers" data-page="servers" id="navServers" hidden><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><rect x="3" y="3" width="8" height="8" rx="2.5"/><rect x="13" y="3" width="8" height="8" rx="2.5"/><rect x="3" y="13" width="8" height="8" rx="2.5"/><rect x="13" y="13" width="8" height="8" rx="2.5"/></svg>Servers</a>
|
|
320
335
|
<a href="#/server" data-page="server"><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><rect x="3" y="4" width="18" height="7" rx="2.5"/><rect x="3" y="13" width="18" height="7" rx="2.5"/><circle cx="7.5" cy="7.5" r="1.2" fill="#0b0b0c"/><circle cx="7.5" cy="16.5" r="1.2" fill="#0b0b0c"/></svg>Server Info</a>
|
|
321
336
|
<button class="grp" id="grpToggle" aria-expanded="true"><span><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><rect x="3" y="3" width="18" height="18" rx="5"/><path d="M8 16v-5M12 16V8M16 16v-3" stroke="#0b0b0c" stroke-width="2" stroke-linecap="round"/></svg>Analytics</span><svg class="chev" style="transform:rotate(180deg)"><use href="#i-chev"/></svg></button>
|
|
322
337
|
<div class="sub" id="subNav">
|
|
@@ -329,12 +344,12 @@ dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
|
329
344
|
<a href="#/requests" data-page="requests"><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><path d="M21 3 3 10.5l7 2.5 2.5 7z"/></svg>Requests</a>
|
|
330
345
|
</nav>
|
|
331
346
|
<div class="side-bottom">
|
|
332
|
-
<nav class="nav"><a href="https://github.com/Sathishram2000/Server-Lens#readme" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><path d="M6 3h9l4 4v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2Z"/><path d="M8 12h8M8 16h5" stroke="#0b0b0c" stroke-width="1.8" stroke-linecap="round"/></svg>Documentation</a></nav>
|
|
347
|
+
<nav class="nav"><a href="https://github.com/Sathishram2000/Node-Server-Lens#readme" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" fill="currentColor" opacity=".8"><path d="M6 3h9l4 4v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2Z"/><path d="M8 12h8M8 16h5" stroke="#0b0b0c" stroke-width="1.8" stroke-linecap="round"/></svg>Documentation</a></nav>
|
|
333
348
|
<hr>
|
|
334
349
|
<div class="help">
|
|
335
350
|
<h4>Like Server Lens?</h4>
|
|
336
351
|
<p>It's free and open source. A star helps other developers find it.</p>
|
|
337
|
-
<a class="btn" href="https://github.com/Sathishram2000/Server-Lens" target="_blank" rel="noopener">Star on GitHub <svg><use href="#i-right"/></svg></a>
|
|
352
|
+
<a class="btn" href="https://github.com/Sathishram2000/Node-Server-Lens" target="_blank" rel="noopener">Star on GitHub <svg><use href="#i-right"/></svg></a>
|
|
338
353
|
</div>
|
|
339
354
|
</div>
|
|
340
355
|
</aside>
|
|
@@ -342,18 +357,21 @@ dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
|
342
357
|
<main id="top">
|
|
343
358
|
<header class="top">
|
|
344
359
|
<h1><button class="iconbtn" id="showSide" aria-label="Show sidebar"><svg width="16" height="16"><use href="#i-right"/></svg></button><span id="pageTitle">Overview</span></h1>
|
|
345
|
-
<
|
|
346
|
-
<
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
360
|
+
<div class="switcher" id="switcher">
|
|
361
|
+
<button class="who" id="who" aria-haspopup="true" aria-expanded="false" title="Switch server">
|
|
362
|
+
<span class="avatar" id="avatar">··</span>
|
|
363
|
+
<span class="host" id="host">Connecting…</span>
|
|
364
|
+
<span class="conn"><span class="dot" id="connDot"></span><span id="connTxt">Offline</span></span>
|
|
365
|
+
<svg class="chev" id="whoChev" hidden><use href="#i-chev"/></svg>
|
|
366
|
+
</button>
|
|
367
|
+
<div class="menu" id="srvMenu" hidden>
|
|
368
|
+
<div class="menu-h"><span>Servers</span><span class="badge flat" id="fleetCount"></span></div>
|
|
369
|
+
<div id="fleet" role="listbox" aria-label="Servers"></div>
|
|
370
|
+
<button class="menu-add" id="addBtn">+ Add server</button>
|
|
371
|
+
</div>
|
|
372
|
+
</div>
|
|
350
373
|
</header>
|
|
351
374
|
|
|
352
|
-
<section class="card pad grid" id="fleetCard" data-pages="servers">
|
|
353
|
-
<div class="card-h"><h2>Servers <span class="badge flat" id="fleetCount"></span></h2><button class="pillbtn" id="addBtn">+ Add server</button></div>
|
|
354
|
-
<div class="fleet" id="fleet"></div>
|
|
355
|
-
</section>
|
|
356
|
-
|
|
357
375
|
<section class="grid kpis" aria-label="Key metrics" data-pages="dashboard traffic">
|
|
358
376
|
<div class="card kpi"><div class="lbl">Total Requests</div><div class="val"><span class="num" id="kReq">–</span><span id="kReqB"></span></div></div>
|
|
359
377
|
<div class="card kpi"><div class="lbl">Avg Response</div><div class="val"><span class="num" id="kAvg">–</span><span id="kAvgB"></span></div></div>
|
|
@@ -449,12 +467,13 @@ dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
|
449
467
|
</div>
|
|
450
468
|
|
|
451
469
|
<dialog id="addDlg">
|
|
452
|
-
<
|
|
453
|
-
<
|
|
470
|
+
<div class="onboard-only welcome"><span class="dot ok"></span>Your hub is running</div>
|
|
471
|
+
<h3 id="dlgTitle">Connect a server</h3>
|
|
472
|
+
<p class="muted" id="dlgSub">Pick how this server should report to the hub.</p>
|
|
454
473
|
<div class="seg" id="addTabs"><button data-t="node" class="on">Node.js / Express</button><button data-t="next">Next.js</button><button data-t="url">Any URL</button><button data-t="api">Other languages</button></div>
|
|
455
474
|
<div data-p="node">
|
|
456
475
|
<div class="step">1 · Install the agent</div><pre>npm install node-server-lens</pre>
|
|
457
|
-
<div class="step">2 · Add it before your routes (Express, Fastify, Koa
|
|
476
|
+
<div class="step">2 · Add it before your routes (Express, NestJS or plain http). Fastify, Koa or Hapi: use <span class="mono">instrument()</span> as in the Next.js tab</div>
|
|
458
477
|
<pre id="snipNode"></pre>
|
|
459
478
|
</div>
|
|
460
479
|
<div data-p="next" hidden>
|
|
@@ -475,6 +494,10 @@ dialog input.in:focus{border-color:rgba(211,127,246,.6)}
|
|
|
475
494
|
<pre id="snipApi"></pre>
|
|
476
495
|
</div>
|
|
477
496
|
<div class="dlg-foot" id="dlgFoot"><button class="btn" data-close>Done</button></div>
|
|
497
|
+
<div class="onboard-only onboard-foot">
|
|
498
|
+
<span class="muted"><span class="dot pending wait"></span>Waiting for your first server… this closes by itself once it reports in.</span>
|
|
499
|
+
<a class="btn primary" href="https://github.com/Sathishram2000/Node-Server-Lens#-connect-your-servers" target="_blank" rel="noopener">Open setup guide <svg><use href="#i-right"/></svg></a>
|
|
500
|
+
</div>
|
|
478
501
|
</dialog>
|
|
479
502
|
|
|
480
503
|
<script>
|
|
@@ -788,17 +811,17 @@ $('grpToggle').addEventListener('click', () => {
|
|
|
788
811
|
$('grpToggle').setAttribute('aria-expanded', String(!open));
|
|
789
812
|
});
|
|
790
813
|
/* ---------- pages (hash router) ---------- */
|
|
791
|
-
const PAGES = { dashboard: 'Overview',
|
|
814
|
+
const PAGES = { dashboard: 'Overview', server: 'Server Info', traffic: 'Traffic', status: 'Status Codes', logs: 'Live Logs', apis: 'API Endpoints', requests: 'Requests' };
|
|
792
815
|
let page = 'dashboard';
|
|
793
816
|
function route() {
|
|
794
|
-
let p = location.hash.replace(/^#\/?/, '') ||
|
|
795
|
-
if (!PAGES[p]
|
|
817
|
+
let p = location.hash.replace(/^#\/?/, '') || 'dashboard';
|
|
818
|
+
if (!PAGES[p]) p = 'dashboard';
|
|
796
819
|
page = p;
|
|
797
820
|
document.querySelectorAll('[data-pages]').forEach((el) => (el.hidden = !el.dataset.pages.split(' ').includes(p)));
|
|
798
821
|
$('row2').classList.toggle('single', p !== 'dashboard');
|
|
799
822
|
$('pageTitle').textContent = PAGES[p];
|
|
800
823
|
document.title = `${PAGES[p]} · ${CFG.title}`;
|
|
801
|
-
document.body.className = 'page-' + p;
|
|
824
|
+
document.body.className = 'page-' + p + (CFG.hub ? ' hub' : '');
|
|
802
825
|
document.querySelectorAll('[data-page]').forEach((a) => a.classList.toggle('active', a.dataset.page === p));
|
|
803
826
|
$('app').classList.remove('drawer');
|
|
804
827
|
scrollTo(0, 0);
|
|
@@ -806,27 +829,40 @@ function route() {
|
|
|
806
829
|
if (p === 'logs') box.scrollTop = box.scrollHeight;
|
|
807
830
|
}
|
|
808
831
|
addEventListener('hashchange', route);
|
|
809
|
-
|
|
810
|
-
|
|
832
|
+
/* ---------- server switcher ---------- */
|
|
833
|
+
const menu = $('srvMenu');
|
|
834
|
+
function toggleMenu(open = menu.hidden) {
|
|
835
|
+
if (!CFG.hub) return;
|
|
836
|
+
menu.hidden = !open;
|
|
837
|
+
$('who').setAttribute('aria-expanded', String(open));
|
|
838
|
+
if (open) (menu.querySelector('.srvrow.sel') || menu.querySelector('.srvrow'))?.focus();
|
|
839
|
+
}
|
|
840
|
+
if (CFG.hub) { document.body.classList.add('hub'); $('whoChev').hidden = false; }
|
|
841
|
+
$('who').addEventListener('click', () => toggleMenu());
|
|
842
|
+
document.addEventListener('click', (e) => { if (!menu.hidden && !e.target.closest('#switcher')) toggleMenu(false); });
|
|
843
|
+
document.addEventListener('keydown', (e) => { if (e.key === 'Escape' && !menu.hidden) { toggleMenu(false); $('who').focus(); } });
|
|
811
844
|
|
|
812
845
|
/* ---------- data flow ---------- */
|
|
813
846
|
const snapSeen = (s) => (s.servers || []).find((x) => x.id === s.id)?.lastSeen;
|
|
814
847
|
function renderFleet(s) {
|
|
815
848
|
if (!CFG.hub || !s.servers) return;
|
|
816
849
|
$('fleetCount').textContent = s.servers.length;
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
<
|
|
850
|
+
const keep = menu.contains(document.activeElement) && document.activeElement.dataset.sid;
|
|
851
|
+
$('fleet').innerHTML = s.servers.map((x) => `<div class="srvrow${x.id === s.id ? ' sel' : ''}" role="option" aria-selected="${x.id === s.id}" tabindex="0" data-sid="${esc(x.id)}">
|
|
852
|
+
<span class="dot ${x.health === 'healthy' ? 'ok' : esc(x.health)}" title="${esc(x.health)}"></span>
|
|
853
|
+
<div class="nm"><span>${esc(x.name)}</span><em class="kind">${x.kind === 'url' ? 'URL' : x.id === 'hub' ? 'Hub' : 'Agent'}</em></div>
|
|
854
|
+
<div class="st"><b>${compact(x.count)}</b> req/h<br>${x.count ? ms(x.avgMs) : '–'} · <span class="${x.errors ? 'err' : ''}">${x.count ? ((x.errors / x.count) * 100).toFixed(1) + '%' : '–'}</span></div>
|
|
820
855
|
<div class="hs">${esc(x.url || x.host || '')}</div>
|
|
821
|
-
|
|
822
|
-
</div>`).join('')
|
|
856
|
+
${x.id !== 'hub' ? `<button class="rm" data-rm="${esc(x.id)}" aria-label="Remove ${esc(x.name)}"><svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M6 6l12 12M18 6 6 18"/></svg></button>` : ''}
|
|
857
|
+
</div>`).join('');
|
|
858
|
+
if (keep) menu.querySelector(`[data-sid="${CSS.escape(keep)}"]`)?.focus();
|
|
823
859
|
}
|
|
824
860
|
function select(id) {
|
|
825
861
|
if (!ws || ws.readyState !== 1) return;
|
|
826
|
-
|
|
862
|
+
toggleMenu(false);
|
|
863
|
+
if (snap && snap.id === id) return;
|
|
827
864
|
try { localStorage.setItem('sl-server', id); } catch {}
|
|
828
865
|
ws.send(JSON.stringify({ type: 'subscribe', id }));
|
|
829
|
-
location.hash = '#/dashboard';
|
|
830
866
|
}
|
|
831
867
|
$('fleet').addEventListener('click', async (e) => {
|
|
832
868
|
const rm = e.target.closest('[data-rm]');
|
|
@@ -837,10 +873,13 @@ $('fleet').addEventListener('click', async (e) => {
|
|
|
837
873
|
if (snap && snap.id === rm.dataset.rm) ws.send(JSON.stringify({ type: 'subscribe', id: 'hub' }));
|
|
838
874
|
return;
|
|
839
875
|
}
|
|
840
|
-
if (e.target.closest('[data-add]')) return openAdd();
|
|
841
876
|
const c = e.target.closest('[data-sid]'); if (c) select(c.dataset.sid);
|
|
842
877
|
});
|
|
843
|
-
$('fleet').addEventListener('keydown', (e) => {
|
|
878
|
+
$('fleet').addEventListener('keydown', (e) => {
|
|
879
|
+
const c = e.target.closest('[data-sid]'); if (!c) return;
|
|
880
|
+
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); select(c.dataset.sid); }
|
|
881
|
+
else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); (e.key === 'ArrowDown' ? c.nextElementSibling : c.previousElementSibling)?.focus(); }
|
|
882
|
+
});
|
|
844
883
|
|
|
845
884
|
/* ---------- connect dialog ---------- */
|
|
846
885
|
const hubUrl = location.origin + CFG.base;
|
|
@@ -870,7 +909,7 @@ $('snipApi').textContent = `curl -X POST ${hubUrl}/api/ingest \\
|
|
|
870
909
|
"requests": [{ "ts": 1733040000000, "method": "GET", "route": "/invoices/:id", "status": 200, "ms": 18 }],
|
|
871
910
|
"logs": [{ "ts": 1733040000000, "level": "info", "msg": "invoice sent" }]
|
|
872
911
|
}'`;
|
|
873
|
-
function openAdd() { $('addDlg').showModal(); }
|
|
912
|
+
function openAdd() { toggleMenu(false); $('addDlg').showModal(); }
|
|
874
913
|
$('addBtn').addEventListener('click', openAdd);
|
|
875
914
|
$('addTabs').addEventListener('click', (e) => {
|
|
876
915
|
const b = e.target.closest('button'); if (!b) return;
|
|
@@ -878,7 +917,22 @@ $('addTabs').addEventListener('click', (e) => {
|
|
|
878
917
|
$('addDlg').querySelectorAll('[data-p]').forEach((p) => (p.hidden = p.dataset.p !== b.dataset.t));
|
|
879
918
|
$('dlgFoot').hidden = b.dataset.t === 'url';
|
|
880
919
|
});
|
|
881
|
-
$('addDlg').addEventListener('click', (e) => { if (e.target.closest('[data-close]') || e.target === $('addDlg')) $('addDlg').close(); });
|
|
920
|
+
$('addDlg').addEventListener('click', (e) => { if (!onboarding && (e.target.closest('[data-close]') || e.target === $('addDlg'))) $('addDlg').close(); });
|
|
921
|
+
$('addDlg').addEventListener('cancel', (e) => { if (onboarding) e.preventDefault(); }); // Escape can't dismiss first-run setup
|
|
922
|
+
|
|
923
|
+
/* ---------- first run: nothing connected yet ---------- */
|
|
924
|
+
let onboarding = false;
|
|
925
|
+
function checkOnboarding(s) {
|
|
926
|
+
const none = CFG.hub && s.servers && s.servers.every((x) => x.id === 'hub');
|
|
927
|
+
if (none === onboarding) return;
|
|
928
|
+
onboarding = none;
|
|
929
|
+
const d = $('addDlg');
|
|
930
|
+
d.classList.toggle('onboard', none);
|
|
931
|
+
$('dlgTitle').textContent = none ? 'Connect your first server' : 'Connect a server';
|
|
932
|
+
$('dlgSub').textContent = none ? 'Nothing is reporting to this hub yet. Pick your stack below, or open the setup guide for every option.' : 'Pick how this server should report to the hub.';
|
|
933
|
+
if (none) { toggleMenu(false); if (!d.open) d.showModal(); }
|
|
934
|
+
else { if (d.open) d.close(); const first = s.servers.find((x) => x.id !== 'hub'); if (first && s.id === 'hub') select(first.id); } // jump to the server that just connected
|
|
935
|
+
}
|
|
882
936
|
$('urlForm').addEventListener('submit', async (e) => {
|
|
883
937
|
e.preventDefault();
|
|
884
938
|
$('mErr').textContent = '';
|
|
@@ -893,6 +947,7 @@ function apply(s) {
|
|
|
893
947
|
if (snap && snap.id !== s.id) { ipsKey = ''; hoverIdx = null; }
|
|
894
948
|
snap = s;
|
|
895
949
|
renderFleet(s);
|
|
950
|
+
checkOnboarding(s);
|
|
896
951
|
renderHeader(s); renderTraffic(); renderDonut(s); renderServer(s); renderApis(); renderRecent();
|
|
897
952
|
}
|
|
898
953
|
function setConn(on, txt) {
|