[英]Is it possible to expose Dropwizard metrics in logs
我正在嘗試在應用程序中設置日志記錄方案,以顯示Dropwizard中的線程計數。 當轉到8081上的管理端口時,可以看到Dropwizard為應用程序提供的指標。 例如,在“儀表”下,您可以看到jvm.memory.total.used及其值。 我想設置slf4j日志以在某些請求中顯示該信息。 因此,例如,我的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;
}
但是,在以這種方式公開指標方面,我找不到很多東西。 這可能嗎?如果可以,這樣做是否可行?
更新:
我設法在GET服務中公開胎面狀態的度量,因此現在我正在嘗試將結果放入日志語句中。 到目前為止,這是我的代碼:
@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映射中的值的地址,而不是與鍵關聯的值。 更具體地說,我得到的是這樣的價值:
JVM Thread Count: com.codahale.metrics.jvm.ThreadStatesGaugeSet$2@129e489
有誰知道我可以解決這個問題,以便獲得實際價值?
編輯:我已經開了與此相關的另一個問題, 在這里 。
我設法使這個工作。 大部分過程記錄在我對問題的編輯中,但這是最終代碼:
@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映射中打印地址而不是實際值的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.