diff --git a/constants/constants.go b/constants/constants.go index e18be1da..dce3de1a 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -15,3 +15,6 @@ const ( EnvSlackBotName = "SLACK_BOT_NAME" EnvSlackChannels = "SLACK_CHANNELS" ) + +// EnvNotificationLevel - minimum level for notifications, defaults to info +const EnvNotificationLevel = "NOTIFICATION_LEVEL" diff --git a/extension/notification/notification.go b/extension/notification/notification.go index a41dfce0..e40fd121 100644 --- a/extension/notification/notification.go +++ b/extension/notification/notification.go @@ -32,6 +32,7 @@ var ( // notifiers. type Config struct { Attempts int + Level types.Level Params map[string]interface{} `yaml:",inline"` } @@ -76,6 +77,7 @@ func RegisterSender(name string, s Sender) { type DefaultNotificationSender struct { config *Config stopper *stopper.Stopper + level types.Level } // New - create new sender @@ -118,6 +120,10 @@ func (m *DefaultNotificationSender) Senders() map[string]Sender { // Send - send notifications through all configured senders func (m *DefaultNotificationSender) Send(event types.EventNotification) error { + if event.Level < m.config.Level { + return nil + } + sendersM.RLock() defer sendersM.RUnlock() diff --git a/main.go b/main.go index cf89196e..00766328 100644 --- a/main.go +++ b/main.go @@ -74,8 +74,21 @@ func main() { ctx, cancel := netContext.WithCancel(context.Background()) defer cancel() + notificationLevel := types.LevelInfo + if os.Getenv(constants.EnvNotificationLevel) != "" { + parsedLevel, err := types.ParseLevel(os.Getenv(constants.EnvNotificationLevel)) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + }).Errorf("main: got error while parsing notification level, defaulting to: %s", notificationLevel) + } else { + notificationLevel = parsedLevel + } + } + notifCfg := ¬ification.Config{ Attempts: 10, + Level: notificationLevel, } sender := notification.New(ctx)