Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prometheus Health Check
package de.mafr.demo.prometheus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import io.micrometer.prometheus.PrometheusMeterRegistry;
@SpringBootApplication
public class PrometheusDemoApplication {
@Autowired
private HealthEndpoint healthEndpoint;
@Bean MeterRegistryCustomizer prometheusHealthCheck() {
return registry -> registry.gauge("health", healthEndpoint, ep -> healthToCode(ep));
}
private static int healthToCode(HealthEndpoint ep) {
Status status = ep.health().getStatus();
return status.equals(Status.UP) ? 1 : 0;
}
public static void main(String[] args) {
SpringApplication.run(PrometheusDemoApplication.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.