Search in sources :

Example 11 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(eq(io.dropwizard.metrics.servlets.HealthCheckServlet.HEALTH_CHECK_REGISTRY))).thenReturn(healthCheckRegistry);
    final io.dropwizard.metrics.servlets.HealthCheckServlet healthCheckServlet = new io.dropwizard.metrics.servlets.HealthCheckServlet(null);
    healthCheckServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, times(1)).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 12 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;
    }
    final Object mapperAttr = context.getAttribute(HEALTH_CHECK_MAPPER);
    if (mapperAttr instanceof ObjectMapper) {
        this.mapper = (ObjectMapper) mapperAttr;
    } else {
        this.mapper = new ObjectMapper();
    }
    this.mapper.registerModule(new HealthCheckModule());
    final Object httpStatusIndicatorAttr = context.getAttribute(HEALTH_CHECK_HTTP_STATUS_INDICATOR);
    if (httpStatusIndicatorAttr instanceof Boolean) {
        this.httpStatusIndicator = (Boolean) httpStatusIndicatorAttr;
    } else {
        this.httpStatusIndicator = true;
    }
}
Also used : ServletException(javax.servlet.ServletException) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ExecutorService(java.util.concurrent.ExecutorService) ServletContext(javax.servlet.ServletContext) HealthCheckModule(com.codahale.metrics.json.HealthCheckModule) HealthCheckFilter(com.codahale.metrics.health.HealthCheckFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 13 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(com.codahale.metrics.jetty9.InstrumentedHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) AdminServlet(com.codahale.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) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) InstrumentedConnectionFactory(com.codahale.metrics.jetty9.InstrumentedConnectionFactory)

Example 14 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 HealthCheckServlet healthCheckServlet = new HealthCheckServlet(healthCheckRegistry);
    healthCheckServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, never()).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 15 with HealthCheckRegistry

use of com.codahale.metrics.health.HealthCheckRegistry in project microservices by pwillhan.

the class HealthModule method configure.

@Override
protected void configure() {
    bind(HealthCheckRegistry.class).toProvider(new Provider<HealthCheckRegistry>() {

        private HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();

        @Override
        public HealthCheckRegistry get() {
            return healthCheckRegistry;
        }
    });
    bind(HealthResource.class).in(Scopes.SINGLETON);
    Provider<HealthCheckRegistry> healthCheckRegistryProvider = getProvider(HealthCheckRegistry.class);
    bindListener(Matchers.any(), new TypeListener() {

        final Converter<String, String> converter = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN);

        @Override
        public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
            if (HealthCheck.class.isAssignableFrom(type.getRawType())) {
                encounter.register((InjectionListener<I>) injectee -> {
                    HealthCheck check = (HealthCheck) injectee;
                    String name = converter.convert(check.getClass().getSimpleName());
                    healthCheckRegistryProvider.get().register(name, check);
                });
            }
        }
    });
}
Also used : TypeListener(com.google.inject.spi.TypeListener) HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) HealthCheck(com.codahale.metrics.health.HealthCheck) InjectionListener(com.google.inject.spi.InjectionListener)

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