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));
}
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;
}
}
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();
}
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);
}
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);
});
}
}
});
}
Aggregations