Skip to content

Commit

Permalink
added healthcheck
Browse files Browse the repository at this point in the history
implemented flag for http checking health endpoint
  • Loading branch information
kbudde committed Feb 4, 2020
1 parent e38d7b8 commit 8c5eb08
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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"]
25 changes: 25 additions & 0 deletions curl.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 0 additions & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8c5eb08

Please sign in to comment.