The metrics-spring module enables the use of Metrics annotations with Spring AOP, complete with simple XML configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.yammer.com/schema/metrics"
xsi:schemaLocation="
http://www.yammer.com/schema/metrics http://www.yammer.com/schema/metrics/metrics.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<metrics:metrics-registry id="metrics"/>
<metrics:health-check-registry id="health"/>
<metrics:annotation-driven metrics-registry="metrics" health-check-registry="health"/>
<metrics:jmx-reporter id="metricsJmxReporter" metrics-registry="metrics"/>
<!-- other beans -->
</beans>
The most important part of the configuration is the element <metrics:annotation-driven />. This registers custom Spring bean processors, which serve three main purposes:
Note
Due to a limitation in Spring AOP, only public methods can be proxied, so @Timed, @Metered, and @ExceptionMetered have no effect on non-public methods. As @Gauge doesn’t involve a proxy, it is possible to use this annotation on private fields and methods.
The <metrics:annotation-driven /> element accepts 5 optional arguments:
The elements <metrics:metrics-registry /> and <metrics:health-check-registry /> are present as a convenience for creating new registry beans.
The element <metrics:jmx-reporter /> creates a JMX Reporter for the specified metrics registry. A JMX Reporter is automatically created for the default metrics registry.