How to show Dropwizard active requests in logs

偶尔善良 提交于 2020-01-16 13:19:01

问题


This question is related to the one I ask here.

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

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

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

回答1:


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()



来源:https://stackoverflow.com/questions/52722173/how-to-show-dropwizard-active-requests-in-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!