Skip to content

Commit 024be30

Browse files
committed
[health] minor fixes
1 parent fa41d3e commit 024be30

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

Dockerfile.goreleaser

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ USER guest
2121
ENTRYPOINT ["/docker-entrypoint.sh"]
2222

2323
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
24-
CMD [ "/app/app" "health" ]
24+
CMD [ "/app/app", "health" ]
2525

2626
CMD [ "/app/app" ]
2727

build/package/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ USER guest
4747
ENTRYPOINT ["/docker-entrypoint.sh"]
4848

4949
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
50-
CMD [ "/app/app" "health" ]
50+
CMD [ "/app/app", "health" ]
5151

5252
CMD [ "/app/app" ]

internal/health/checker.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package health
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
httpclient "net/http"
@@ -12,6 +13,8 @@ import (
1213
"go.uber.org/zap"
1314
)
1415

16+
var ErrNotHealthy = errors.New("not healthy")
17+
1518
type Checker struct {
1619
config http.Config
1720

@@ -33,7 +36,12 @@ func (c *Checker) Execute(ctx context.Context) error {
3336

3437
client := httpclient.DefaultClient
3538

36-
req, err := httpclient.NewRequestWithContext(ctx, httpclient.MethodGet, "http://"+c.config.Listen+"/health/live", nil)
39+
req, err := httpclient.NewRequestWithContext(
40+
ctx,
41+
httpclient.MethodGet,
42+
"http://"+c.config.Listen+"/health/live",
43+
nil,
44+
)
3745
if err != nil {
3846
return fmt.Errorf("failed to create request: %w", err)
3947
}
@@ -46,17 +54,20 @@ func (c *Checker) Execute(ctx context.Context) error {
4654

4755
body, err := io.ReadAll(res.Body)
4856
if err != nil {
49-
c.logger.Error("failed to read body", zap.Error(err))
57+
return fmt.Errorf("failed to read response body: %w", err)
5058
}
5159

5260
c.logger.Info(string(body))
5361

54-
if res.StatusCode >= 400 {
55-
return fmt.Errorf("health check failed: %s", string(body))
62+
if res.StatusCode >= httpclient.StatusBadRequest {
63+
c.logger.Error("health check failed", zap.Int("status", res.StatusCode), zap.String("body", string(body)))
64+
return fmt.Errorf("%w: health check failed: %s", ErrNotHealthy, string(body))
5665
}
5766

58-
if err := c.shutdowner.Shutdown(); err != nil {
59-
c.logger.Error("failed to shutdown", zap.Error(err))
67+
c.logger.Info("health check passed", zap.Int("status", res.StatusCode))
68+
69+
if shErr := c.shutdowner.Shutdown(); shErr != nil {
70+
c.logger.Error("failed to shutdown", zap.Error(shErr))
6071
}
6172

6273
return nil

0 commit comments

Comments
 (0)