From d35dd325bec71f75dd8f2ced457957b493168e1c Mon Sep 17 00:00:00 2001 From: Brian Dwyer Date: Wed, 7 Jul 2021 13:27:48 -0400 Subject: [PATCH] Print the last response if we timeout --- main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/main.go b/main.go index 2b23209..a63b54b 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,13 @@ package main import ( + "bytes" "context" "crypto/tls" + "encoding/json" "flag" "fmt" + "io" "net" "net/http" "os" @@ -190,6 +193,8 @@ func waitUntilHealthy() { ctx, cancel := context.WithTimeout(context.Background(), healthcheckTimeout) defer cancel() + var bodyBytes []byte + for { req, err := http.NewRequestWithContext(ctx, "GET", healthcheckUrl, nil) if err != nil { @@ -201,6 +206,14 @@ func waitUntilHealthy() { resp, err := client.Do(req.WithContext(rctx)) if err != nil { if ctxErr := ctx.Err(); ctxErr == context.DeadlineExceeded { + if len(bodyBytes) > 0 { + var prettyJSON bytes.Buffer + if err := json.Indent(&prettyJSON, bodyBytes, "", " "); err != nil { + log.Error(string(bodyBytes)) + } else { + log.Error(prettyJSON.String()) + } + } log.Fatal(fmt.Errorf("healthcheck exceeded timeout(%s): %w", healthcheckTimeout, err)) } if ctxErr := rctx.Err(); ctxErr == context.DeadlineExceeded { @@ -217,6 +230,8 @@ func waitUntilHealthy() { return default: log.Warnf("%v :: (%v) %v", healthcheckUrl, resp.StatusCode, resp.Status) + bodyBytes, _ = io.ReadAll(resp.Body) + resp.Body.Close() time.Sleep(5 * time.Second) continue }