简体   繁体   English

是否可以在日志中公开Dropwizard指标

[英]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. 我正在尝试在应用程序中设置日志记录方案,以显示Dropwizard中的线程计数。 When I go to the admin port on 8081, I can see the metrics that Dropwizard provides for an application. 当转到8081上的管理端口时,可以看到Dropwizard为应用程序提供的指标。 For example, under Gauges, you can see jvm.memory.total.used, along with the value. 例如,在“仪表”下,您可以看到jvm.memory.total.used及其值。 I'd like to set up the slf4j logs to show that in some of my requests. 我想设置slf4j日志以在某些请求中显示该信息。 So, for example, my GET requests might look something like this: 因此,例如,我的GET请求可能看起来像这样:

@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. 我设法在GET服务中公开胎面状态的度量,因此现在我正在尝试将结果放入日志语句中。 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. 我已经确认这会返回指标,所以现在我试图使log语句正常工作,并打印出该量规的值。 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. 我现在的主要问题是日志似乎正在打印出getGauges映射中的值的地址,而不是与键关联的值。 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. 我忘记了使用getValue()获取log语句的值,这很好地解决了我从getGauges映射中打印地址而不是实际值的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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