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?
Spring boot now supports Codahale Metrics out of the box.
See http://docs.spring.io/spring-boot/docs/1.0.0.RC5/reference/htmlsingle/#production-ready-code-hale-metrics
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.
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/
Related
For exporting the metrics (to Prometheus) from the spring boot micro service, we can use the spring boot actuator and one more option is to use the Prometheus JMX exporter(https://github.com/prometheus/jmx_exporter) as a javaAgent when running the service. Though both of the options serve the same purpose, I do see that the JMX exporter is exporting way lot more metrics than the spring boot actuator. I was scouting through some spring boot documentations to see if there is any option to enable more metrics with spring boot actuator, looks like all the JMX metrics are enabled by default. So the questions is, is there a way to expose more metrics from spring boot actuator? Is there any recommendation or comparison study available for both the options mentioned above?
Any help here is greatly appreciated. Thanks!
If you are using Spring boot 2.x, then it works like this:
In Spring Boot 2.0, the in-house metrics were replaced with Micrometer support, so we can expect breaking changes. If our application was using metric services such as GaugeService or CounterService, they will no longer be available.
Instead, we're expected to interact with Micrometer directly. In Spring Boot 2.0, we'll get a bean of type MeterRegistry autoconfigured for us.
for Spring boot 1.x:
The metrics endpoint publishes information about OS and JVM as well as application-level metrics. Once enabled, we get information such as memory, heap, processors, threads, classes loaded, classes unloaded, and thread pools along with some HTTP metrics as well.
and this seems to work like Prometheus JMX
When researching how to customize Spring's RestTemplate, I came across the 4.15.1. RestTemplate Customization section of the official Spring Boot docs (see excerpt and link at the bottom of this question).
It says "make sure to configure your custom instance with a RestTemplateBuilderConfigurer". I searched for "RestTemplateBuilderConfigurer" but found nothing, so apparently this is something that I need to implement.
Just looking at the signature, and comparing to RestTemplateBuilder#configure (link), it seems like the idea is that configurer.configure takes a RestTemplateBuilder and merges the configuration with Spring Boot's default RestTemplateBuilder configuration.
I could write a bunch of imperative code to do this configuration merging, but I suspect that I'm missing something here and there's a much easier way to do this with some out-of-the-box Spring features.
So, how can I implement a RestTemplateBuilderConfigurer? Or did I misunderstand the docs?
4.15.1. RestTemplate Customization (link)
Finally, you can also create your own RestTemplateBuilder bean. To
prevent switching off the auto-configuration of a RestTemplateBuilder
and prevent any RestTemplateCustomizer beans from being used, make
sure to configure your custom instance with a
RestTemplateBuilderConfigurer. The following example exposes a
RestTemplateBuilder with what Spring Boot would auto-configure, except
that custom connect and read timeouts are also specified:
#Bean
public RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer configurer) {
return configurer.configure(new RestTemplateBuilder()).setConnectTimeout(Duration.ofSeconds(5))
.setReadTimeout(Duration.ofSeconds(2));
}
Currently spring boot actuator metrics returns metrics for various system parameters. I want to turn on some selected metrics parameters, for example; show only metrics related to memory and processor. I had several attempts to figure out solution but nothing worked for me. I see SystemPublicMetrics registers all basic system metrics and management system matrics, how can I turn on only few of them?
Required output:
{
"mem": 495055,
"mem.free": 372397,
"processors": 4
}
You can do this by disabling CacheMetricsAutoConfiguration of spring boot auto configuration at start-up by adding metrics class to exclusion list.
For instance to disable cache metrics add following at startup:
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsAutoConfiguration;
#EnableAutoConfiguration(exclude = {CacheMetricsAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {
...
...
This should help..
You will not be able to disable specific metrics. Rather, you will be able to enable/disable at endpoints only.
Following are the flags that you can add in application.properties to enable/disable specific endpoints in Spring Boot Actuator
endpoints.autoconfig.enabled=false
endpoints.beans.enabled=false
endpoints.configprops.enabled=false
endpoints.dump.enabled=false
endpoints.env.enabled=false
endpoints.health.enabled=true
endpoints.info.enabled=true
endpoints.metrics.enabled=false
endpoints.mappings.enabled=false
endpoints.shutdown.enabled=false
endpoints.trace.enabled=false
ive been tasked with a total refactor of legacy code. It's a simple webservice, just an http request, then business logic with possibly a few database calls and a few other microservice calls, then a json response. I am being pushed not to use spring boot because no one else around me has used it before, and I was told jersey does everything spring boot does. I've never used jersey so im trying to find out how to do things that spring boot makes simple (ie repository layer with spring-data, caching, spring-consul, spring-zuul, spring-actuator, spring-circuit-breaker) It looks like jersey does do an analog of spring-security, bean validation, and easy insertion of servlet filters, but not everything spring-boot does. Is there an easy way to wire in a JPA type repository in jersey? I cant find it in the docs at https://jersey.java.net/documentation/latest/index.html.
I think about it this way. There are different layers in your application. You have a service layer, and you have a "REST layer". You access the Spring repositories with the service layer. Then you have the REST layer. With Spring, you have Spring MVC which is its web layer implementation, that you can also use as REST services. There is also Jersey, which is completely independent of Spring, which is a another REST layer options.
That be said, when using Spring MVC as the REST layer, adding the service layer with Spring data is seamless. But Jersey also has integration with Spring, that allows us to use Spring at the service layer inside our Jersey REST services. You check out this post which has some links to example of how this can be done (no hacking, this is supported out of the box). Using this approach, you can just injector your Spring data repositories into your Jersey resource class
interface PetsRepository extends JpaRepository<Pets, Long> {}
#Path("/pets")
class PetsResource {
#Autowired
private PetsRepository repo;
}
Now lets talk about Spring Boot. Spring Boot is just a bootstrapping framework. What it does is allow you to easily bootstrap an application without all the boilerplate configuration you would need without it. When your using Spring Boot for your REST services, you're not actually using Spring Boot itself as the REST service engine. You are only using it to bootstrap Spring MVC and maybe your Spring Data. But Spring MVC is the actual REST service engine.
Now like I said before, Jersey has support for integrating Spring into into it (for the service layer). Because of this support, Spring Boot has also provided a bootstrap configuration to integrate this support seamlessly. So instead of using the manual configuration that you would see in one of the examples linked to above, Spring Boot handles this configuration for us. So we can use Jersey as the REST layer, and Spring beans as the service layer. Check out the links below
See also:
Spring Boot docs for Jersey support
I'm working with Spring Boot, and I want to annotate some of my #Service methods with #Timed so I could view their statistics calling the metrics endpoint.
I've read from the Spring documentation that is can use dropwizard registry, but I can't find any example in which it uses dropwizar annotations.
How can I configure my Spring Boot app so each method annotated with #Timed automatically publishes its statistics using the build-in Spring actuator module?
Have you had a look at: http://www.ryantenney.com/metrics-spring/
Seems that you need to include that library if you want to use Spring with Metrics 3.0 / 3.1.