How to show Dropwizard active requests in logs

2019-08-16 17:25发布

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条回答
放荡不羁爱自由
2楼-- · 2019-08-16 18:02

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

查看更多
登录 后发表回答