我已经使用 Dropwizard 实现了一个 Java Web 服务.现在我想让它也暴露 Prometheus metrics .
我已关注 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");
}
任何指向我做错了什么的指针?
最佳答案
记住默认 dropwizard 配置在不同的端口上有管理应用程序。您可以在那里找到指标 servlet。
关于java - 在 Dropwizard 中公开 Prometheus 指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52931289/