Skip to content

feat(webapp): 指标注册表与 /health /ready /metrics(#71) - #78

Merged
Antruly merged 3 commits into
masterfrom
feat/webapp-metrics
Nov 21, 2026
Merged

Antruly merged 3 commits into
masterfrom
feat/webapp-metrics

Conversation

@Antruly

@Antruly Antruly commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Closes #71.

内容

核心层 uvcpp_metrics_registry(src/uvcpp/,无外部依赖、无后台线程/定时器/单例):

  • Counter / Gauge / Histogram,线程安全;
  • render() 输出 Prometheus 文本格式(# HELP / # TYPE、_bucket{le} 累计桶、+Inf、_sum / _count,标签按名字排序、值按文本格式转义);
  • register_gauge(name, help, fn):渲染时取值,给连接池 in_use()、线程池深度这类本来在别处维护的数用(回调在锁外跑)。

webapp 层 uvcpp_web_metrics:

  • GET /health:恒 200(liveness 不查依赖 —— 数据库挂了该摘流量,不该重启进程);
  • GET /ready:set_ready_check() 返回 false 时 503,body 带原因;每个请求现问一次;
  • GET /metrics:Prometheus 文本(text/plain; version=0.0.4);
  • 可选请求中间件(默认开,注册为最外层):http_requests_total{method,status}、http_request_duration_ms{method}(默认 1..10000 ms 桶)、http_requests_in_flight;连接数用回调 Gauge uvcpp_web_app_connections;
  • 标签不放 URL 原串(高基数);路由维度由业务按路由模式自己计数;
  • 不 install() 时零成本。

用例

web_app_metrics_func.cpp:

  • 注册表:Counter/Gauge/Histogram 文本、标签排序与转义、le 累计桶、+Inf、_sum/_count、回调 Gauge、4 线程 × 5000 次并发自增无丢失(并发同时 render);
  • 端点(真 uvcpp_web_app + 真 TCP 客户端):/health 200;/ready 在 200 / 503(带原因)之间切换;/metrics 里能看到 200/404/503 计数、耗时直方图、直接渲染时在途 Gauge 为 0(HTTP 请求里为 1,即渲染它的这条请求自己)、连接数回调 Gauge。

验证

  • build-all 163/163、build-h3 / build-default 编过;
  • check_docs / check_doc_lines / check_doc_versions / check_ci_layout 全绿。

(本分支基于当前 master;runner 漂移那几条红会由 #77 合入后 rebase 掉。)

核心层新增 `uvcpp_metrics_registry`(无外部依赖、无线程/定时器/单例):
Counter / Gauge / Histogram,线程安全,`render()` 输出 Prometheus 文本
(0.0.4 格式:`# HELP` / `# TYPE` / `_bucket{le}` 累计桶 / `+Inf` / `_sum` /
`_count`,标签按名字排序、值转义),另有"渲染时取值"的回调 Gauge
(给连接池 `in_use()`、线程池深度这类本来在别处维护的数)。

webapp 层新增 `uvcpp_web_metrics`:

- `/health`:恒 200(liveness 不查依赖);
- `/ready`:`set_ready_check()` 返回 false 时 503 + 原因,每请求现问;
- `/metrics`:Prometheus 文本;
- 可选请求中间件(默认开,注册在最外层):`http_requests_total{method,status}`、
  `http_request_duration_ms{method}`(默认 1..10000 ms 桶)、
  `http_requests_in_flight`;连接数用回调 Gauge `uvcpp_web_app_connections`;
- 标签只放有限集合(方法/状态码),**不放 URL 原串**(高基数)——路由维度让业务
  自己按路由模式计数。

用例 `web_app_metrics_func`:注册表文本/转义/桶/并发更新 + 真端口上的
`/health`、`/ready` 的 200/503 切换、`/metrics` 里 200/404/503 计数与直方图、
在途 Gauge 归零、连接数回调 Gauge。文档 `doc/webapp-guide.md` §20。

本地 build-all 163/163、build-h3/default 编过;文档门禁全绿。
@Antruly
Antruly force-pushed the feat/webapp-metrics branch from 52042e1 to 22a6256 Compare November 21, 2026 14:59
@Antruly
Antruly merged commit c0d1162 into master Nov 21, 2026
47 of 49 checks passed
@Antruly
Antruly deleted the feat/webapp-metrics branch November 21, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(webapp): add standard health and metrics endpoints

1 participant