搜索

5.2. 使用 Eclipse Vert.x 使用 Prometheus 公开应用程序指标

download PDF

Prometheus 连接到受监控的应用程序来收集数据,应用程序不会向服务器发送指标。

前提条件

  • 集群中运行的 Prometheus 服务器

流程

  1. vertx-micrometervertx-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>

  2. 从 3.5.4 开始,公开 Prometheus 的指标需要在自定义 Launcher 类中配置 Eclipse Vert.x 选项。

    在 custom Launcher 类中,覆盖 beforeStartingVertxafterStartingVertx 方法来配置指标引擎,例如:

    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);
            }
        });
    }

  3. 创建自定义 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
    
          });
        });
      }
    }

  4. 在应用程序的 pom .xml 文件中设置 < vertx.verticle > 和 <vertx.launcher > 属性以指向您的自定义类:

    <properties>
      ...
      <vertx.verticle>org.acme.CustomVertxApp</vertx.verticle>
      <vertx.launcher>org.acme.CustomLauncher</vertx.launcher>
      ...
    </properties>
  5. 启动应用程序:

    $ mvn vertx:run
  6. 多次调用 traced 端点:

    $ curl http://localhost:8080/
    Hello
  7. 等待至少 15 秒进行集合,并在 Prometheus UI 中看到指标:

    1. http://localhost:9090/ 上打开 Prometheus UI,然后在 Expression 框中输入 hello
    2. 从建议中选择 application:hello_count 示例,再单击 Execute
    3. 在显示的表中,您可以看到调用资源方法的次数。
    4. 或者,选择 application:hello_time_mean_seconds 以查看所有调用的平均值时间。

    请注意,您创建的所有指标都带有 application: 前缀。还有其他指标,作为 Eclipse MicroProfile Metrics 规格要求,由 Eclipse Vert.x 自动公开。这些指标以 base:vendor: 前缀,并公开有关应用在其中运行的 JVM 的信息。

其他资源

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.