Skip to content

Real-time student monitoring

perfshop-monitoring is a standalone Node.js component that exposes a real-time HTML dashboard. It plays two distinct but complementary roles:

  1. Docker collector — it queries the Docker API, through the perfshop-docker-relay relay, to expose in Prometheus format the CPU / memory / network / I/O metrics of the four containers it watches
  2. HTML dashboard — it serves a web page that displays these metrics live along with those of the Spring Boot backend, in a deliberately simpler and more pedagogical version than Grafana

Sources

monitoring/src/server.js, monitoring/public/index.html, monitoring/package.json, monitoring/Dockerfile

Why a separate HTML dashboard?

Grafana is an excellent observability platform but its interface can overwhelm beginner students. perfshop-monitoring offers a simplified view:

  • A single screen, no navigation
  • Basic gauges and charts (no PromQL to write)
  • Focused on PerfShop container resources rather than the host
  • Automatic update every few seconds, without interaction

The full Grafana dashboard remains available for more advanced students and instructors — see Grafana.

Stack

Component Version Role
Node.js 20 Alpine Runtime
Express 4.18 HTTP server
node-fetch 3.3 HTTP calls to the backend
Docker API (relay) /run/docker-relay/docker.sock List and statistics of the watched containers, through perfshop-docker-relay

The container does not mount the Docker daemon socket: it mounts, read-only, the named volume docker-relay-monitoring, which holds the relay's socket. The relay grants it only /containers/json (list reduced to the four watched containers) and /containers/{name}/stats; any other call is refused. See Docker socket: the relay.

Configuration via environment variables

Variable Default Role
PORT 3001 HTTP listen port
APP_METRICS_URL http://perfshop-app:8080/actuator/prometheus Backend Prometheus endpoint
DOCKER_SOCKET /var/run/docker.sock Path of the Docker API socket — the compose files set it to /run/docker-relay/docker.sock, the relay's socket
HEAPDUMP_URL http://perfshop-app:9090/actuator/heapdump Heap dump endpoint (internal management port)
PERFSHOP_API_INTERNAL http://perfshop-app:8080 Internal backend of the proxies: /api/admin/login, the trainer's right before a restart of the backend (/api/admin/accounts/me), admin routes (see below)
PUBLIC_API_URL http://localhost:8080 Injected into window.__CONFIG__
PUBLIC_MONITORING_URL http://localhost:3001 Same
PUBLIC_GRAFANA_URL http://localhost:3002 Same
PUBLIC_CHAOS_URL http://localhost:3003 Same
PERFSHOP_LANG fr Language injected into window.__CONFIG__.LANG

The five PUBLIC_* variables are grouped in a PUBLIC_CONFIG object injected into each served HTML page via an inline <script> (route / and /config.js). This allows client JavaScript to call the right endpoints while respecting the deployment configuration.

Monitored containers

The CONTAINERS_TO_WATCH array is hard-coded in server.js:

const CONTAINERS_TO_WATCH = [
  'perfshop-frontend', 'perfshop-app', 'perfshop-db', 'perfshop-monitoring'
];

These are the four critical containers at the heart of PerfShop. The resolveContainerNames() function intelligently handles Docker Compose naming: it accepts project prefixes (perfshop-perfshop-app) and separators (- or _) to work on both NAS and Docker Desktop. It runs at startup then every 60 seconds to stay up to date in case of container restarts.

Exposed endpoints

/metrics — Prometheus format

Returns as plain text Prometheus-format metrics that Prometheus scrapes and stores. Two families of metrics are exposed:

Docker metrics (per monitored container):

Metric Type Labels
docker_container_cpu_percent gauge container
docker_container_mem_usage_bytes gauge container
docker_container_mem_limit_bytes gauge container
docker_container_mem_percent gauge container
docker_container_net_rx_bytes counter container
docker_container_net_tx_bytes counter container
docker_container_io_read_bytes counter container
docker_container_io_write_bytes counter container
docker_container_pids gauge container

The memory measurement excludes disk cache (memory.stats.cache or inactive_file) to display actually used memory and not just the Linux page cache.

