描述如何把 Dubbo 的 Prometheus 指标实时回写到 Gatling 报告

解读

在国内高并发压测场景中,面试官真正想验证的是:

  1. 你是否能把 Dubbo 暴露的 Prometheus 指标(QPS、RT、线程池、熔断计数等)与 Gatling 的实时报告体系 打通,实现“代码级+系统级”双维度压测闭环;
  2. 是否熟悉 Prometheus Pull → Pushgateway → Gatling 自定义 Reporter 这一国内最常用的“零侵入”链路,避免修改业务代码;
  3. 是否能在 5 分钟级延迟内 完成指标回写,满足阿里、字节、美团等一线厂“压测即监控”的规范。

知识点

  1. Dubbo 3.x 内置 Prometheus 指标:dubbo_metrics_invocation_total、dubbo_metrics_rt_millis、dubbo_thread_pool_active 等,通过 /actuator/prometheus 端点暴露。
  2. Pushgateway 模式:压测机与业务网隔离时,Prometheus 无法直接拉取,需由压测脚本把指标推送到 Pushgateway,再被 Prometheus 拉走。
  3. Gatling 3.7+ 自定义 Reporter 接口:io.gatling.core.stats.writer.DataWriter 与 io.gatling.commons.stats.assertion.Assertion 支持在 simulation.log 之外追加任意数据源。
  4. Scala/Java 双语言实现:国内团队以 Java 为主,需用 Java 实现 DataWriter,降低维护门槛。
  5. 时间窗口对齐:Gatling 的 scenario 时间戳与 Prometheus 的 timestamp 必须 秒级对齐,否则会出现“指标漂移”被面试官直接判负。
  6. 指标聚合粒度:国内规范要求 30s 滑动窗口 内的 P99、P999 与 Dubbo 的 Histogram 分位点一一对应,避免“压测报告好看、线上翻车”。

答案

步骤一:在 Dubbo 端开启 Prometheus 并统一标签

management.metrics.export.prometheus.enabled=true
management.endpoints.web.exposure.include=prometheus
dubbo.metrics.prometheus.exporter.enabled=true

为所有指标补上 固定的 application="gatling-demo" 标签,方便后续查询。

步骤二:压测机本地启动 Pushgateway

docker run -d --name=pgw -p 9091:9091 prom/pushgateway

步骤三:Gatling 脚本内嵌 Java 自定义 DataWriter

  1. 引入依赖
<dependency>
  <groupId>io.prometheus</groupId>
  <artifactId>simpleclient_pushgateway</artifactId>
  <version>0.16.0</version>
</dependency>
  1. 实现 DubboPrometheusPushWriter
  • 在 onInit 里创建 PushGateway pgw = new PushGateway("localhost:9091");
  • 每 30s 的 onFlush 中,通过 PrometheusMetricsProvider 拉取 Dubbo 指标:
CollectorRegistry registry = new CollectorRegistry();
HttpURLConnection conn = (HttpURLConnection) new URL("http://dubbo-pod:8080/actuator/prometheus").openConnection();
TextFormat.read004(conn.getInputStream(), registry);
pgw.pushAdd(registry, "gatling_dubbo");
  • 把 QPS、RT、线程池队列长度 三个核心指标转成 Gatling 的 Series[Double],通过 DataWriterMessage 回写到 globalStatsEngine。

步骤四:Gatling 报告渲染
在 gatling.conf 中开启 charting.indicators = [ "dubbo_qps", "dubbo_rt_p99", "dubbo_thread_active" ],Gatling 会在 js/stats.json 中追加自定义指标,HTML 报告自动出现 “Dubbo 实时监控” 折线图,实现 零侵入、秒级刷新。

步骤五:CI 门禁
在 GitLab CI 中增加断言:

assertDetails("dubbo_rt_p99").lessThan(120)

若 Dubbo 侧 P99 超过 120ms,即使 Gatling 自身 RT 通过,也判定压测失败,符合国内“双保险”要求。

拓展思考

  1. 多协议场景:如果 Dubbo 同时暴露 Triple、Dubbo2、Grpc 三种协议,需在 Pushgateway 的 job 名中加入 protocol 标签,避免指标覆盖。
  2. Kubernetes 环境:使用 Prometheus Operator 的 PodMonitor 替代 Pushgateway,通过 Gatling Operator 的 Sidecar 模式直接把指标写入 Thanos Receive,实现 10w+ QPS 压测场景 下 5s 级延迟。
  3. 成本优化:在 nightly 压测中,可把 Dubbo 指标采样率降到 0.1%,通过 Prometheus 的 recording rule 预聚合,减少 Gatling 报告体积 80%,节省 50% 对象存储费用。