Domanda

Spring Boot has preconfigured metrics. As I know it uses http://metrics.codahale.com/ library. How can I get MetricRegistry object to add my custom metrics?

È stato utile?

Soluzione 2

Spring Boot Does not use Codahale Metrics (yet). The plan is to support it as an option if it is on the classpath. If you prefer to do it that way then a MetricRegistry will be in the application context and you can simply @Autowire it and use it. You can see that work in progress on a branch in my fork.

The Boot interfaces for adding metrics are GaugeService and CounterService. You inject those and use them to record measurements. When Codahale gets support then this will be the recommended entry point for that as well, so you can get started using it now and add the Codahale stuff later if you want.

Altri suggerimenti

There's some integration magic accomplished by http://www.ryantenney.com/metrics-spring/ that wires codahale metrics into the actuator /health endpoint.

With this dependency included,

compile 'com.ryantenney.metrics:metrics-spring:3.0.0-RC2'

you can "enableMetrics" in your application configuration

@EnableMetrics
public class ApplicationConfiguration {
    ...

This allows you to time each request with the @timed annotation:

@Timed
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody
Foo foo() {
    ...

and to have your other interactions with MetricRegistry aggregate into the actuator /health endpoint.

I've put together an example application which implements this integration:

https://github.com/benschw/consul-cluster-puppet/tree/master/demo

and written a more in depth tutorial here: http://txt.fliglio.com/2014/10/spring-boot-actuator/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top