下面列出了怎么用com.codahale.metrics.servlets.PingServlet的API类实例代码及写法,或者点击链接到github查看源代码。
private void init() {
logger.info("Initializing Broker Http Metrics Reporter");
InetSocketAddress inetSocketAddress = new InetSocketAddress(bindAddress, port);
server = new Server(inetSocketAddress);
ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
servletContextHandler.addEventListener(new MetricsServletContextListener());
servletContextHandler.addEventListener(new JVMMetricsServletContextListener());
servletContextHandler.addEventListener(new HealthCheckServletContextListener());
servletContextHandler.addServlet(new ServletHolder(new HermesServlet()), "/hermes");
servletContextHandler.addServlet(new ServletHolder(new MetricsServlet()), "/metrics/metrics");
servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), "/metrics/threads");
servletContextHandler.addServlet(new ServletHolder(new HealthCheckServlet()), "/metrics/healthcheck");
servletContextHandler.addServlet(new ServletHolder(new PingServlet()), "/metrics/ping");
server.setHandler(servletContextHandler);
logger.info("Finished initializing Broker Http Metrics Reporter");
}
private void evaluateWeb()
throws Exception {
if (!runPerServiceMode(web)) {
return;
}
// Load balancers should hit the ping servlet, exposed on the main port to reflect main connection pool issues
_environment.servlets().addServlet("/ping", new PingServlet());
// Serve static assets
_environment.jersey().register(FaviconResource.class);
// Add a filter to provide finer 5xx metrics than the default DropWizard metrics include.
//noinspection unchecked
_environment.jersey().getResourceConfig().getContainerResponseFilters()
.add(new ServerErrorResponseMetricsFilter(_environment.metrics()));
}
private void initHttpServer() {
int httpPort = config.httpPort();
LOGGER.info("HTTP port is {}", httpPort);
httpServer = new Server(httpPort);
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/");
httpServer.setHandler(contextHandler);
contextHandler.addServlet(new ServletHolder(new PingServlet()), "/ping");
contextHandler.addServlet(new ServletHolder(new BuildInfoServlet(getBuildInfoMap(this.getClass()))), "/buildinfo");
contextHandler.addServlet(new ServletHolder(new MetricsServlet(METRIC_REGISTRY_INSTANCE)), "/metrics");
contextHandler.addServlet(new ServletHolder(new HealthCheckServlet(registerHealthCheck())), "/health");
}
public void init(ServletConfig config) throws ServletException {
this.config = config;
if (Boolean.valueOf(config.getInitParameter("show-jvm-metrics"))) {
JVMMetricsServletContextListener listener = new JVMMetricsServletContextListener();
this.jvmServlet = new JVMMetricsServlet(listener.getMetricRegistry());
this.jvmServlet.init(config);
this.jvmUri = "/jvm";
}
healthCheckServlet = new HealthCheckServlet(HermesMetricsRegistry.getHealthCheckRegistry());
healthCheckServlet.init(config);
pingServlet = new PingServlet();
pingServlet.init(config);
threadDumpServlet = new ThreadDumpServlet();
threadDumpServlet.init(config);
metricServlet = new MetricsServlet(HermesMetricsRegistry.getMetricRegistry());
metricServlet.init(config);
this.metricServletGroupByT = new HashMap<String, MetricsServlet>();
this.metricServletGroupByTP = new HashMap<String, MetricsServlet>();
this.metricServletGroupByTPG = new HashMap<String, MetricsServlet>();
this.healthcheckUri = "/healthcheck";
this.pingUri = "/ping";
this.threadsUri = "/threads";
this.globalUri = "/global";
this.metricsUri = "/metrics";
}
@Override
protected void configure() {
// NOTE: AdminServletModule (metrics-guice integration) generates invalid links, so wire up servlets ourselves
final Clock clock = Clock.defaultClock();
bind(Clock.class).toInstance(clock);
final JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
bind(JsonFactory.class).toInstance(jsonFactory);
install(new ServletModule()
{
@Override
protected void configureServlets() {
bind(MetricsServlet.class);
bind(HealthCheckServlet.class);
serve(MOUNT_POINT + "/ping").with(new PingServlet());
serve(MOUNT_POINT + "/threads").with(new ThreadDumpServlet());
serve(MOUNT_POINT + "/data").with(MetricsServlet.class);
serve(MOUNT_POINT + "/healthcheck").with(HealthCheckServlet.class);
serve(MOUNT_POINT + "/prometheus").with(new io.prometheus.client.exporter.MetricsServlet());
// record metrics for all webapp access
filter("/*").through(new InstrumentedFilter());
bind(SecurityFilter.class);
// configure security
filter(MOUNT_POINT + "/*").through(SecurityFilter.class);
}
});
// require permission to use endpoints
install(new FilterChainModule()
{
@Override
protected void configure() {
addFilterChain(MOUNT_POINT + "/**",
NexusAuthenticationFilter.NAME,
AnonymousFilter.NAME,
AntiCsrfFilter.NAME,
PermissionsFilter.config("nexus:metrics:read"));
}
});
log.info("Metrics support configured");
}