Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Interface for HealthCheck Operations Providers #846

Closed
jwcarman opened this issue Aug 3, 2015 · 4 comments
Closed

Create Interface for HealthCheck Operations Providers #846

jwcarman opened this issue Aug 3, 2015 · 4 comments
Milestone

Comments

@jwcarman
Copy link

jwcarman commented Aug 3, 2015

We are using an OSGi environment and we have created a "wrapper" interface for HealthCheck objects, so that they can be exposed as OSGi services and picked up using the "whiteboard" pattern. It would be great if there were a native interface bundled with this library that folks could implement in order to provide health check results.

@jillesvangurp
Copy link

We use Java 8 and it would be nice if HealthCheck was a functional interface instead of an abstract class. That way we could use lambda functions to quickly create health checks without a lot of boiler plate for the inner class.

The execute method in the class could move elsewhere probably. Unfortunately it is public and non-final, which suggest that somebody might be overriding this. Or if you want to keep the abstract class, it could be renamed to AbstractHealthCheck and implement a new HealthCheck interface. This would be a backwards incompatible change. Alternatively, the interface could have a different name.

Another interface that would be nice is something similar to the MetricSet interface to indicate that a class provides a HealthCheck. I actually created one for our spring setup along with a spring context listener that registers all health checks.

public interface HealthCheckProvider {
    HealthCheck healthCheck();
}

public class HealthCheckContextListener implements ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOG = LoggerFactory.getLogger(HealthCheckContextListener.class);

    private final HealthCheckRegistry healthCheckRegistry;

    public HealthCheckContextListener(HealthCheckRegistry healthCheckRegistry) {
        this.healthCheckRegistry = healthCheckRegistry;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // register health checks provided by other beans
        Map<String, HealthCheckProvider> healthCheckProvidingBeans = event.getApplicationContext().getBeansOfType(HealthCheckProvider.class);
        for(Entry<String, HealthCheckProvider> e: healthCheckProvidingBeans.entrySet()) {
            LOG.info("health check " + e.getKey() + " " + e.getValue().getClass().getName());
            HealthCheck check = e.getValue().healthCheck();
            healthCheckRegistry.register(e.getKey(), check);
        }
        // register any health check beans registered directly
        Map<String, HealthCheck> healthCheckBeans = event.getApplicationContext().getBeansOfType(HealthCheck.class);
        for(Entry<String, HealthCheck> e: healthCheckBeans.entrySet()) {
            LOG.info("health check " + e.getKey() + " " + e.getValue().getClass().getName());
            HealthCheck check = e.getValue();
            healthCheckRegistry.register(e.getKey(), check);
        }
    }
}

@github-actions
Copy link

github-actions bot commented Sep 6, 2019

This issue is stale because it has been open 180 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Sep 6, 2019
@jillesvangurp
Copy link

It seems this was resolved by #1289

@joschi joschi added this to the 5.0.0 milestone Sep 6, 2019
@joschi
Copy link
Member

joschi commented Sep 6, 2019

@jillesvangurp Thanks for finding the PR! 😄

@joschi joschi closed this as completed Sep 6, 2019
@joschi joschi removed the Stale label Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants