notification levels

pull/107/head
Karolis Rusenas 2017-10-03 20:21:50 +01:00
parent d837d7cda9
commit 773850b876
3 changed files with 22 additions and 0 deletions

View File

@ -15,3 +15,6 @@ const (
EnvSlackBotName = "SLACK_BOT_NAME"
EnvSlackChannels = "SLACK_CHANNELS"
)
// EnvNotificationLevel - minimum level for notifications, defaults to info
const EnvNotificationLevel = "NOTIFICATION_LEVEL"

View File

@ -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()

13
main.go
View File

@ -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 := &notification.Config{
Attempts: 10,
Level: notificationLevel,
}
sender := notification.New(ctx)