Metrics Servlet, embedded Jetty

3,080 views
Skip to first unread message

Vincent Rischmann

unread,
Apr 20, 2013, 2:06:49 PM4/20/13
to metric...@googlegroups.com
Hi,

I'm trying to use AdminServlet from the metrics-servlets module with an embedded jetty server but it's not working. I always get this:

org.eclipse.jetty.servlet.ServletHolder$1: javax.servlet.ServletException: Couldn't find a HealthCheckRegistry instance.


Right now I'm creating the metric registry and the healthcheckregistry as a static field in my main class.

final static MetricRegistry metricRegistry = new MetricRegistry("defaultMetricRegistry");
final static HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();


I add the servlet like this:

context.addServlet(new ServletHolder(AdminServlet.class), "/monitoring/*");

This is specific to embedded jetty, but it's just adding a servlet.

When I go to "http://localhost:8080/monitoring/" I get the exception above.

Does anybody have an idea about what the problem is ?

Matt Brown

unread,
Apr 23, 2013, 11:38:41 AM4/23/13
to metric...@googlegroups.com
Look at the source for the HealthCheckServlet (https://github.com/codahale/metrics/blob/master/metrics-servlets/src/main/java/com/codahale/metrics/servlets/HealthCheckServlet.java#L33) - it expects to find the registry to use in a servletContext attribute named "com.codahale.metrics.servlets.HealthCheckServlet.registry".

Baljeet Sandhu

unread,
May 29, 2013, 10:08:22 PM5/29/13
to metric...@googlegroups.com
The docs state:
"If the servlet context has an attributed named com.codahale.metrics.servlet.HealthCheckServlet.registry which is aHealthCheckRegistry, HealthCheckServlet will use that instead of the default HealthCheckRegistry."

In the code we throw an exception right away if that attribute is not present. Setting up a special servlet context attribute is a bit of  a chore. Any one know how/if the servlet can get the default registry?

The same applies to the other servlets too.

Coda Hale

unread,
May 30, 2013, 5:58:36 AM5/30/13
to metric...@googlegroups.com
There is no default registry.
--
You received this message because you are subscribed to the Google Groups "metrics-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to metrics-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Coda Hale
http://codahale.com

Paul Sanwald

unread,
May 30, 2013, 2:15:10 PM5/30/13
to metric...@googlegroups.com
I just had this same problem, and I was able to solve it by adding a few things to my servlet context. this code worked for me:

public class MetricsServletContextListener implements ServletContextListener {
    static final MetricRegistry metricRegistry = new MetricRegistry();
    static final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        servletContextEvent.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY,healthCheckRegistry);
        servletContextEvent.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);
    }

On Saturday, April 20, 2013 2:06:49 PM UTC-4, Vincent Rischmann wrote:

Coda Hale

unread,
May 30, 2013, 3:02:38 PM5/30/13
to metric...@googlegroups.com
You know, I'd totally accept that as a pull request.


--
You received this message because you are subscribed to the Google Groups "metrics-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to metrics-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paul Sanwald

unread,
May 30, 2013, 5:28:03 PM5/30/13
to metric...@googlegroups.com
Cool, I will submit it as a pull request :). Would you prefer me to create an issue on github and then link my pull request, or just send the pull request? I will assume the latter, but let me know if that's not cool.

--paul

Coda Hale

unread,
May 30, 2013, 5:29:00 PM5/30/13
to metric...@googlegroups.com
Just send it.


On Thursday, May 30, 2013, Paul Sanwald wrote:
Cool, I will submit it as a pull request :). Would you prefer me to create an issue on github and then link my pull request, or just send the pull request? I will assume the latter, but let me know if that's not cool.

--paul

--
You received this message because you are subscribed to the Google Groups "metrics-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to metrics-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

gitted

unread,
Jun 27, 2013, 1:25:40 PM6/27/13
to metric...@googlegroups.com
So its been a while so not sure what was pulled but I do see that listerner but not sure how to wire it up.

This is what I have in my main class:

final Server server = new Server(8080);

ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/api"); 

servletContextHandler.addServlet(new ServletHolder(new SomeServlet)), "/v1/");
servletContextHandler.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");

server.setHandler(servletContextHandler);

logger.info("starting jetty");
server.start();
server.join();

After reading this thread and http://metrics.codahale.com/manual/servlets/ I know I have to add the servlet context with the registry etc.

How can I do this?

Also, since this is a servlet context, is my understanding correct that I only need 1 of these across all of my servlets?

gitted

unread,
Jun 27, 2013, 1:27:30 PM6/27/13
to metric...@googlegroups.com
Sorry forgot to metion, in my "SomeServlet" I have this:

public class SomeServlet extends HttpServlet {
    // metrics
    private final MetricRegistry metricsRegistry = new MetricRegistry();

    private final Meter requestsMetric = metricsRegistry.meter("requests");
    private final Timer responsesTimer = metricsRegistry.timer("responses");

....
}

I did get that expected error since I haven't wired up the context listener:

Couldn't find a MetricRegistry instance.

Alper Akture

unread,
Feb 11, 2014, 4:12:21 PM2/11/14
to metric...@googlegroups.com
This thread is pretty old, but after struggling with this issue for a few days, I was able to fix it with

    _server = new Server(_port);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[]{"index.html"});
    resourceHandler.setResourceBase(".");
    HandlerList handlers = new HandlerList();
    ServletContextHandler guiceHandler = new ServletContextHandler();

    ServletContextHandler metricsContextHandler = new ServletContextHandler();
    metricsContextHandler.setContextPath("/one/metrics");
    metricsContextHandler.addEventListener(new MyMetricsServletContextListener(_metricRegistry));
    metricsContextHandler.addServlet(MetricsServlet.class, "/registry");

    guiceHandler.setContextPath("/one/id");
    try {
        FilterHolder guiceFilter = new FilterHolder(_filter);
        guiceHandler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));
        handlers.setHandlers(new Handler[]{metricsContextHandler, guiceHandler, resourceHandler});
        _server.setHandler(handlers);
        _server.setDumpAfterStart(true);
        _server.start();
    }catch(Exception ex) {
        log.error("Error starting http server", ex);
        throw new RuntimeException(ex);
    }

and my ContextListener (registry provided by a guice binding):

private static class MyMetricsServletContextListener extends MetricsServlet.ContextListener {
    private MetricRegistry _metricRegistry;
    public MyMetricsServletContextListener(MetricRegistry metricRegistry) {
        _metricRegistry = metricRegistry;
    }
    @Override
    protected MetricRegistry getMetricRegistry() {
        return _metricRegistry;
    }
}
Reply all
Reply to author
Forward
0 new messages