This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: introduce health check feature (#49)
Signed-off-by: Mateusz Urbanek <mateusz.urbanek.98@gmail.com>
- Loading branch information
Showing
9 changed files
with
299 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package gocosi | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrNilMux = errors.New("nil mux") | ||
ErrHealthcheckStatusUnknown = errors.New("healthcheck status unknown") | ||
) | ||
|
||
type ErrHealthCheckFailure struct { | ||
failures map[string]string | ||
} | ||
|
||
var _ error = (*ErrHealthCheckFailure)(nil) | ||
|
||
func (err *ErrHealthCheckFailure) Error() string { | ||
reasons := []string{} | ||
|
||
for service, reason := range err.failures { | ||
reasons = append(reasons, fmt.Sprintf("%s (reason: '%s')", service, reason)) | ||
} | ||
|
||
return "healthcheck failed: " + strings.Join(reasons, ", ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gocosi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hellofresh/health-go/v5" | ||
) | ||
|
||
const ( | ||
// HealthcheckEndpoint is the HTTP endpoint path for the healthcheck service. | ||
HealthcheckEndpoint = "/healthz" | ||
|
||
// HealthcheckAddr. | ||
HealthcheckAddr = "http://localhost:8080" + HealthcheckEndpoint | ||
) | ||
|
||
// HealthcheckFunc. | ||
func HealthcheckFunc(ctx context.Context, addr string) error { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addr, nil) | ||
if err != nil { | ||
return fmt.Errorf("unable to create new request: %w", err) | ||
} | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("healthcheck failed: %w", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
c := &health.Check{} | ||
|
||
err = json.NewDecoder(res.Body).Decode(c) | ||
if err != nil { | ||
return fmt.Errorf("unable to decode healthcheck response: %w", err) | ||
} | ||
|
||
log.Info("healthcheck finished", | ||
"status", c.Status, | ||
"system", c.System, | ||
"failures", c.Failures, | ||
"component", c.Component, | ||
) | ||
|
||
switch res.StatusCode { | ||
case http.StatusOK: | ||
return nil | ||
|
||
case http.StatusServiceUnavailable: | ||
return &ErrHealthCheckFailure{failures: c.Failures} | ||
|
||
default: | ||
return ErrHealthcheckStatusUnknown | ||
} | ||
} |
Oops, something went wrong.