codahale/metrics — pull request #99 by ccare (2011-10-27 15:44:27+00:00)
Title: Added support for Guice to create an MeterMetric to monitor exceptions

Hi,

Thanks for accepting my last patch. Would this addition be of any interest to you? I've found it helps solve a frequently occurring pattern and removes lots of boiler-plate.

Thanks,

Charles

===============================

When instrumenting classes with the annotations in metrics-guice, it is
sometimes desirable to count the number of times a method has failed.
Previously, this resulted in lots of occurrences of the following pattern:

@Metered
public void doBlah() throws MyException {
try {
...
} catch (MyException e) {
failureMetric.mark();
throw e;
}
}

This commit introduces a new annotation @ExceptionMetered to the
metrics-guice module. This new annotation has similar semantics to the
existing @Metered annotation, but instead of counting invocations of the
decorated method, it counts the number of invocations that resulted in
an exception being thrown. It can be used in conjunction with the
existing @Metered and @Timed annotation. For example, the above example
can be written as:

@Metered
@ExceptionMetered(cause=MyException.class)
public void doBlah() throws MyException {
...
}

As usual, the new annotation takes arguments to modify the name and
units of the metric. Using the "cause" field, it is possible to
specify the type of Throwable (defaults to java.lang.Exception) that
the instrumentation will capture.