Julien
Julien

Reputation: 471

metrics for my api powered by jersey

I try to instrumente my Jersey webservice with Metrics

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

I don't understand how to use this library? Do I need to add something in my web.xml file?

Thanks

Upvotes: 6

Views: 2352

Answers (3)

markthegrea
markthegrea

Reputation: 3851

Drop your linen and start your grin'n. I got this working!

  1. Hook up the servlet. You need a generic spot to make and store the metrics. Build one of these 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;
        }
    }
    
  2. Set the servlet context with the data in some startup area:

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

  4. Create one of these guys. This hooks up 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);
         }
     }
    
  5. Tell jersey about it. Since it uses the @Provider annotation it must be in an area that can scan for it. I had to add mine to the web.xml here but you might not have to:

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

Upvotes: 0

Le Hibou
Le Hibou

Reputation: 1591

To instrument your Jersey web service, you must add the metrics-jersey module to your application, it contains a @Provider implementation class (make sure Jersey find it) that allow you to instrument your Jersey resources methods annotated with @Timed, Metered and ExceptionMetered.

By default, Metrics reports through JMX, so you can use JConsole to validate your instrumentations. Like Alex wrote, there are others reporting options but it requires additional configuration or code (call enable method on the Reporter). For example you can fetch reports in JSON by HTTP, or have you webservice send reports to a monitoring server such as Graphite.

Upvotes: 4

Alex Stybaev
Alex Stybaev

Reputation: 4693

As I can see, you just need to include metrics lib to the build path. On web-services methods you just use annotation @Timed.

To see the reports, you must enable the reporting style you like - reporters

Upvotes: 0

Related Questions