diff --git a/Dockerfile b/Dockerfile index 2e552a7..81a8263 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,9 +36,6 @@ RUN CGO_ENABLED=0 go build \ -X main.BuildDate=$(date -u ""+%Y%m%d-%H:%M:%S"")" \ -o /app . -# Second stage: get statically linked curl -FROM wmark/curl:latest as curl - # Final stage: the running container. FROM scratch AS final @@ -54,9 +51,6 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ # Import the compiled executable from the first stage. COPY --from=builder /app /app -# Get curl from image -COPY --from=curl /usr/bin/curl /usr/bin/curl - # Declare the port on which the webserver will be exposed. # As we're going to run the executable as an unprivileged user, we can't bind # to ports below 1024. @@ -66,6 +60,6 @@ EXPOSE 9419 USER nobody:nobody # Check if exporter is alive; 10 retries gives prometheus some time to retrieve bad data (5 minutes) -HEALTHCHECK --retries=10 CMD "/usr/bin/curl -s -f http://localhost:9419/health || exit 1"] +HEALTHCHECK --retries=10 CMD ["/app", "-check-url", "http://localhost:9419/health"] # Run the compiled binary. ENTRYPOINT ["/app"] diff --git a/curl.go b/curl.go new file mode 100644 index 0000000..bc39dd1 --- /dev/null +++ b/curl.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "time" +) + +// curl like check for an http url. +// used inside docker image for checking health endpoint +// will os.Exit(0) for http response code 200. Otherwise os.Exit(1) +func curl(url string) { + var client = &http.Client{Timeout: 30 * time.Second} + resp, err := client.Get(url) + if err != nil { + fmt.Printf("Error checking url: %v\n", err) + os.Exit(1) + } + if resp.StatusCode != http.StatusOK { + fmt.Printf("Error checking url: Unexpected http code %v\n", resp.StatusCode) + os.Exit(1) + } + os.Exit(0) +} diff --git a/exporter.go b/exporter.go index 846713d..5f4011d 100644 --- a/exporter.go +++ b/exporter.go @@ -49,7 +49,6 @@ type exporter struct { type Exporter interface { Collect(ctx context.Context, ch chan<- prometheus.Metric) error Describe(ch chan<- *prometheus.Desc) - LastScrapeOK() bool } func newExporter() *exporter { diff --git a/main.go b/main.go index d493b9e..85aacea 100644 --- a/main.go +++ b/main.go @@ -31,10 +31,15 @@ func initLogger() { } func main() { - + var checkURL = flag.String("check-url", "", "Curl url and return exit code (http: 200 => 0, otherwise 1)") var configFile = flag.String("config-file", "conf/rabbitmq.conf", "path to json config") flag.Parse() + if *checkURL != "" { // do a single http get request. Used in docker healthckecks as curl is not inside the image + curl(*checkURL) + return + } + err := initConfigFromFile(*configFile) //Try parsing config file if _, isPathError := err.(*os.PathError); isPathError { // No file => use environment variables initConfig()