feat(08-01): add Prometheus /metrics endpoint with prom-client
- Install prom-client library for Prometheus metrics - Create src/lib/server/metrics.ts with default Node.js process metrics - Add /metrics endpoint that returns Prometheus-format text - Exposes CPU, memory, heap, event loop metrics Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
7
src/lib/server/metrics.ts
Normal file
7
src/lib/server/metrics.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Registry, collectDefaultMetrics } from 'prom-client';
|
||||
|
||||
// Create a custom registry for metrics
|
||||
export const registry = new Registry();
|
||||
|
||||
// Collect default Node.js process metrics (CPU, memory, event loop, etc.)
|
||||
collectDefaultMetrics({ register: registry });
|
||||
22
src/routes/metrics/+server.ts
Normal file
22
src/routes/metrics/+server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { registry } from '$lib/server/metrics';
|
||||
|
||||
export const GET: RequestHandler = async () => {
|
||||
try {
|
||||
const metrics = await registry.metrics();
|
||||
|
||||
return new Response(metrics, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': registry.contentType
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Metrics collection failed:', error);
|
||||
|
||||
return new Response('Metrics unavailable', {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'text/plain' }
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user