5.2. 使用 Eclipse Vert.x 使用 Prometheus 公开应用程序指标
Prometheus 连接到受监控的应用程序来收集数据,应用程序不会向服务器发送指标。
前提条件
- 集群中运行的 Prometheus 服务器
流程
将
vertx-micrometer
和vertx-web
依赖项包含到应用程序的pom.xml
文件中:pom.xml
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-micrometer-metrics</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency>
从 3.5.4 开始,公开 Prometheus 的指标需要在自定义
Launcher
类中配置 Eclipse Vert.x 选项。在 custom
Launcher
类中,覆盖beforeStartingVertx
和afterStartingVertx
方法来配置指标引擎,例如:CustomLauncher.java 文件示例
package org.acme; import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.config.MeterFilter; import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.http.HttpServerOptions; import io.vertx.micrometer.MicrometerMetricsOptions; import io.vertx.micrometer.VertxPrometheusOptions; import io.vertx.micrometer.backends.BackendRegistries; public class CustomLauncher extends Launcher { @Override public void beforeStartingVertx(VertxOptions options) { options.setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true) .setStartEmbeddedServer(true) .setEmbeddedServerOptions(new HttpServerOptions().setPort(8081)) .setEmbeddedServerEndpoint("/metrics")) .setEnabled(true)); } @Override public void afterStartingVertx(Vertx vertx) { PrometheusMeterRegistry registry = (PrometheusMeterRegistry) BackendRegistries.getDefaultNow(); registry.config().meterFilter( new MeterFilter() { @Override public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) { return DistributionStatisticConfig.builder() .percentilesHistogram(true) .build() .merge(config); } }); }
创建自定义
Verticle
类,并覆盖start
方法来收集指标。例如,使用Timer
类测量执行时间:CustomVertxApp.java 文件示例
package org.acme; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import io.vertx.core.AbstractVerticle; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.http.HttpServerOptions; import io.vertx.micrometer.backends.BackendRegistries; public class CustomVertxApp extends AbstractVerticle { @Override public void start() { MeterRegistry registry = BackendRegistries.getDefaultNow(); Timer timer = Timer .builder("my.timer") .description("a description of what this timer does") .register(registry); vertx.setPeriodic(1000, l -> { timer.record(() -> { // Do something }); }); } }
在应用程序的
pom
> 属性以指向您的自定义类:.xml
文件中设置 <vertx.verticle
> 和 <vertx.launcher<properties> ... <vertx.verticle>org.acme.CustomVertxApp</vertx.verticle> <vertx.launcher>org.acme.CustomLauncher</vertx.launcher> ... </properties>
启动应用程序:
$ mvn vertx:run
多次调用 traced 端点:
$ curl http://localhost:8080/ Hello
等待至少 15 秒进行集合,并在 Prometheus UI 中看到指标:
-
在 http://localhost:9090/ 上打开 Prometheus UI,然后在 Expression 框中输入
hello
。 -
从建议中选择
application:hello_count
示例,再单击 Execute。 - 在显示的表中,您可以看到调用资源方法的次数。
-
或者,选择
application:hello_time_mean_seconds
以查看所有调用的平均值时间。
请注意,您创建的所有指标都带有
application:
前缀。还有其他指标,作为 Eclipse MicroProfile Metrics 规格要求,由 Eclipse Vert.x 自动公开。这些指标以base:
和vendor:
前缀,并公开有关应用在其中运行的 JVM 的信息。-
在 http://localhost:9090/ 上打开 Prometheus UI,然后在 Expression 框中输入
其他资源
- 有关在 Eclipse Vert.x 中使用 Micrometer 指标的更多信息,请参阅 Eclipse Vert.x} Micrometer Metrics。