简体   繁体   中英

Codahale metrics counter reset and count everyday

Using Codahale metrics is there an way to count for last 24 hours(today).

to generate report like below:

Request Count:

lastSec   lastMin    lastHour    today
=======================================
1           5           22        45   

Response Count:

lastSec   lastMin    lastHour    today
=======================================
1           5           22        45   

There is Meter methods to get last second, minute, fifteen rates. but how to get for last hour and today count?

Below tried:

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

To sum up or calculate count(total) per arbitrary interval for counters reported with counter.inc :

hitcount(perSecond(AProcessor.a-req-count.count), '1hour')

hitcount(perSecond(AProcessor.a-res-count.count), '1day')

Afaik it does all the black magic inside. Including but not limited to summarize(scaleToSeconds(nonNegativeDerivative(your.count),1), '1day') and also there should be scaling according to carbon's retention periods (one or many) that fall into chosen aggregation interval.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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