Browser client metrics (Frontend Chaos) — only if received in the previous 10 seconds:

Metric Role
perfshop_client_fps Frames per second measured in the browser
perfshop_client_heap_used_mb Used JS heap in MB
perfshop_client_long_tasks_per_sec Number of long tasks (>50 ms) per second
perfshop_client_fetch_req_per_sec Fetch requests launched per second
perfshop_client_dom_node_count Number of DOM nodes
perfshop_client_cpu_worker_active 1 if a CPU Web Worker is running
perfshop_client_last_received_timestamp Unix timestamp of the last push

These metrics are pushed by chaos-agent.js from browsers via POST /api/chaos/client-metrics every 2 seconds. perfshop-monitoring acts as a bridge between browsers and Prometheus (Prometheus does not scrape browsers directly).

/api/docker/all

Returns the cached Docker stats as JSON for the HTML dashboard. A 5-second TTL cache avoids overloading the Docker daemon.

/api/docker/stats?container=NAME

Returns the stats of a specific container as JSON.

/api/prometheus-raw

Transparent proxy to the Spring Boot backend (/actuator/prometheus). The client dashboard uses this endpoint with its own parsePrometheus() parser rather than going directly to Spring Boot, for two reasons: to work around cross-origin issues and to benefit from the 5-second timeout that avoids blocking the dashboard if the backend is under chaos.

/api/chaos/client-metrics

  • POST: receives the browser metrics pushed by chaos-agent.js. Each field is validated by type (typeof x === 'number') to prevent injections.
  • GET: returns the last received metrics with a stale flag if more than 10 seconds have elapsed without a push.

/api/heapdump

The heap dump of the class (src/heapdump-proxy.js), relayed to the backend's /actuator/heapdump endpoint (management port 9090, reached over the Docker network). Open to the students without an account since version 1.0 (it was reserved to the trainer before).

  • One dump at a time: taking a dump stops the JVM while it is written. A click made during a production waits for that production; the file is then served to every click of the 2 minutes that follow its end, then deleted (one minute of grace for a download already started). The file goes through a temporary folder of the container, never through Node's memory: it can weigh several hundred MB during a leak.
  • GET /api/heapdump: the dump in one request, for a terminal — curl -o heapdump.hprof http://<platform address>:3001/api/heapdump. Headers: a name dated with the instant of the dump (the same for every click that gets this file), Content-Length, X-Heapdump-Taken-At, X-Heapdump-Reused (true: the file of an earlier click).
  • POST /api/heapdump: what the dashboard button and the widget (heapdump-widget.html) use. It prepares the dump and answers { name, url, size, takenAt, sharedUntil, reused }; the page then has the browser itself download GET /api/heapdump/<name>, without holding the file in memory.
  • Status codes: 503 (backend unreachable, or no answer from the actuator within 60 seconds), 502 (transfer broken, empty dump), the actuator's own status otherwise; 404 for a name that is not a served dump.

A heap dump holds the secrets of the JVM (sessions, tokens): on a platform reachable from the Internet, whoever reaches the monitoring can download one.

/api/admin/backend/restart — restart the backend (trainer)

The "🔄 Restart the backend" button of the monitoring admin, next to the heap dump (src/backend-restart.js, public/admin/js/admin-restart.js). Trainer only: an account with the "monitoring" right, or a superadmin, checked against the backend (GET /api/admin/accounts/me, X-Admin-Token token).

  • GET /api/admin/backend/restart (token): the announced cut — the last one measured on this platform, otherwise an estimate (10 s to stop, the start of the backend read from its application_ready_time_seconds metric, a few seconds to resume) — and the state of the last restart. The page calls it when it loads, then every minute, which confirms the right again.
  • POST /api/admin/backend/restart (token): asks the Docker relay to restart perfshop-app — the only container it lets anyone restart, and only the monitoring; answers 202 and a ticket; a second click during a restart answers 409. The monitoring then polls the health of the backend (/actuator/health) until UP: the cut is measured from the click to the return.
  • GET /api/admin/backend/restart/<ticket> (no token): the state of THAT restart — restarting, back with its duration, failed with its cause (relay: the relay did not restart the backend; not-back: it did not come back within 5 minutes). No token, because the restart wipes the trainers' tokens, which the backend keeps in memory.
  • When the backend no longer answers the check of the right — a frozen JVM, the very case the button is for —, a token whose right was confirmed less than 15 minutes ago is accepted; otherwise 503, and the restart is done on the platform's machine (docker restart perfshop-app).
  • Status codes: 401 (no token, or token refused), 403 (account without the "monitoring" right), 402 (no licence), 409, 503.

