简体   繁体   中英

Is it possible to expose Dropwizard metrics in logs

I'm trying to set up a logging scheme in an application to show the thread count count in Dropwizard. When I go to the admin port on 8081, I can see the metrics that Dropwizard provides for an application. For example, under Gauges, you can see jvm.memory.total.used, along with the value. I'd like to set up the slf4j logs to show that in some of my requests. So, for example, my GET requests might look something like this:

@GET
@TIME
@Produces(MediaType.Application_JSON)
public List<String> getNames {
    List<String> nameList = dao.getList();
    log.info("Memory used: {}", /*the value for jvm.memory.total.used*/)
    return nameList;
}

However, I've been unable to find much on exposing the metrics in this way. Is this possible, and if so, is it even practical to do it?

UPDATE:

I managed to expose the metrics for the tread states in a GET service, so now I'm experimenting with trying to get the results into a log statement. Here is my code so far:

@Path("metrics")
@Produces(MediaType.APPLICATION_JSON)
public class GetStuff {
    private static final Logger log = LoggerFactory.getLogger(GetStuff.class);

    @GET
    @Path("/metrics")
    public MetricRegistry provideMetrics() {
        MetricRegistry metrics = new MetricRegistry();
        metrics.register("jvm.threads", new ThreadStatesGaugeSet());

        log.info("JVM Thread Count: {}", metrics.getGauges().get("jvm.threads.count"));
        return metrics;
    }
}

I've confirmed that this returns the metrics, so now I'm trying to get the log statement to work properly, and print out the value of that gauge. My main issue now is that the log seems to be printing out the address of the value in the getGauges mapping, instead of the value associated to the key. More specifically, I'm getting values like this:

JVM Thread Count: com.codahale.metrics.jvm.ThreadStatesGaugeSet$2@129e489

Does anyone know how I can fix this, so I can get the actual value?

EDIT: I've opened another question related to this one, here .

I managed to get this working. Most of the process is documented in my edit to the question, but here is the final code:

@Path("metrics")
@Produces(MediaType.APPLICATION_JSON)
public class GetStuff {
    private static final Logger log = LoggerFactory.getLogger(GetStuff.class);

    @GET
    @Path("/metrics")
    public MetricRegistry provideMetrics() {
        MetricRegistry metrics = new MetricRegistry();
        metrics.register("jvm.threads", new ThreadStatesGaugeSet());

        log.info("JVM Thread Count: {}", metrics.getGauges().get("jvm.threads.count").getValue);
        return metrics;
    }
}

I had forgotten to use getValue() to get the value for the log statement, which nicely fixes my issue of the address being printed from the getGauges map instead of the actual value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM