feat(web): add client-side telemetry with Web Vitals and dev metrics endpoint

This commit is contained in:
MythEclipse
2026-06-07 18:13:11 +07:00
parent eefb16ad1d
commit 5a47b0c658
8 changed files with 122 additions and 7 deletions
+42
View File
@@ -0,0 +1,42 @@
import type { Plugin } from 'vite';
/**
* Vite plugin that exposes a /metrics endpoint during development.
*
* The endpoint returns Prometheustext metrics collected in
* src/lib/telemetry.ts.
*/
export function metricsPlugin(): Plugin {
let telemetryModule: typeof import('./src/lib/telemetry') | null = null;
return {
name: 'zeavis-metrics',
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
// Only handle GET /metrics
if (req.method !== 'GET' || !req.url?.startsWith('/metrics')) {
return next();
}
// Lazyload the telemetry module (ensures the app is bootstrapped first)
if (!telemetryModule) {
try {
telemetryModule = await server.ssrLoadModule('./src/lib/telemetry.ts') as typeof import('./src/lib/telemetry');
} catch {
// If the module isn't ready yet, return an empty body
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end('# telemetry module not yet loaded\n');
return;
}
}
const body = telemetryModule.collectMetrics();
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end(body);
});
},
};
}