如何在日志中显示Dropwizard活动请求

时间:2018-10-09 13:23:53

标签: java counter dropwizard metrics codahale-metrics

这个问题与我问的问题enter image description here有关。

我正在尝试每10分钟记录对我的Dropwizard应用程序进行的活动请求的数量。这个想法是跟踪应用程序的使用情况,以便它可以得到适当的支持。为此,我尝试将dropwizard指标公开到日志中。为了进行测试,我编写了以下代码:

@GET
@Path("/metrics")
public MetricRegistry provideMetrics() {
    MetricRegistry metrics = new MetricRegistry();
    metrics.register("io.dropwizard.jetty.MutableServletContextHandler.active-dispatches", new Counter());

    logger.info("Total Requests: {}",
                metrics.counter("io.dropwizard.jetty.MutableServletContextHandler.active-requests"));
    logger.info("Active Requests: {}",
                metrics.counter("io.dropwizard.jetty.MutableServletContextHandler.active-dispatches"));
    logger.info("Suspended Requests: {}",
                metrics.counter("io.dropwizard.jetty.MutableServletContextHandler.active-suspended"));

    return metrics;
}

理论上,当我运行以下GET请求时,活动请求和分派请求的数量应等于1:

@GET
@Path("all")
public List<String> getAll() {
    List<String> list = dao.getAllExamples();
    TimeUnit.SECONDS.sleep(10);
    return list;
}

但是,我制作的模因路径的输出仍为0。我已经检查了8081上Dropwizard管理端口的度量,它显示:

counters: {
io.dropwizard.jetty.MutableServletContextHandler.active-dispatches: {
count: 1
},
io.dropwizard.jetty.MutableServletContextHandler.active-requests: {
count: 1
},
io.dropwizard.jetty.MutableServletContextHandler.active-suspended: {
count: 0
},
}

1 个答案:

答案 0 :(得分:2)

您似乎正在创建一个完全独立的MetricRegistry,并注册与dropwizard相同名称的新计数器。您需要访问dropwizard创建的默认MetricRegistry。您可以通过environment.metrics()

进行访问