Search in sources :

Example 6 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project helios by spotify.

the class TotalHealthCheckGaugeTest method testAllFail.

@Test
public void testAllFail() {
    final HealthCheckRegistry registry = new HealthCheckRegistry();
    registry.register("fail1", stubHealthCheck(HealthCheck.Result.unhealthy("error")));
    registry.register("fail2", stubHealthCheck(HealthCheck.Result.unhealthy("error")));
    final TotalHealthCheckGauge gauge = new TotalHealthCheckGauge(registry);
    assertThat(gauge.getValue(), is(0));
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) Test(org.junit.Test)

Example 7 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project HikariCP by brettwooldridge.

the class TestMetrics method testSetters1.

@Test
public void testSetters1() throws Exception {
    HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(1);
    ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    MetricRegistry metricRegistry = new MetricRegistry();
    HealthCheckRegistry healthRegistry = new HealthCheckRegistry();
    try {
        Connection connection = ds.getConnection();
        connection.close();
        // After the pool as started, we can only set them once...
        ds.setMetricRegistry(metricRegistry);
        ds.setHealthCheckRegistry(healthRegistry);
        // and never again...
        ds.setMetricRegistry(metricRegistry);
        Assert.fail("Should not have been allowed to set registry after pool started");
    } catch (IllegalStateException ise) {
        // pass
        try {
            ds.setHealthCheckRegistry(healthRegistry);
            Assert.fail("Should not have been allowed to set registry after pool started");
        } catch (IllegalStateException ise2) {
        // pass
        }
    } finally {
        ds.close();
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) MetricRegistry(com.codahale.metrics.MetricRegistry) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) Connection(java.sql.Connection) Test(org.junit.Test)

Example 8 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project HikariCP by brettwooldridge.

the class HikariConfig method setHealthCheckRegistry.

/**
 * Set a Codahale HealthCheckRegistry to use for HikariCP.
 *
 * @param healthCheckRegistry the Codahale HealthCheckRegistry to set
 */
public void setHealthCheckRegistry(Object healthCheckRegistry) {
    if (healthCheckRegistry != null) {
        if (healthCheckRegistry instanceof String) {
            try {
                InitialContext initCtx = new InitialContext();
                healthCheckRegistry = initCtx.lookup((String) healthCheckRegistry);
            } catch (NamingException e) {
                throw new IllegalArgumentException(e);
            }
        }
        if (!(healthCheckRegistry instanceof HealthCheckRegistry)) {
            throw new IllegalArgumentException("Class must be an instance of com.codahale.metrics.health.HealthCheckRegistry");
        }
    }
    this.healthCheckRegistry = healthCheckRegistry;
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 9 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project metrics by dropwizard.

the class ExampleServer method main.

public static void main(String[] args) throws Exception {
    COUNTER_1.inc();
    COUNTER_2.inc();
    final ThreadPool threadPool = new InstrumentedQueuedThreadPool(REGISTRY);
    final Server server = new Server(threadPool);
    final Connector connector = new ServerConnector(server, new InstrumentedConnectionFactory(new HttpConnectionFactory(), REGISTRY.timer("http.connection")));
    server.addConnector(connector);
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/initial");
    context.setAttribute(MetricsServlet.METRICS_REGISTRY, REGISTRY);
    context.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, new HealthCheckRegistry());
    final ServletHolder holder = new ServletHolder(new AdminServlet());
    context.addServlet(holder, "/dingo/*");
    final InstrumentedHandler handler = new InstrumentedHandler(REGISTRY);
    handler.setHandler(context);
    server.setHandler(handler);
    server.start();
    server.join();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) InstrumentedHandler(io.dropwizard.metrics.jetty11.InstrumentedHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) InstrumentedQueuedThreadPool(io.dropwizard.metrics.jetty11.InstrumentedQueuedThreadPool) AdminServlet(io.dropwizard.metrics.servlets.AdminServlet) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) InstrumentedQueuedThreadPool(io.dropwizard.metrics.jetty11.InstrumentedQueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) InstrumentedConnectionFactory(io.dropwizard.metrics.jetty11.InstrumentedConnectionFactory)

Example 10 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project metrics by dropwizard.

the class HealthCheckServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    final ServletContext context = config.getServletContext();
    if (null == registry) {
        final Object registryAttr = context.getAttribute(HEALTH_CHECK_REGISTRY);
        if (registryAttr instanceof HealthCheckRegistry) {
            this.registry = (HealthCheckRegistry) registryAttr;
        } else {
            throw new ServletException("Couldn't find a HealthCheckRegistry instance.");
        }
    }
    final Object executorAttr = context.getAttribute(HEALTH_CHECK_EXECUTOR);
    if (executorAttr instanceof ExecutorService) {
        this.executorService = (ExecutorService) executorAttr;
    }
    final Object filterAttr = context.getAttribute(HEALTH_CHECK_FILTER);
    if (filterAttr instanceof HealthCheckFilter) {
        filter = (HealthCheckFilter) filterAttr;
    }
    if (filter == null) {
        filter = HealthCheckFilter.ALL;
    }
    this.mapper = new ObjectMapper().registerModule(new HealthCheckModule());
}
Also used : ServletException(jakarta.servlet.ServletException) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ExecutorService(java.util.concurrent.ExecutorService) ServletContext(jakarta.servlet.ServletContext) HealthCheckModule(com.codahale.metrics.json.HealthCheckModule) HealthCheckFilter(com.codahale.metrics.health.HealthCheckFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)31 Test (org.junit.Test)11 MetricRegistry (com.codahale.metrics.MetricRegistry)6 HealthCheck (com.codahale.metrics.health.HealthCheck)6 Test (org.junit.jupiter.api.Test)6 HikariDataSource (com.zaxxer.hikari.HikariDataSource)3 ServletContext (jakarta.servlet.ServletContext)3 ServletContext (javax.servlet.ServletContext)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)2 Result (com.codahale.metrics.health.HealthCheck.Result)2 HealthCheckFilter (com.codahale.metrics.health.HealthCheckFilter)2 HealthCheckModule (com.codahale.metrics.json.HealthCheckModule)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ServletConfig (jakarta.servlet.ServletConfig)2 Connection (java.sql.Connection)2 ExecutorService (java.util.concurrent.ExecutorService)2 Connector (org.eclipse.jetty.server.Connector)2 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)2 Server (org.eclipse.jetty.server.Server)2