-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
feat: initial support for healthchecks.io #480
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,80 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
|
||
v1 "github.com/garethgeorge/backrest/gen/go/v1" | ||
"github.com/garethgeorge/backrest/internal/hook/hookutil" | ||
"github.com/garethgeorge/backrest/internal/orchestrator/tasks" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type healthchecksHandler struct{} | ||
|
||
func (healthchecksHandler) Name() string { | ||
return "healthchecks" | ||
} | ||
|
||
func (healthchecksHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, runner tasks.TaskRunner, event v1.Hook_Condition) error { | ||
payload, err := hookutil.RenderTemplateOrDefault(cmd.GetActionHealthchecks().GetTemplate(), hookutil.DefaultTemplate, vars) | ||
if err != nil { | ||
return fmt.Errorf("template rendering: %w", err) | ||
} | ||
|
||
l := runner.Logger(ctx) | ||
l.Sugar().Infof("Sending healthchecks message to %s", cmd.GetActionHealthchecks().GetWebhookUrl()) | ||
l.Debug("Sending healthchecks message", zap.String("payload", payload)) | ||
|
||
PingUrl := cmd.GetActionHealthchecks().GetWebhookUrl() | ||
|
||
// Send a "start" signal to healthchecks.io when the hook is starting. | ||
if event == v1.Hook_CONDITION_CHECK_START || | ||
event == v1.Hook_CONDITION_PRUNE_START || | ||
event == v1.Hook_CONDITION_SNAPSHOT_START { | ||
PingUrl += "/start" | ||
} | ||
|
||
// Send a "fail" signal to healthchecks.io when the hook is failing. | ||
if event == v1.Hook_CONDITION_UNKNOWN || | ||
event == v1.Hook_CONDITION_ANY_ERROR || | ||
event == v1.Hook_CONDITION_CHECK_ERROR || | ||
event == v1.Hook_CONDITION_PRUNE_ERROR || | ||
event == v1.Hook_CONDITION_SNAPSHOT_ERROR { | ||
PingUrl += "/fail" | ||
} | ||
|
||
// Send a "log" signal to healthchecks.io when the hook is ending. | ||
if event == v1.Hook_CONDITION_SNAPSHOT_END { | ||
PingUrl += "/log" | ||
} | ||
|
||
type Message struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
request := Message{ | ||
Text: payload, | ||
} | ||
|
||
requestBytes, _ := json.Marshal(request) | ||
|
||
body, err := hookutil.PostRequest(PingUrl, "application/json", bytes.NewReader(requestBytes)) | ||
if err != nil { | ||
return fmt.Errorf("sending healthchecks message to %q: %w", PingUrl, err) | ||
} | ||
|
||
l.Debug("Healthchecks response", zap.String("body", body)) | ||
return nil | ||
} | ||
|
||
func (healthchecksHandler) ActionType() reflect.Type { | ||
return reflect.TypeOf(&v1.Hook_ActionHealthchecks{}) | ||
} | ||
|
||
func init() { | ||
DefaultRegistry().RegisterHandler(&healthchecksHandler{}) | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
perhaps my only nit is it may be worth declaring some utilities or constants for this e.g. isStartCondition, isErrorCondition, isSuccessCondition so that we can be consistent about this going forward.
These blocks probably also should have handling for
CONDITION_SNAPSHOT_SUCCESS
,CONDITION_PRUNE_SUCCESS
, andCONDITION_CHECK_SUCCESS
This is something that could be added to protoutil e.g. protoutil/conditions.go . It would be good to think about how to add a test that asserts that the functions are updated whenever the enum definition changes, but that's also something I can followup with :)
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.
Yes, I already thought about it,
I can create an array with the constants and the function or, a function only using switch like this example
What do you see better?
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 think a couple of arrays and
slices.Contains
as the check would be a good way to define this.The test I'm thinking of would look somewhat similar to https://github.com/garethgeorge/backrest/blob/main/internal/hook/hook_test.go#L10-L16 e.g. just assert that each enum value exists in at least one of the declared arrays categorizing them.
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've been looking for a more elegant solution, see what you think.
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.
Dictionary approach looks good to me! Great work & thanks for adding this hook. I think it's a very intuitive behavior.