-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprometheus.ts
More file actions
81 lines (69 loc) · 2.1 KB
/
Copy pathprometheus.ts
File metadata and controls
81 lines (69 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { FastifyInstance } from 'fastify';
import {
Counter,
Gauge,
Histogram,
Registry,
collectDefaultMetrics,
} from 'prom-client';
const register = new Registry();
// Collect default Node.js metrics
collectDefaultMetrics({ register });
// Custom metrics
export const httpRequestsTotal = new Counter({
name: 'http_requests_total',
help: 'Total HTTP requests',
labelNames: ['method', 'path', 'status'],
registers: [register],
});
export const httpRequestDuration = new Histogram({
name: 'http_request_duration_seconds',
help: 'HTTP request duration in seconds',
labelNames: ['method', 'path', 'status'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 2, 5],
registers: [register],
});
export const documentsProcessed = new Counter({
name: 'documents_processed_total',
help: 'Total documents processed',
labelNames: ['status', 'format', 'lane'],
registers: [register],
});
export const queueSize = new Gauge({
name: 'processing_queue_size',
help: 'Current size of processing queue',
labelNames: ['status'],
registers: [register],
});
export const embeddingDuration = new Histogram({
name: 'embedding_generation_duration_seconds',
help: 'Embedding generation duration',
buckets: [0.1, 0.5, 1, 2, 5, 10],
registers: [register],
});
// Metrics route
export async function metricsRoute(fastify: FastifyInstance): Promise<void> {
fastify.get('/metrics', async (request, reply) => {
reply.header('Content-Type', register.contentType);
return register.metrics();
});
}
// Request metrics hook
export function metricsHook(fastify: FastifyInstance): void {
fastify.addHook('onRequest', async (request) => {
(request as any).startTime = Date.now();
});
fastify.addHook('onResponse', async (request, reply) => {
const duration = (Date.now() - (request as any).startTime) / 1000;
const path = request.routeOptions?.url || request.url;
httpRequestsTotal.inc({
method: request.method,
path,
status: reply.statusCode,
});
httpRequestDuration.observe(
{ method: request.method, path, status: reply.statusCode },
duration
);
});
}