The confirmation announces the cut and its effects: the shop, the API and the chaos console are cut while the backend restarts; the chaos levels and the active scenario go back to 0; every trainer is signed out, the one who clicked too — the page then offers to sign in again.

/api/admin/login

Proxy to the backend's /api/admin/login. This alias works around cross-origin restrictions and allows the dashboard to log in as admin without the browser having to directly call perfshop-app:8080. The backend returns 402 if a license is missing, and the proxy propagates this code faithfully.

Admin backend routes (same-origin proxy)

The monitoring pages are served with the CSP shared by the PerfShop components (connect-src 'self'): the browser can only reach perfshop-monitoring. The server therefore relays to the internal backend (PERFSHOP_API_INTERNAL) the routes the admin page needs (src/admin-proxy.js) — and only those:

Method Route Trainer token
GET /api/admin/chaos/public/status yes
GET /api/chaos/frontend/state —
GET /api/chaos/public/{scripting,business,security,functional}/status —
GET /api/chaos/public/{scripting,business,security,functional}/logs —
DELETE /api/admin/chaos/scripting/logs yes
POST /api/admin/chaos/business/logs/clear yes
POST /api/admin/logout yes — signing out revokes the token on the backend before clearing the session

Rules: explicit allow-list (the lever write routes and the reset are not relayed); canonical path (no query string); only the X-Admin-Token header is relayed (no cookie, no body); backend status and body returned as-is, without Set-Cookie, with Cache-Control: no-store; backend unreachable → 502, no answer within 5 s → 504. The backend alone judges the token: without a trainer session, the levers' state answers 401 and the admin page shows "Trainer session missing or expired" with a "Sign in again" button — never "No active anomaly".

HTML dashboard

The dashboard is served on /. The public configuration (window.__CONFIG__: public URLs, language) is delivered by GET /config.js, loaded by every page — no image rebuild when a public URL changes. No page contains inline script, style or event handler (CSP script-src 'self', style-src 'self'): buttons carry a data-action attribute handled by a single listener (test/csp-no-inline.test.js).

The dashboard displays:

  • Four container cards (frontend, backend, db, monitoring) with CPU / memory / network
  • Real-time JVM charts (heap, threads, Tomcat / Hikari pools)
  • A Business Chaos panel (counters of detected anomalies)
  • A Security Chaos panel (attack counters)
  • A heap dump download zone
  • An admin login sidebar

The parsePrometheus() parser (client-side) extracts metrics like jvm_memory_used_bytes{area="heap"}, process_cpu_usage, jvm_threads_live_threads, tomcat_threads_busy_threads, hikaricp_connections_active etc. to feed the charts.

JavaScript architecture — duplication eliminated

The dashboard is split into three clear layers:

File Role Lines
public/js/monitoring-charts.js Pure utilities: mkCfg, mkDS, addPt, parsePrometheus, histogramQuantile, diffBuckets, formatBytes, switchTab ~140
public/js/monitoring-core.js Shared student/admin logic: construction of common Chart.js instances, fetchers (fetchBackendMetrics, fetchAppCpu, fetchClientMetrics, fetchDockerStats, fetchGeneral), triggerHeapDump, bindGlobalPause, openGrafana. Exposed via window.PerfShopMonitoring. ~530
public/js/monitoring-app.js Student wrapper (~60 lines) — wires monitoring-core.js onto index.html elements and defines polling. Only contains the student-specific backend banner logic. ~60
public/admin/js/admin-app.js Admin wrapper (~730 lines) — wires monitoring-core.js plus admin-only code: beChart13 (chaos intensities, 9 levers), Backend/Frontend Chaos panels, Scripting, Business Chaos, Security Chaos, Functional Chaos, token detail modal, logout, ACTIONS table of the buttons (data-action). ~730
public/admin/js/admin-chaos-state.js Reading of the levers' state (pure, tested functions): "read", "trainer session required" (401) or "unavailable". Exposed via window.PerfShopAdminChaos. ~70
public/admin/js/admin-restart.js "Restart the backend" button (tested): confirmation that announces the cut, follow-up of the restart by its ticket, measured duration, back to the sign-in. Exposed via window.PerfShopAdminRestart. —

