0
我似乎無法正確設置Metrics servlet。它的404。我使用Guice 3.0,嵌入式Jetty 9和Metrics 3.0.1。這裏就是我設置Jetty服務器:使用Guice和Embedded jetty配置codahale MetricsServlet
public void start() {
//start jetty
_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();
guiceHandler.addEventListener(_metricsServletContextListener);
guiceHandler.setContextPath("/rest");
guiceHandler.addServlet(new ServletHolder(new MetricsServlet(_metricRegistry)), "/metrics");
FilterHolder guiceFilter = new FilterHolder(_filter);
guiceHandler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));
handlers.setHandlers(new Handler[]{guiceHandler, resourceHandler});
_server.setHandler(handlers);
_server.setDumpAfterStart(true);
try {
_server.start();
}catch(Exception ex) {
log.error("Error starting http server", ex);
throw new RuntimeException(ex);
}
log.info("one id http service started, doc base: {}, URI: {}", resourceHandler.getResourceBase(), _server.getURI());
}
這裏的ContextListener擴展:
public class MetricsServletContextListener extends MetricsServlet.ContextListener {
private MetricRegistry _metricRegistry;
public MetricsServletContextListener(MetricRegistry metricRegistry) {
_metricRegistry = metricRegistry;
}
@Override
protected MetricRegistry getMetricRegistry() {
return _metricRegistry;
}
}
如果我打http://localhost:8080/rest/metrics
,它404的。看到碼頭啓動日誌,該servlet得到創建的,似乎是高達:
+= [email protected] - STARTED
| += [email protected]{/rest,null,AVAILABLE} - STARTED
| | += [email protected] - STARTED
| | | += com.codahale.metrics.servlets.MetricsServlet- [email protected]==com.codahale.metrics.servlets.MetricsServlet,-1,true - STARTED
| | | +- [/metrics]=>com.codahale.metrics.servlets.MetricsServlet-299abf92
| | | += com.google.inject.servlet.GuiceFilter-5baef385 - STARTED