Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Dropwizard Metrics Servlet with spring-boot?

I'm using spring-boot-starter-actuator for getting a localhost/metrics endpoint.

Now I also want to use the dropwizard.metrics and the metrics-servlets dependency. On their webpage (https://dropwizard.github.io/metrics/3.1.0/getting-started/) it is stated that with this a AdminServet with some kind of admin menu for metrics, healt, threaddump and ping would be created.

But I don't see that servlet. Do I maybe have to register it explicit within spring-boot?

like image 221
membersound Avatar asked Jul 28 '15 13:07

membersound


People also ask

Does Dropwizard use spring?

Of the available Dropwizard integrations, Spring support is also included.

Which is better spring boot vs Dropwizard?

The bare bone spring boot app starts in 1.64 seconds whereas the bare bone Dropwizard app took 1.526 seconds to startup. Spring Boot consumes much more memory. This was true. Spring Boot loaded 7591 classes whereas Dropwizard loaded 6255 classes.

What is io Dropwizard metrics?

Metrics is a Java library which gives you unparalleled insight into what your code does in production. Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.

What does @timed annotation do?

Web MVC and Annotation-Based WebFlux The interceptors need to be enabled for every request handler or controller that you want to time. Add @Timed to: A controller class to enable timings on every request handler in the controller.


2 Answers

I had to instantiate the servlet explicit and provide a servlet mapping path as follows:

@Bean
public ServletRegistrationBean servletRegistrationBean(){
    return new ServletRegistrationBean(new AdminServlet(),"/metrics/admin/*");
}
like image 134
membersound Avatar answered Sep 21 '22 16:09

membersound


In case someone is working with version 5.0.0, these steps were required to get it work:

@Inject
private ServletContext servletContext;

@Inject
private MetricRegistry metricRegistry;

@Inject
private HealthCheckRegistry healthCheckRegistry;

@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean(){
    servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
        metricRegistry);
    servletContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY,
        healthCheckRegistry);

    return new ServletRegistrationBean<>(new AdminServlet(), "/metrics/*");
}

Source: https://stackoverflow.com/a/41382649/1047418

like image 42
Torben Avatar answered Sep 20 '22 16:09

Torben