如何将Dropwizard指标暴露给Prometheus

9

我已经使用Dropwizard实现了一个Java Web服务。现在我希望它也能公开Prometheus指标

我遵循了this非常简单的例子。然而,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");
}

有什么指针可以告诉我我做错了什么吗?


2
管理员端口是多少?/metrics嵌套在管理员应用程序下。 - LiorH
2
感谢指针,端点 http://localhost:8081/metrics 实际上是存在的。 - Carsten
我想将Dropwizard指标暴露给Prometheus,但是我卡住了。你能提供一个完整的例子吗? - gstackoverflow
1个回答

2

记住,默认的Dropwizard配置中,管理应用程序在不同的端口上。那里就可以找到指标servlet。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接