@@ -2,6 +2,7 @@ package health
22
33import (
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+
1518type 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