com.codahale.metrics.servlets.HealthCheckServlet Java Examples

The following examples show how to use com.codahale.metrics.servlets.HealthCheckServlet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpMetricsServer.java    From hermes with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #2
Source File: Poseidon.java    From Poseidon with Apache License 2.0 6 votes vote down vote up
private ServletContextHandler getMetricsHandler() {
    MetricRegistry registry = Metrics.getRegistry();
    HealthCheckRegistry healthCheckRegistry = Metrics.getHealthCheckRegistry();
    healthCheckRegistry.register("rotation", new Rotation(configuration.getRotationStatusFilePath()));

    registry.registerAll(new GarbageCollectorMetricSet());
    registry.registerAll(new MemoryUsageGaugeSet());
    registry.registerAll(new ThreadStatesGaugeSet());
    registry.registerAll(new JvmAttributeGaugeSet());

    ServletContextHandler servletContextHandler = new ServletContextHandler();
    servletContextHandler.setContextPath("/__metrics");
    servletContextHandler.setAttribute(MetricsServlet.class.getCanonicalName() + ".registry", registry);
    servletContextHandler.setAttribute(HealthCheckServlet.class.getCanonicalName() + ".registry", healthCheckRegistry);
    servletContextHandler.addServlet(new ServletHolder(new AdminServlet()), "/*");

    return servletContextHandler;
}
 
Example #3
Source File: CassandraConnectorTask.java    From debezium-incubator with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #4
Source File: HermesServlet.java    From hermes with Apache License 2.0 5 votes vote down vote up
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";
}
 
Example #5
Source File: StatisticsController.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
public void setServletContext(final ServletContext servletContext) {
    servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY, this.metricsRegistry);
    servletContext.setAttribute(MetricsServlet.SHOW_SAMPLES, Boolean.TRUE);
    servletContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, this.healthCheckRegistry);
}
 
Example #6
Source File: AdminTransportConfiguration.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Bean(initMethod="start", destroyMethod="stop")
@Order(0)
public Server adminServer(
           @Value("${admin.hostname}") String hostname,
           @Value("${admin.port}") int port) {

       // set up servlets
       ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY);
	context.setErrorHandler(null);
	context.setWelcomeFiles(new String[] { "/" });
	
	// enable gzip
	context.addFilter(GzipFilter.class, "/*", null);
	
	// add common admin servlets
	context.addServlet(new ServletHolder(new HealthServlet(healthCheckRegistry)), "/healthcheck");
	context.addServlet(new ServletHolder(new ClasspathResourceServlet("com/kixeye/chassis/transport/admin", "/admin/", "index.html")), "/admin/*");
	context.addServlet(new ServletHolder(new PropertiesServlet()), "/admin/properties");
	context.addServlet(new ServletHolder(new ClasspathDumpServlet()), "/admin/classpath");

       // add websocket servlets if WebSockets have been initialized
       if (mappingRegistry != null && messageRegistry != null) {
           context.addServlet(new ServletHolder(new ProtobufMessagesDocumentationServlet(appName, mappingRegistry, messageRegistry)), "/schema/messages/protobuf");
           context.addServlet(new ServletHolder(new ProtobufEnvelopeDocumentationServlet()), "/schema/envelope/protobuf");
       }
	
       // add metric servlets if Metric has been initialized
       if (metricRegistry != null && healthCheckRegistry != null) {
           context.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);
           context.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthCheckRegistry);
           
           ServletHolder holder = new ServletHolder(new AdminServlet());
           holder.setInitParameter("service-name", System.getProperty("app.name"));
           context.addServlet(holder, "/metrics/*");
       }

       // create the server
   	InetSocketAddress address = StringUtils.isBlank(hostname) ? new InetSocketAddress(port) : new InetSocketAddress(hostname, port);
   	
   	Server server = new Server();

   	JettyConnectorRegistry.registerHttpConnector(server, address);
       server.setHandler(context);
   
	return server;
}
 
Example #7
Source File: Main.java    From billow with Apache License 2.0 4 votes vote down vote up
private Main(Config config) throws Exception {
    log.info("Startup...");

    try {
        System.out.println(Resources.toString(getResource("banner.txt"), Charsets.UTF_8));
    } catch (IllegalArgumentException | IOException e) {
        log.debug("No banner.txt", e);
    }

    final MetricRegistry metricRegistry = new MetricRegistry();
    final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();

    log.info("Creating database");

    final Config awsConfig = config.getConfig("aws");
    final Long refreshRate = awsConfig.getDuration("refreshRate", TimeUnit.MILLISECONDS);

    final AWSDatabaseHolder dbHolder = new AWSDatabaseHolder(awsConfig);

    final Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.getContext().put(AWSDatabaseHolderRefreshJob.DB_KEY, dbHolder);
    scheduler.start();

    final SimpleTrigger trigger = newTrigger().
            withIdentity(AWSDatabaseHolderRefreshJob.NAME).
            startNow().
            withSchedule(simpleSchedule().withIntervalInMilliseconds(refreshRate).repeatForever()).
            build();

    final JobDetail jobDetail = newJob(AWSDatabaseHolderRefreshJob.class).
            withIdentity(AWSDatabaseHolderRefreshJob.NAME).
            build();

    scheduler.scheduleJob(jobDetail, trigger);

    log.info("Creating age health check");
    healthCheckRegistry.register("DB", new HealthCheck() {
        @Override
        protected Result check() throws Exception {
            return dbHolder.healthy();
        }
    });

    log.info("Creating HTTP servers");
    final Server mainServer = new Server(config.getInt("mainPort"));
    final Server adminServer = new Server(config.getInt("adminPort"));

    configureConnectors(mainServer);
    configureConnectors(adminServer);

    log.info("Creating HTTP handlers");
    final Handler mainHandler = new Handler(metricRegistry, dbHolder, refreshRate);
    final InstrumentedHandler instrumentedHandler =
            new InstrumentedHandler(metricRegistry);
    instrumentedHandler.setHandler(mainHandler);

    mainServer.setHandler(instrumentedHandler);

    final ServletContextHandler adminHandler = new ServletContextHandler();
    adminHandler.addServlet(new ServletHolder(new AdminServlet()), "/*");

    final ServletContext adminContext = adminHandler.getServletContext();
    adminContext.setAttribute(MetricsServlet.METRICS_REGISTRY, metricRegistry);
    adminContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthCheckRegistry);
    adminServer.setHandler(adminHandler);

    log.info("Starting HTTP servers");

    adminServer.start();
    mainServer.start();

    log.info("Joining...");

    mainServer.join();
    adminServer.join();

    log.info("Shutting down scheduler...");

    scheduler.shutdown();

    log.info("We're done!");
}