diff --git a/alerting/config.go b/alerting/config.go index c19c394d..2bcc4db9 100644 --- a/alerting/config.go +++ b/alerting/config.go @@ -1,6 +1,9 @@ package alerting import ( + "fmt" + "reflect" + "github.com/TwiN/gatus/v5/alerting/alert" "github.com/TwiN/gatus/v5/alerting/provider" "github.com/TwiN/gatus/v5/alerting/provider/custom" @@ -154,3 +157,17 @@ func (config Config) GetAlertingProviderByAlertType(alertType alert.Type) provid } return nil } + +// SetAlertingProviderToNil Sets an alerting provider to nil to avoid having to revalidate it every time an +// alert of its corresponding type is sent. +func (config *Config) SetAlertingProviderToNil(p provider.AlertProvider) { + fmt.Println(config.GitHub) + entityType := reflect.TypeOf(config).Elem() + for i := 0; i < entityType.NumField(); i++ { + field := entityType.Field(i) + if field.Type == reflect.TypeOf(p) { + fmt.Println("Setting", field.Name, "to nil") + reflect.ValueOf(config).Elem().Field(i).Set(reflect.Zero(field.Type)) + } + } +} diff --git a/config/config.go b/config/config.go index 9040d936..91e5b8ba 100644 --- a/config/config.go +++ b/config/config.go @@ -323,6 +323,7 @@ func validateAlertingConfig(alertingConfig *alerting.Config, endpoints []*core.E } else { log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", alertType) invalidProviders = append(invalidProviders, alertType) + alertingConfig.SetAlertingProviderToNil(alertProvider) } } else { invalidProviders = append(invalidProviders, alertType) diff --git a/config/config_test.go b/config/config_test.go index df880f8d..8d8557d9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -980,11 +980,8 @@ endpoints: if config.Alerting == nil { t.Fatal("config.Alerting shouldn't have been nil") } - if config.Alerting.PagerDuty == nil { - t.Fatal("PagerDuty alerting config shouldn't have been nil") - } - if config.Alerting.PagerDuty.IsValid() { - t.Fatal("PagerDuty alerting config should've been invalid") + if config.Alerting.PagerDuty != nil { + t.Fatal("PagerDuty alerting config should've been set to nil, because its IsValid() method returned false and therefore alerting.Config.SetAlertingProviderToNil() should've been called") } } diff --git a/watchdog/alerting.go b/watchdog/alerting.go index d08f6b3c..881edbab 100644 --- a/watchdog/alerting.go +++ b/watchdog/alerting.go @@ -36,7 +36,7 @@ func handleAlertsToTrigger(endpoint *core.Endpoint, result *core.Result, alertin continue } alertProvider := alertingConfig.GetAlertingProviderByAlertType(endpointAlert.Type) - if alertProvider != nil && alertProvider.IsValid() { + if alertProvider != nil { log.Printf("[watchdog][handleAlertsToTrigger] Sending %s alert because alert for endpoint=%s with description='%s' has been TRIGGERED", endpointAlert.Type, endpoint.Name, endpointAlert.GetDescription()) var err error if os.Getenv("MOCK_ALERT_PROVIDER") == "true" { @@ -70,7 +70,7 @@ func handleAlertsToResolve(endpoint *core.Endpoint, result *core.Result, alertin continue } alertProvider := alertingConfig.GetAlertingProviderByAlertType(endpointAlert.Type) - if alertProvider != nil && alertProvider.IsValid() { + if alertProvider != nil { log.Printf("[watchdog][handleAlertsToResolve] Sending %s alert because alert for endpoint=%s with description='%s' has been RESOLVED", endpointAlert.Type, endpoint.Name, endpointAlert.GetDescription()) err := alertProvider.Send(endpoint, endpointAlert, result, true) if err != nil {