是否可以在日志中公开Dropwizard指标 [英] Is it possible to expose Dropwizard metrics in logs

查看:84
本文介绍了是否可以在日志中公开Dropwizard指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序中设置日志记录方案,以显示Dropwizard中的线程计数.当转到8081上的管理端口时,可以看到Dropwizard为应用程序提供的指标.例如,在仪表"下,您可以看到jvm.memory.total.used及其值.我想设置slf4j日志以在某些请求中显示该信息.因此,例如,我的GET请求可能看起来像这样:

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?

更新:

我设法在GET服务中公开胎面状态的度量,因此现在我正在尝试将结果放入日志语句中.到目前为止,这是我的代码:

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

我已经确认这将返回指标,所以现在我试图使log语句正常工作,并打印出该量规的值.我现在的主要问题是日志似乎正在打印出getGauges映射中值的地址,而不是与键关联的值.更具体地说,我得到的是这样的值:

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?

我打开了与此问题相关的另一个问题,这里.

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

我忘了使用getValue()来获取log语句的值,这很好地解决了我从getGauges映射中打印地址而不是实际值的问题.

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.

这篇关于是否可以在日志中公开Dropwizard指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