Dropwizardmetrics-basic usage

Source: Internet
Author: User

Dropwizardmetrics-basic usage

Previously, healthcheck introduced how to use metrics lib to add some simple health checks to the system. Now, let's talk about the more important part of dropwizard metrics and record the measurement information of the system. Dropwizard provides a variety of measurement methods: the simplest count counter, the histogram for calculating the time distribution of complex points, the meter for calculating the speed, and the timer with the histogram and meter functions at the same time. If you are interested, you can check the implementation details. There are few classes and it is easy to understand.
Metrics reports similar to healthcheck, which provides various reporting channels in the database. It can also be easily expanded by yourself.
Metrics provides various packages to help simplify development. Metrics-servlet can directly display metrics and healthcheck information in the web request.
Next, let's give an example to Measure Web application requests, and then obtain metrics and healthcheck information through AdminServlet.

Import the required package in maven
 
              
   
    io.dropwizard.metrics
               metrics-core            
   
    ${metrics.version}
           
          
              
   
    io.dropwizard.metrics
               metrics-servlets            
   
    ${metrics.version}
           
          
              
   
    io.dropwizard.metrics
               metrics-json            
   
    ${metrics.version}
           
          
              
   
    io.dropwizard.metrics
               metrics-healthchecks            
   
    ${metrics.version}
           
  
Create a servlet to register metrics and healtcheck
public class HealthCheckServletContextListener extends HealthCheckServlet.ContextListener{ public static final HealthCheckRegistry HEALTH_CHECK_REGISTRY = new HealthCheckRegistry(); @Override protected HealthCheckRegistry getHealthCheckRegistry() { return HEALTH_CHECK_REGISTRY; }}
public class MetricsServletContextListener extends MetricsServlet.ContextListener { public static final MetricRegistry METRIC_REGISTRY = new MetricRegistry(); @Override protected MetricRegistry getMetricRegistry() { return METRIC_REGISTRY; }}
Create a timer in Spring Web action and add it to MetricRegistery of MetricsServletContextListener. Call time for each method to obtain the context and end the execution of context. stop.
 private final Timer executions = MetricsServletContextListener.METRIC_REGISTRY.timer(MetricRegistry.name(SignupController.class, "executions")); @Autowired private UserService userService; @RequestMapping(method = RequestMethod.POST) public String register(@Valid User user, BindingResult bindingResult, RedirectAttributes redirectAttributes) { final Timer.Context context = executions.time(); if (bindingResult.hasErrors()) { redirectAttributes.addFlashAttribute("user", user); redirectAttributes.addFlashAttribute(BindingResult.MODEL_KEY_PREFIX + "user", bindingResult); context.stop(); return "redirect:/"; } try { userService.registerUser(user); redirectAttributes.addFlashAttribute("username", user.getUsername()); return "redirect:/"; } catch (UserExistsException e) { return "redirect:/"; } finally { context.stop(); } }
Configure the AdminServlet access path in Web. xml and 2 listener
   com.cloud.demo.HealthCheckServletContextListener     com.cloud.demo.MetricsServletContextListener     metrics   com.codahale.metrics.servlets.AdminServlet     metrics   /metrics/*  

In this way, you can view system metric data through/metrics/healthcheck or/metrics.

Remember: the most important thing about system measurement is to analyze what kind of measurement information is needed in the system. Technical implementation is not that important.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.