Metrics for my t-shirt api

I am trying to measure my web service using Metrics

http://metrics.codahale.com/manual/jersey/

I do not understand how to use this library? Do I need to add something to the web.xml file?

thanks

+6
source share
3 answers

To use the web service in Jersey, you need to add a module to the metrics-jersey application that contains the @Provider implementation class (make sure Jersey finds it) that will allow you to use Jersey resource tools annotated with @Timed, Metered and ExceptionMetered.

Metrics reports through JMX by default, so you can use JConsole to test your tools. As Alex wrote, there are other reporting options, but this requires additional configuration or code (calling the enable method in the reporter). For example, you can receive reports in JSON over HTTP or send web services to a monitoring server such as Graphite.

+4
source

As I can see, you just need to include metrics lib in the build path. In web service methods, you simply use the @Timed annotation.

To view reports, you must include the reporting style that you like - reporters

0
source

Throw your laundry and start your grin. I got this job!

  • Connect the servlet. To create and store indicators, you need a common place. Build one of them for both MetricsRegistry and HealthCheckRegistry :

     public class MetricsServletContextListener extends MetricsServlet.ContextListener { public static final MetricRegistry METRIC_REGISTRY = new MetricRegistry(); @Override protected MetricRegistry getMetricRegistry() { return METRIC_REGISTRY; } } 
  • Set the servlet context with the data in some initial area:

     sc.getServletContext().setAttribute( "com.codahale.metrics.servlets.HealthCheckServlet.registry", healthChecks ); sc.getServletContext().setAttribute( "com.codahale.metrics.servlets.MetricsServlet.registry", MetricsServletContextListener.METRIC_REGISTRY ); 
  • Url: http: // blah / blah / metrics / metrics? Pretty = true

  • Create one of these guys. This ties the metrics to Jersey:

      @Provider public class TmaticInstrumentedResourceMethodDispatchAdapterWrapper implements ResourceMethodDispatchAdapter { private InstrumentedResourceMethodDispatchAdapter adapter = null; public TmaticInstrumentedResourceMethodDispatchAdapterWrapper() { adapter = new InstrumentedResourceMethodDispatchAdapter(MetricsServletContextListener.METRIC_REGISTRY); } @Override public ResourceMethodDispatchProvider adapt(ResourceMethodDispatchProvider provider) { return adapter.adapt(provider); } } 
  • Tell about this jersey. Since it uses the @Provider annotation, it must be in an area that can check it. I should have added mine to web.xml here, but you might not need it:

     <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>blah.endpoint,blah.utils</param-value> </init-param> 
  • And add the @Timed annotation to your Jersey endpoint.

0
source

Source: https://habr.com/ru/post/911934/


All Articles