Reset
This commit is contained in:
128
alerting/alert/alert.go
Normal file
128
alerting/alert/alert.go
Normal file
@ -0,0 +1,128 @@
|
||||
package alert
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/TwiN/logr"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrAlertWithInvalidDescription is the error with which Gatus will panic if an alert has an invalid character
|
||||
ErrAlertWithInvalidDescription = errors.New("alert description must not have \" or \\")
|
||||
)
|
||||
|
||||
// Alert is endpoint.Endpoint's alert configuration
|
||||
type Alert struct {
|
||||
// Type of alert (required)
|
||||
Type Type `yaml:"type"`
|
||||
|
||||
// Enabled defines whether the alert is enabled
|
||||
//
|
||||
// Use Alert.IsEnabled() to retrieve the value of this field.
|
||||
//
|
||||
// This is a pointer, because it is populated by YAML and we need to know whether it was explicitly set to a value
|
||||
// or not for provider.ParseWithDefaultAlert to work.
|
||||
Enabled *bool `yaml:"enabled,omitempty"`
|
||||
|
||||
// FailureThreshold is the number of failures in a row needed before triggering the alert
|
||||
FailureThreshold int `yaml:"failure-threshold"`
|
||||
|
||||
// SuccessThreshold defines how many successful executions must happen in a row before an ongoing incident is marked as resolved
|
||||
SuccessThreshold int `yaml:"success-threshold"`
|
||||
|
||||
// Description of the alert. Will be included in the alert sent.
|
||||
//
|
||||
// This is a pointer, because it is populated by YAML and we need to know whether it was explicitly set to a value
|
||||
// or not for provider.ParseWithDefaultAlert to work.
|
||||
Description *string `yaml:"description,omitempty"`
|
||||
|
||||
// SendOnResolved defines whether to send a second notification when the issue has been resolved
|
||||
//
|
||||
// This is a pointer, because it is populated by YAML and we need to know whether it was explicitly set to a value
|
||||
// or not for provider.ParseWithDefaultAlert to work. Use Alert.IsSendingOnResolved() for a non-pointer
|
||||
SendOnResolved *bool `yaml:"send-on-resolved,omitempty"`
|
||||
|
||||
// ProviderOverride is an optional field that can be used to override the provider's configuration
|
||||
// It is freeform so that it can be used for any provider-specific configuration.
|
||||
ProviderOverride map[string]any `yaml:"provider-override,omitempty"`
|
||||
|
||||
// ResolveKey is an optional field that is used by some providers (i.e. PagerDuty's dedup_key) to resolve
|
||||
// ongoing/triggered incidents
|
||||
ResolveKey string `yaml:"-"`
|
||||
|
||||
// Triggered is used to determine whether an alert has been triggered. When an alert is resolved, this value
|
||||
// should be set back to false. It is used to prevent the same alert from going out twice.
|
||||
//
|
||||
// This value should only be modified if the provider.AlertProvider's Send function does not return an error for an
|
||||
// alert that hasn't been triggered yet. This doubles as a lazy retry. The reason why this behavior isn't also
|
||||
// applied for alerts that are already triggered and has become "healthy" again is to prevent a case where, for
|
||||
// some reason, the alert provider always returns errors when trying to send the resolved notification
|
||||
// (SendOnResolved).
|
||||
Triggered bool `yaml:"-"`
|
||||
}
|
||||
|
||||
// ValidateAndSetDefaults validates the alert's configuration and sets the default value of fields that have one
|
||||
func (alert *Alert) ValidateAndSetDefaults() error {
|
||||
if alert.FailureThreshold <= 0 {
|
||||
alert.FailureThreshold = 3
|
||||
}
|
||||
if alert.SuccessThreshold <= 0 {
|
||||
alert.SuccessThreshold = 2
|
||||
}
|
||||
if strings.ContainsAny(alert.GetDescription(), "\"\\") {
|
||||
return ErrAlertWithInvalidDescription
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDescription retrieves the description of the alert
|
||||
func (alert *Alert) GetDescription() string {
|
||||
if alert.Description == nil {
|
||||
return ""
|
||||
}
|
||||
return *alert.Description
|
||||
}
|
||||
|
||||
// IsEnabled returns whether an alert is enabled or not
|
||||
// Returns true if not set
|
||||
func (alert *Alert) IsEnabled() bool {
|
||||
if alert.Enabled == nil {
|
||||
return true
|
||||
}
|
||||
return *alert.Enabled
|
||||
}
|
||||
|
||||
// IsSendingOnResolved returns whether an alert is sending on resolve or not
|
||||
func (alert *Alert) IsSendingOnResolved() bool {
|
||||
if alert.SendOnResolved == nil {
|
||||
return false
|
||||
}
|
||||
return *alert.SendOnResolved
|
||||
}
|
||||
|
||||
// Checksum returns a checksum of the alert
|
||||
// Used to determine which persisted triggered alert should be deleted on application start
|
||||
func (alert *Alert) Checksum() string {
|
||||
hash := sha256.New()
|
||||
hash.Write([]byte(string(alert.Type) + "_" +
|
||||
strconv.FormatBool(alert.IsEnabled()) + "_" +
|
||||
strconv.FormatBool(alert.IsSendingOnResolved()) + "_" +
|
||||
strconv.Itoa(alert.SuccessThreshold) + "_" +
|
||||
strconv.Itoa(alert.FailureThreshold) + "_" +
|
||||
alert.GetDescription()),
|
||||
)
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
func (alert *Alert) ProviderOverrideAsBytes() []byte {
|
||||
yamlBytes, err := yaml.Marshal(alert.ProviderOverride)
|
||||
if err != nil {
|
||||
logr.Warnf("[alert.ProviderOverrideAsBytes] Failed to marshal alert override of type=%s as bytes: %v", alert.Type, err)
|
||||
}
|
||||
return yamlBytes
|
||||
}
|
||||
192
alerting/alert/alert_test.go
Normal file
192
alerting/alert/alert_test.go
Normal file
@ -0,0 +1,192 @@
|
||||
package alert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAlert_ValidateAndSetDefaults(t *testing.T) {
|
||||
invalidDescription := "\""
|
||||
scenarios := []struct {
|
||||
name string
|
||||
alert Alert
|
||||
expectedError error
|
||||
expectedSuccessThreshold int
|
||||
expectedFailureThreshold int
|
||||
}{
|
||||
{
|
||||
name: "valid-empty",
|
||||
alert: Alert{
|
||||
Description: nil,
|
||||
FailureThreshold: 0,
|
||||
SuccessThreshold: 0,
|
||||
},
|
||||
expectedError: nil,
|
||||
expectedFailureThreshold: 3,
|
||||
expectedSuccessThreshold: 2,
|
||||
},
|
||||
{
|
||||
name: "invalid-description",
|
||||
alert: Alert{
|
||||
Description: &invalidDescription,
|
||||
FailureThreshold: 10,
|
||||
SuccessThreshold: 5,
|
||||
},
|
||||
expectedError: ErrAlertWithInvalidDescription,
|
||||
expectedFailureThreshold: 10,
|
||||
expectedSuccessThreshold: 5,
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
if err := scenario.alert.ValidateAndSetDefaults(); !errors.Is(err, scenario.expectedError) {
|
||||
t.Errorf("expected error %v, got %v", scenario.expectedError, err)
|
||||
}
|
||||
if scenario.alert.SuccessThreshold != scenario.expectedSuccessThreshold {
|
||||
t.Errorf("expected success threshold %v, got %v", scenario.expectedSuccessThreshold, scenario.alert.SuccessThreshold)
|
||||
}
|
||||
if scenario.alert.FailureThreshold != scenario.expectedFailureThreshold {
|
||||
t.Errorf("expected failure threshold %v, got %v", scenario.expectedFailureThreshold, scenario.alert.FailureThreshold)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlert_IsEnabled(t *testing.T) {
|
||||
if !(&Alert{Enabled: nil}).IsEnabled() {
|
||||
t.Error("alert.IsEnabled() should've returned true, because Enabled was set to nil")
|
||||
}
|
||||
if value := false; (&Alert{Enabled: &value}).IsEnabled() {
|
||||
t.Error("alert.IsEnabled() should've returned false, because Enabled was set to false")
|
||||
}
|
||||
if value := true; !(&Alert{Enabled: &value}).IsEnabled() {
|
||||
t.Error("alert.IsEnabled() should've returned true, because Enabled was set to true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlert_GetDescription(t *testing.T) {
|
||||
if (&Alert{Description: nil}).GetDescription() != "" {
|
||||
t.Error("alert.GetDescription() should've returned an empty string, because Description was set to nil")
|
||||
}
|
||||
if value := "description"; (&Alert{Description: &value}).GetDescription() != value {
|
||||
t.Error("alert.GetDescription() should've returned false, because Description was set to 'description'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlert_IsSendingOnResolved(t *testing.T) {
|
||||
if (&Alert{SendOnResolved: nil}).IsSendingOnResolved() {
|
||||
t.Error("alert.IsSendingOnResolved() should've returned false, because SendOnResolved was set to nil")
|
||||
}
|
||||
if value := false; (&Alert{SendOnResolved: &value}).IsSendingOnResolved() {
|
||||
t.Error("alert.IsSendingOnResolved() should've returned false, because SendOnResolved was set to false")
|
||||
}
|
||||
if value := true; !(&Alert{SendOnResolved: &value}).IsSendingOnResolved() {
|
||||
t.Error("alert.IsSendingOnResolved() should've returned true, because SendOnResolved was set to true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlert_Checksum(t *testing.T) {
|
||||
description1, description2 := "a", "b"
|
||||
yes, no := true, false
|
||||
scenarios := []struct {
|
||||
name string
|
||||
alert Alert
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "barebone",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
},
|
||||
expected: "fed0580e44ed5701dbba73afa1f14b2c53ca5a7b8067a860441c212916057fe3",
|
||||
},
|
||||
{
|
||||
name: "with-description-1",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
Description: &description1,
|
||||
},
|
||||
expected: "005f407ebe506e74a4aeb46f74c28b376debead7011e1b085da3840f72ba9707",
|
||||
},
|
||||
{
|
||||
name: "with-description-2",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "3c2c4a9570cdc614006993c21f79a860a7f5afea10cf70d1a79d3c49342ef2c8",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-enabled-false",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
Enabled: &no,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "837945c2b4cd5e961db3e63e10c348d4f1c3446ba68cf5a48e35a1ae22cf0c22",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-enabled-true",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
Enabled: &yes, // it defaults to true if not set, but just to make sure
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "3c2c4a9570cdc614006993c21f79a860a7f5afea10cf70d1a79d3c49342ef2c8",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-enabled-true-and-send-on-resolved-true",
|
||||
alert: Alert{
|
||||
Type: TypeDiscord,
|
||||
Enabled: &yes,
|
||||
SendOnResolved: &yes,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "bf1436995a880eb4a352c74c5dfee1f1b5ff6b9fc55aef9bf411b3631adfd80c",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-failure-threshold-7",
|
||||
alert: Alert{
|
||||
Type: TypeSlack,
|
||||
FailureThreshold: 7,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "8bd479e18bda393d4c924f5a0d962e825002168dedaa88b445e435db7bacffd3",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-failure-threshold-9",
|
||||
alert: Alert{
|
||||
Type: TypeSlack,
|
||||
FailureThreshold: 9,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "5abdfce5236e344996d264d526e769c07cb0d3d329a999769a1ff84b157ca6f1",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-success-threshold-5",
|
||||
alert: Alert{
|
||||
Type: TypeSlack,
|
||||
SuccessThreshold: 7,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "c0000e73626b80e212cfc24830de7094568f648e37f3e16f9e68c7f8ef75c34c",
|
||||
},
|
||||
{
|
||||
name: "with-description-2-and-success-threshold-1",
|
||||
alert: Alert{
|
||||
Type: TypeSlack,
|
||||
SuccessThreshold: 1,
|
||||
Description: &description2,
|
||||
},
|
||||
expected: "5c28963b3a76104cfa4a0d79c89dd29ec596c8cfa4b1af210ec83d6d41587b5f",
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
scenario.alert.ValidateAndSetDefaults()
|
||||
if checksum := scenario.alert.Checksum(); checksum != scenario.expected {
|
||||
t.Errorf("expected checksum %v, got %v", scenario.expected, checksum)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
79
alerting/alert/type.go
Normal file
79
alerting/alert/type.go
Normal file
@ -0,0 +1,79 @@
|
||||
package alert
|
||||
|
||||
// Type is the type of the alert.
|
||||
// The value will generally be the name of the alert provider
|
||||
type Type string
|
||||
|
||||
const (
|
||||
// TypeAWSSES is the Type for the awsses alerting provider
|
||||
TypeAWSSES Type = "aws-ses"
|
||||
|
||||
// TypeCustom is the Type for the custom alerting provider
|
||||
TypeCustom Type = "custom"
|
||||
|
||||
// TypeDiscord is the Type for the discord alerting provider
|
||||
TypeDiscord Type = "discord"
|
||||
|
||||
// TypeEmail is the Type for the email alerting provider
|
||||
TypeEmail Type = "email"
|
||||
|
||||
// TypeGitHub is the Type for the github alerting provider
|
||||
TypeGitHub Type = "github"
|
||||
|
||||
// TypeGitLab is the Type for the gitlab alerting provider
|
||||
TypeGitLab Type = "gitlab"
|
||||
|
||||
// TypeGitea is the Type for the gitea alerting provider
|
||||
TypeGitea Type = "gitea"
|
||||
|
||||
// TypeGoogleChat is the Type for the googlechat alerting provider
|
||||
TypeGoogleChat Type = "googlechat"
|
||||
|
||||
// TypeGotify is the Type for the gotify alerting provider
|
||||
TypeGotify Type = "gotify"
|
||||
|
||||
// TypeIncidentIO is the Type for the incident-io alerting provider
|
||||
TypeIncidentIO Type = "incident-io"
|
||||
|
||||
// TypeJetBrainsSpace is the Type for the jetbrains alerting provider
|
||||
TypeJetBrainsSpace Type = "jetbrainsspace"
|
||||
|
||||
// TypeMatrix is the Type for the matrix alerting provider
|
||||
TypeMatrix Type = "matrix"
|
||||
|
||||
// TypeMattermost is the Type for the mattermost alerting provider
|
||||
TypeMattermost Type = "mattermost"
|
||||
|
||||
// TypeMessagebird is the Type for the messagebird alerting provider
|
||||
TypeMessagebird Type = "messagebird"
|
||||
|
||||
// TypeNtfy is the Type for the ntfy alerting provider
|
||||
TypeNtfy Type = "ntfy"
|
||||
|
||||
// TypeOpsgenie is the Type for the opsgenie alerting provider
|
||||
TypeOpsgenie Type = "opsgenie"
|
||||
|
||||
// TypePagerDuty is the Type for the pagerduty alerting provider
|
||||
TypePagerDuty Type = "pagerduty"
|
||||
|
||||
// TypePushover is the Type for the pushover alerting provider
|
||||
TypePushover Type = "pushover"
|
||||
|
||||
// TypeSlack is the Type for the slack alerting provider
|
||||
TypeSlack Type = "slack"
|
||||
|
||||
// TypeTeams is the Type for the teams alerting provider
|
||||
TypeTeams Type = "teams"
|
||||
|
||||
// TypeTeamsWorkflows is the Type for the teams-workflows alerting provider
|
||||
TypeTeamsWorkflows Type = "teams-workflows"
|
||||
|
||||
// TypeTelegram is the Type for the telegram alerting provider
|
||||
TypeTelegram Type = "telegram"
|
||||
|
||||
// TypeTwilio is the Type for the twilio alerting provider
|
||||
TypeTwilio Type = "twilio"
|
||||
|
||||
// TypeZulip is the Type for the Zulip alerting provider
|
||||
TypeZulip Type = "zulip"
|
||||
)
|
||||
Reference in New Issue
Block a user