在 Dropwizard 中公开 Prometheus 指标

Expose Prometheus Metrics in Dropwizard

我已经使用 Dropwizard. Now I want it to also expose Prometheus metrics 实现了 Java 网络服务。

我已经关注了this pretty straight-forward example. However, the endpoint at http://localhost:9090/metrics还没曝光

相关代码如下:

pom.xml 中的依赖项:

    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_dropwizard</artifactId>
        <version>0.5.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_servlet -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_servlet</artifactId>
        <version>0.5.0</version>
    </dependency>

Java代码:

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.exporter.MetricsServlet;
[...]

public class MyApplication extends Application<MyServiceConfiguration> {

@Override
public void run(final MyServiceConfiguration configuration,
        final Environment environment) {
    final MyServiceResource resource = createResource(configuration);
    environment.jersey().register(resource);

    registerHealthChecks(environment, resource);

    registerMetrics(environment);
}

private void registerMetrics(Environment environment) {
    CollectorRegistry collectorRegistry = new CollectorRegistry();
    collectorRegistry.register(new DropwizardExports(environment.metrics()));
    environment.admin().addServlet("metrics", new MetricsServlet(collectorRegistry))
            .addMapping("/metrics");
}

有没有指出我做错了什么?

记住默认的 dropwizard 配置在不同的端口上有管理应用程序。这就是您可以找到指标 servlet 的地方。