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

问题描述:

这个问题与我问的问题有关.此处.

This question is related to the one I ask here.

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

I'm trying to log the number of active requests being made to my Dropwizard application every 10 minutes. The idea is to track usage of the app so it can get the proper support. In doing this, I'm trying to expose the dropwizard metrics to the logs. For testing purposes, I've made the following code:

@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:

In theory, when I run the following GET request, the number of active requests and dispatched requests should be equal to 1:

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

但是,我制作的模因路径的输出仍为0.我已经在8081的Dropwizard管理端口上检查了指标,并显示:

However, the output from the metics path I made remains at 0. I've checked the metrics at the Dropwizard admin port on 8081, and it shows:

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
},
}

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

It looks like you are creating a completely separate MetricRegistry and registering new counters with the same names as the dropwizard ones. What you need is access to the default MetricRegistry that dropwizard creates. You can access this via environment.metrics()