Abhishek Nayak
Abhishek Nayak

Reputation: 3748

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

Upvotes: 0

Views: 3009

Answers (1)

Sasha
Sasha

Reputation: 1411

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.

Upvotes: 1

Related Questions