Search code examples
scalacodahale-metrics

How do I get the last elapsed time from Codahale metrics timers?


I'm using the Codahale metrics in my Scala application, but don't seem to see a way to get the last value for a timed operation. I can get mean, median, and percentiles - all great stuff from the histogram - but I just want to know how long the last operation took!

Look at entry zero in the snapshot and scale it from the value in nanoseconds down to milliseconds? That doesn't seem to be right...

I must be missing the obvious!

    t.time {

      iService.availCheck(q)

    } match {

      case ReturnInfo(Some(true)) =>

        // The HUB says TRUE, this name is available. That's gold.

        logger.debug("Avail check for (" + q + ") HUB TRUE: " + (t.snapshot.getValues()(0) / 100000.0).toString + "ms")

        Ok(Some(true))
    }

Solution

  • Found the answer - the stop() function, embedded in the t.time call, returns the elapsed time. I just had to refactor my code to have access to it. If not consumed, it's lost. Poof.

    Go figure ;)