Dropwizard Metrics-Introduction to basic use

Source: Internet
Author: User

Previously introduced in the Healthcheck how to add some simple health detection through metrics LIB to the system, now talk about Dropwizard metrics more important parts, record the system's measurement information. Dropwizard offers a variety of metrics: The simplest counter, the complexity of the histogram for calculating the time distribution, the meter for calculating the rate, and the timer with histogram and meter functions at the same time. Interested to see the implementation details, the class is very small, easy to read.
Metrics's reporting approach is very similar to the Healthcheck, which provides a variety of reporting channels for the library itself. It's also easy to expand on your own.
Metrics offers a variety of packages that can help simplify development. Metrics-servlet can directly show the information of metrics and Healthcheck in the Web request.
Next, let's use an example to measure the Web application's request, and then get metrics and healthcheck information through Adminservlet.

    • Import the required packages in Maven
 <dependency>            <groupId>Io.dropwizard.metrics</groupId>            <artifactid>Metrics-core</artifactid>            <version>${metrics.version}</version>        </Dependency>        <dependency>            <groupId>Io.dropwizard.metrics</groupId>            <artifactid>Metrics-servlets</artifactid>            <version>${metrics.version}</version>        </Dependency>        <dependency>            <groupId>Io.dropwizard.metrics</groupId>            <artifactid>Metrics-json</artifactid>            <version>${metrics.version}</version>        </Dependency>        <dependency>            <groupId>Io.dropwizard.metrics</groupId>            <artifactid>Metrics-healthchecks</artifactid>            <version>${metrics.version}</version>        </Dependency>
    • 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<    /span> Health_check_registry; }}
publicclass MetricsServletContextListener extends MetricsServlet.ContextListener {    publicstaticfinalnew MetricRegistry();    @Override    protectedgetMetricRegistry() {        return METRIC_REGISTRY;    }}
    • Create a timer in spring Web action, add to Metricsservletcontextlistener's metricregistery, and start calling time for each method to get the context, End Execution Context.stop
    Private FinalTimer executions = MetricsServletContextListener.METRIC_REGISTRY.timer (Metricregistry.name (signupcontroller.class ,"Executions"));@Autowired    PrivateUserService UserService;@RequestMapping(method = Requestmethod.post) PublicStringRegister(@Valid user User, Bindingresult bindingresult, Redirectattributes redirectattributes) {FinalTimer.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 (); }    }
    • Configuring the Adminservlet access path in Web. XML, configuring 2 listener
    <listener>        <listener-class>Com.cloud.demo.HealthCheckServletContextListener</listener-class></listener><listener>        <listener-class>Com.cloud.demo.MetricsServletContextListener</listener-class></listener>    <servlet>        <servlet-name>Metrics</servlet-name>        <servlet-class>Com.codahale.metrics.servlets.AdminServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>Metrics</servlet-name>        <url-pattern>/metrics/*</url-pattern>    </servlet-mapping>

This allows you to view system metrics data through/metrics/healthcheck or/metrics/metrics.

Remember: The most important thing about system metrics is to analyze what metrics are needed in the system, and technology implementations are less important.
For more Dropwizard function packs, refer to the user manual

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dropwizard Metrics-Introduction to basic use

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.