Skip to content

Instantly share code, notes, and snippets.

@mafr
Created August 25, 2018 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafr/cf352528e155d19f301f30ab575030fd to your computer and use it in GitHub Desktop.
Save mafr/cf352528e155d19f301f30ab575030fd to your computer and use it in GitHub Desktop.
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