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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…