Created
August 25, 2018 05:25
-
-
Save mafr/cf352528e155d19f301f30ab575030fd to your computer and use it in GitHub Desktop.
Prometheus Health Check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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