Before this refactor, monitoring-app.js and admin-app.js shared ~278 lines verbatim (50% of the student file was strictly identical to admin functions). A Chart.js label bug or a silent catch(e){} had to be fixed twice. monitoring-core.js unifies this foundation: single source of truth, single fix to apply.

monitoring-core.js API contract:

const M = window.PerfShopMonitoring;

const isPaused = M.bindGlobalPause();      // #global-pause-btn → boolean getter
const charts   = M.createSharedCharts({ beChart13: /* admin-only optional */ });
const state    = M.createSharedState();    // EMA counters, previous buckets, etc.

M.fetchBackendMetrics(charts, state, isPaused);   // returns { text, tomcatBusy, dbPending }
M.fetchAppCpu(charts, isPaused, { verboseMissing: true });
M.fetchClientMetrics(charts, isPaused, { updateBanner: true });  // student only
M.fetchDockerStats(charts, state, isPaused);
M.fetchGeneral(charts, isPaused);
M.triggerHeapDump();
M.openGrafana(selectElement, GRAFANA_URL);

Option flags (verboseMissing, updateBanner) carry the small behavioral differences between student and admin without multiplying functions. Script load order is strict:

<script src="/config.js"></script>                              <!-- window.__CONFIG__ -->
<script src="/vendor/chart-4.4.7.umd.min.js"></script>          <!-- Chart.js global (vendored) -->
<script src="/js/i18n.js"></script>                             <!-- _t, loadI18n, I18N -->
<script src="/js/monitoring-charts.js"></script>                <!-- mkDS, mkCfg, parsePrometheus -->
<script src="/js/monitoring-core.js"></script>                  <!-- window.PerfShopMonitoring -->
<script src="/js/monitoring-app.js"></script>                   <!-- student -->
<!-- admin: /admin/js/admin-chaos-state.js, /admin/js/admin-restart.js then /admin/js/admin-app.js -->

Internationalization

The monitoring has its own public/i18n/ folder with fr.json and en.json. The language is propagated via window.__CONFIG__.LANG (value of PERFSHOP_LANG). Loading the dictionary follows the same pattern as chaos-admin: async loader, _t(), declarative applyI18n().

public/js/i18n.js exposes five declarative HTML attributes:

Attribute Effect
data-i18n="key" el.textContent = _t('key') (or document.title if the element is <title>)
data-i18n-html="key" el.innerHTML = _t('key') — trusted static content, no user input
data-i18n-placeholder="key" el.placeholder = _t('key')
data-i18n-title="key" el.title = _t('key') (tooltip)
data-i18n-label="key" el.label = _t('key') — for <optgroup>

Adding a new language is as simple as dropping a public/i18n/<code>.json file containing the same keys as fr.json, then setting PERFSHOP_LANG=<code> in the container environment. No code change required. The loader applies a detection cascade: window.__CONFIG__.LANG → navigator.language → fr. If the requested file is missing, a fallback to fr.json is applied automatically with a console warning.

See Ancillary tools.

Differences with Grafana

Criterion perfshop-monitoring Grafana
Target audience Beginner students, quick demonstration Advanced students, instructors, QA
Interaction Read-only, no config Customizable dashboards, PromQL
Coverage PerfShop containers + JVM + Chaos The entire observability stack
Data source Docker API (through the relay) + /actuator/prometheus direct Prometheus (history) + Loki + Tempo
Persistence None (real-time only) Prometheus storage (15 days by default)

One does not replace the other: the HTML dashboard is an entry point to observability, Grafana is the analysis tool for incidents.