File size: 976 Bytes
9671dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 简易 Prometheus 指标(KISS/YAGNI):仅计数与活动数
const metrics = {
  requestsTotal: 0,
  requestsInFlight: 0,
  errorsTotal: 0,
};

export function onRequestStart() {
  metrics.requestsTotal += 1;
  metrics.requestsInFlight += 1;
}

export function onRequestEnd() {
  if (metrics.requestsInFlight > 0) metrics.requestsInFlight -= 1;
}

export function onError() {
  metrics.errorsTotal += 1;
}

export function renderPrometheus() {
  return [
    '# HELP cors_sp_requests_total Total HTTP requests received',
    '# TYPE cors_sp_requests_total counter',
    `cors_sp_requests_total ${metrics.requestsTotal}`,
    '# HELP cors_sp_requests_inflight In-flight HTTP requests',
    '# TYPE cors_sp_requests_inflight gauge',
    `cors_sp_requests_inflight ${metrics.requestsInFlight}`,
    '# HELP cors_sp_errors_total Total error responses',
    '# TYPE cors_sp_errors_total counter',
    `cors_sp_errors_total ${metrics.errorsTotal}`,
  ].join('\n') + '\n';
}