使用Codahale指标可以计算过去24小时(今天)。
生成如下报告:
lastSec lastMin lastHour today
=======================================
1 5 22 45
lastSec lastMin lastHour today
=======================================
1 5 22 45
有Meter methods获得最后一秒,十分钟,十五分的费率。但如何获得最后一小时和今天的计数?
以下尝试过:
public class ReportMetrics {
static final MetricRegistry metrics = new MetricRegistry();
static final Counter aReqCount = ReportMetrics.metrics.counter(name(AProcessor.class, "a-req-count"));
static final Counter aResCount = ReportMetrics.metrics.counter(name(AProcessor.class, "a-res-count"));
private static final AProcessor aProcessor = new AProcessor();
public static void main(String[] args) {
startReport();
for(int i=0; i<=5; i++){
//add
aProcessor.addJob();
wait5Seconds();
//take
arProcessor.takeJob();
wait5Seconds();
}
}
static void startReport() {
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
}
static void wait5Seconds() {
try {
Thread.sleep(5*1000);
}
catch(InterruptedException e) {}
}
public long requestCount(){
ReportMetrics.metrics.aReqCount.getCount();
}
public long responseCount(){
ReportMetrics.metrics.aResCount.getCount();
}
public long pendingRequestCount(){
return requestCount() - responseCount();
}
}
class AProcessor {
public void addJob(){
ReportMetrics.metrics.aReqCount.inc();
}
public Object takeJob(){
ReportMetrics.metrics.aResCount.inc();
}
}
答案 0 :(得分:1)
总结或计算使用counter.inc
报告的计数器的每个任意间隔的计数(总计):
hitcount(perSecond(AProcessor.a-req-count.count), '1hour')
hitcount(perSecond(AProcessor.a-res-count.count), '1day')
Afaik它完成了所有的黑魔法。包括但不限于summarize(scaleToSeconds(nonNegativeDerivative(your.count),1), '1day')
并且还应该根据碳的保留期(一个或多个)进行缩放,这些保留期落入选定的聚合间隔。