-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various improvements following real-world usage. #4
Conversation
- Capture a previously missed error - Introduce a debug env var
Merging now so I can use in package-kafka. Feedback welcome as always. |
@@ -48,7 +48,8 @@ func runHealthChecker(cliContext *cli.Context) error { | |||
} | |||
|
|||
opts, err := parseOptions(cliContext) | |||
if isSimpleError(err) { | |||
if isDebugMode() { | |||
opts.Logger.Infof("Note: To enable debug mode, set %s to \"true\"", ENV_VAR_NAME_DEBUG_MODE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by this. Why do you return err
right away in debug mode? And tell the user to enable debug mode?
err := writeHttpResponse(w, resp) | ||
if err != nil { | ||
opts.Logger.Error("Failed to send HTTP response. Exiting.") | ||
panic(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why exit the health check entirely because of this?
err := attemptTcpConnection(port, opts) | ||
if err != nil { | ||
logger.Warnf("TCP connection to port %d FAILED: %s", port, err) | ||
allPortsValid = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not safe to do in a concurrent environment! You're modifying shared state from multiple different goroutines at the same time.
Use an integer (initialized to 0) instead with an atomic increment operation on errors. If it ends up greater than 0, at least one health check failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, there should be tests for all of this.
@@ -45,21 +72,23 @@ func attemptTcpConnection(port int, opts *options.Options) error { | |||
logger := opts.Logger | |||
logger.Infof("Attempting to connect to port %d via TCP...", port) | |||
|
|||
_, err := net.Dial("tcp", fmt.Sprintf("0.0.0.0:%d", port)) | |||
defaultTimeout := time.Second * 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be less than the total configured timeout for all the health checks?
w.Write([]byte("OK")) | ||
} | ||
func writeHttpResponse(w http.ResponseWriter, resp *httpResponse) error { | ||
w.WriteHeader(resp.StatusCode) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't this return an error?
stdout
, notstderr
.HEALTH_CHECKER_DEBUG
. When set totrue
, health-checker will show the full stack trace on most error messages.