Allow sending error message on failure

This commit is contained in:
Melora Hugues 2024-10-03 15:26:03 +02:00
parent 24c3d5c5b2
commit 97f47d0274
2 changed files with 16 additions and 4 deletions

View file

@ -12,10 +12,17 @@ var failureCmd = &cobra.Command{
Use: "failure", Use: "failure",
Short: "Signal the check has failed", Short: "Signal the check has failed",
Long: ``, Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
check := parseCheckFlags() check := parseCheckFlags()
msg := ""
if len(args) > 0 {
msg = args[0]
}
clt := gohealthchecks.NewPingClient(pingHost) clt := gohealthchecks.NewPingClient(pingHost)
err := clt.ReportFailure(cmd.Context(), check) err := clt.ReportFailure(cmd.Context(), check, msg)
if err != nil { if err != nil {
failf("Failed to notify failure: %s\n", err) failf("Failed to notify failure: %s\n", err)
} else { } else {

View file

@ -44,7 +44,7 @@ func addQueryParams(reqURL *url.URL, check Check) {
type PingClient interface { type PingClient interface {
ReportSuccess(ctx context.Context, check Check) error ReportSuccess(ctx context.Context, check Check) error
ReportStart(ctx context.Context, check Check) error ReportStart(ctx context.Context, check Check) error
ReportFailure(ctx context.Context, check Check) error ReportFailure(ctx context.Context, check Check, msg string) error
LogMessage(ctx context.Context, check Check, log []byte) error LogMessage(ctx context.Context, check Check, log []byte) error
ReportExitCode(ctx context.Context, check Check, exitCode int) error ReportExitCode(ctx context.Context, check Check, exitCode int) error
} }
@ -91,13 +91,18 @@ func (c *pingClient) ReportStart(ctx context.Context, check Check) error {
} }
return handleResponse(resp) return handleResponse(resp)
} }
func (c *pingClient) ReportFailure(ctx context.Context, check Check) error { func (c *pingClient) ReportFailure(ctx context.Context, check Check, msg string) error {
url, err := url.JoinPath(c.host, check.path(), "fail") url, err := url.JoinPath(c.host, check.path(), "fail")
if err != nil { if err != nil {
return fmt.Errorf("invalid check or hostname provided: %w", err) return fmt.Errorf("invalid check or hostname provided: %w", err)
} }
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) var body io.Reader
if msg != "" {
body = bytes.NewBufferString(msg + "\n")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil { if err != nil {
return fmt.Errorf("failed to build ping request: %w", err) return fmt.Errorf("failed to build ping request: %w", err)
} }