Search in sources :

Example 21 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project chassis by Kixeye.

the class MetricsConfiguration method healthCheckRegistry.

/**
 * Initializes the health check registry
 *
 * @return health check registry bean
 */
@Bean
public HealthCheckRegistry healthCheckRegistry(ApplicationContext context) {
    final HealthCheckRegistry bean = new HealthCheckRegistry();
    // auto-register beans implementing health checks
    Map<String, HealthCheck> healthChecks = context.getBeansOfType(HealthCheck.class);
    for (HealthCheck check : healthChecks.values()) {
        bean.register(check.getClass().getName(), check);
    }
    return bean;
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) HealthCheck(com.codahale.metrics.health.HealthCheck) Bean(org.springframework.context.annotation.Bean)

Example 22 with HealthCheckRegistry

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

the class HealthCheckServletTest method constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig.

@Test
public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig() throws Exception {
    final HealthCheckRegistry healthCheckRegistry = mock(HealthCheckRegistry.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    final io.dropwizard.metrics.servlets.HealthCheckServlet healthCheckServlet = new io.dropwizard.metrics.servlets.HealthCheckServlet(healthCheckRegistry);
    healthCheckServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, never()).getAttribute(eq(io.dropwizard.metrics.servlets.HealthCheckServlet.HEALTH_CHECK_REGISTRY));
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ServletConfig(jakarta.servlet.ServletConfig) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.Test)

Example 23 with HealthCheckRegistry

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

the class HealthCheckServletTest method constructorWithRegistryAsArgumentUsesServletConfigWhenNull.

@Test
public void constructorWithRegistryAsArgumentUsesServletConfigWhenNull() throws Exception {
    final HealthCheckRegistry healthCheckRegistry = mock(HealthCheckRegistry.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    when(servletContext.getAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY)).thenReturn(healthCheckRegistry);
    final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(null);
    healthCheckServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, times(1)).getAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY);
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 24 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project chassis by Kixeye.

the class ChassisConfiguration method healthCheckRegistry.

/**
 * Initializes the health check registry
 *
 * @return health check registry bean
 */
@Bean
public HealthCheckRegistry healthCheckRegistry(ApplicationContext context, DiscoveryManager eureka) {
    final HealthCheckRegistry bean = new HealthCheckRegistry();
    // auto-register beans implementing health checks
    Map<String, HealthCheck> healthChecks = context.getBeansOfType(HealthCheck.class);
    for (HealthCheck check : healthChecks.values()) {
        bean.register(check.getClass().getName(), check);
    }
    // connect health checks into Eureka
    if (!disableEureka) {
        eureka.getDiscoveryClient().registerHealthCheckCallback(new HealthCheckCallback() {

            @Override
            public boolean isHealthy() {
                for (Entry<String, HealthCheck.Result> entry : bean.runHealthChecks().entrySet()) {
                    if (!entry.getValue().isHealthy()) {
                        return false;
                    }
                }
                return true;
            }
        });
    }
    return bean;
}
Also used : Entry(java.util.Map.Entry) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) HealthCheck(com.codahale.metrics.health.HealthCheck) HealthCheckCallback(com.netflix.appinfo.HealthCheckCallback) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Bean(org.springframework.context.annotation.Bean)

Example 25 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project tutorials by eugenp.

the class HealthCheckIntegrationTest method whenUseHealthCheck_thenHealthChecked.

@Test
public void whenUseHealthCheck_thenHealthChecked() {
    HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();
    healthCheckRegistry.register("db", new DatabaseHealthCheck());
    healthCheckRegistry.register("uc", new UserCenterHealthCheck());
    assertThat(healthCheckRegistry.getNames().size(), equalTo(2));
    Map<String, HealthCheck.Result> results = healthCheckRegistry.runHealthChecks();
    assertFalse(results.isEmpty());
    results.forEach((k, v) -> assertTrue(v.isHealthy()));
    healthCheckRegistry.unregister("uc");
    assertThat(healthCheckRegistry.getNames().size(), equalTo(1));
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) Test(org.junit.Test)

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