Skip to content

Commit

Permalink
fix(senders): fix text in sms and email (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored Jul 31, 2023
1 parent 6f19376 commit dc433ea
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 27 deletions.
36 changes: 32 additions & 4 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/moira-alert/moira/templating"
)

Expand All @@ -28,6 +29,14 @@ const (
const (
format = "15:04 02.01.2006"
remindMessage = "This metric has been in bad state for more than %v hours - please, fix."
limit = 1000
)

type NotificationEventSettings int

const (
DefaultNotificationSettings NotificationEventSettings = iota
SIFormatNumbers
)

// NotificationEvent represents trigger state changes event
Expand Down Expand Up @@ -409,7 +418,7 @@ func (notification *ScheduledNotification) GetKey() string {
notification.Event.Metric,
notification.Event.State,
notification.Event.Timestamp,
notification.Event.GetMetricsValues(),
notification.Event.GetMetricsValues(DefaultNotificationSettings),
notification.SendFail,
notification.Throttled,
notification.Timestamp,
Expand Down Expand Up @@ -447,32 +456,51 @@ func (schedule *ScheduleData) IsScheduleAllows(ts int64) bool {
}

func (event NotificationEvent) String() string {
return fmt.Sprintf("TriggerId: %s, Metric: %s, Values: %s, OldState: %s, State: %s, Message: '%s', Timestamp: %v", event.TriggerID, event.Metric, event.GetMetricsValues(), event.OldState, event.State, event.CreateMessage(nil), event.Timestamp)
return fmt.Sprintf("TriggerId: %s, Metric: %s, Values: %s, OldState: %s, State: %s, Message: '%s', Timestamp: %v", event.TriggerID, event.Metric, event.GetMetricsValues(DefaultNotificationSettings), event.OldState, event.State, event.CreateMessage(nil), event.Timestamp)
}

// GetMetricsValues gets event metric value and format it to human readable presentation
func (event NotificationEvent) GetMetricsValues() string {
var targetNames []string //nolint
func (event NotificationEvent) GetMetricsValues(settings NotificationEventSettings) string {
targetNames := make([]string, 0, len(event.Values))
for targetName := range event.Values {
targetNames = append(targetNames, targetName)
}

if len(targetNames) == 0 {
return "—"
}

if len(targetNames) == 1 {
switch settings {
case SIFormatNumbers:
if event.Values[targetNames[0]] >= limit {
return humanize.SIWithDigits(event.Values[targetNames[0]], 3, "")
}
return humanize.FtoaWithDigits(event.Values[targetNames[0]], 3)
}
return strconv.FormatFloat(event.Values[targetNames[0]], 'f', -1, 64)
}

var builder strings.Builder
sort.Strings(targetNames)
for i, targetName := range targetNames {
builder.WriteString(targetName)
builder.WriteString(": ")
value := strconv.FormatFloat(event.Values[targetName], 'f', -1, 64)
switch settings {
case SIFormatNumbers:
if event.Values[targetName] >= limit {
value = humanize.SIWithDigits(event.Values[targetName], 3, "")
} else {
value = humanize.FtoaWithDigits(event.Values[targetName], 3)
}
}
builder.WriteString(value)
if i < len(targetNames)-1 {
builder.WriteString(", ")
}
}

return builder.String()
}

Expand Down
21 changes: 17 additions & 4 deletions datatypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,37 @@ func TestNotificationEvent_GetValue(t *testing.T) {
Convey("Test GetMetricsValues", t, func() {
event := NotificationEvent{}
event.Values = make(map[string]float64)

Convey("One target with zero", func() {
event.Values["t1"] = 0
So(event.GetMetricsValues(), ShouldResemble, "0")
So(event.GetMetricsValues(DefaultNotificationSettings), ShouldResemble, "0")
})

Convey("One target with short fraction", func() {
event.Values["t1"] = 2.32
So(event.GetMetricsValues(), ShouldResemble, "2.32")
So(event.GetMetricsValues(DefaultNotificationSettings), ShouldResemble, "2.32")
})

Convey("One target with long fraction", func() {
event.Values["t1"] = 2.3222222
So(event.GetMetricsValues(), ShouldResemble, "2.3222222")
So(event.GetMetricsValues(DefaultNotificationSettings), ShouldResemble, "2.3222222")
})

Convey("Two targets", func() {
event.Values["t2"] = 0.12
event.Values["t1"] = 2.3222222
So(event.GetMetricsValues(), ShouldResemble, "t1: 2.3222222, t2: 0.12")
So(event.GetMetricsValues(DefaultNotificationSettings), ShouldResemble, "t1: 2.3222222, t2: 0.12")
})

Convey("One target over 1000 with SIFormatNumbers enum value", func() {
event.Values["t1"] = 1110.15
So(event.GetMetricsValues(SIFormatNumbers), ShouldResemble, "1.11 k")
})

Convey("Two targets lower 1000 with SIFormatNumbers enum value", func() {
event.Values["t1"] = 111.15
event.Values["t2"] = 54.5
So(event.GetMetricsValues(SIFormatNumbers), ShouldResemble, "t1: 111.15, t2: 54.5")
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion notifier/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (worker *FetchEventsWorker) processEvent(event moira.NotificationEvent) err
)
if event.State != moira.StateTEST {
log.Debug().
String("metric", fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())).
String("metric", fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))).
String("old_state", event.OldState.String()).
String("new_state", event.State.String()).
Msg("Processing trigger for metric")
Expand Down
14 changes: 7 additions & 7 deletions notifier/events/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestDisabledNotification(t *testing.T) {
logger.EXPECT().String(gomock.Any(), gomock.Any()).Return(logger).AnyTimes()
logger.EXPECT().Debug().Return(eventBuilder).AnyTimes()

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestSubscriptionsManagedToIgnoreEvents(t *testing.T) {
dataBase.EXPECT().GetTagsSubscriptions(triggerData.Tags).Times(1).
Return([]*moira.SubscriptionData{&subscriptionToIgnoreWarnings}, nil)

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestSubscriptionsManagedToIgnoreEvents(t *testing.T) {
dataBase.EXPECT().GetTagsSubscriptions(triggerData.Tags).Times(1).
Return([]*moira.SubscriptionData{&subscriptionToIgnoreWarnings}, nil)

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestSubscriptionsManagedToIgnoreEvents(t *testing.T) {
}
dataBase.EXPECT().GetTagsSubscriptions(triggerData.Tags).Times(1).Return([]*moira.SubscriptionData{&subscriptionToIgnoreWarningsAndRecoverings}, nil)

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestFailReadContact(t *testing.T) {
logger.EXPECT().String(gomock.Any(), gomock.Any()).Return(logger).AnyTimes()
logger.EXPECT().Debug().Return(eventBuilder).AnyTimes()

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -464,7 +464,7 @@ func TestEmptySubscriptions(t *testing.T) {
dataBase.EXPECT().GetTrigger(event.TriggerID).Return(trigger, nil)
dataBase.EXPECT().GetTagsSubscriptions(triggerData.Tags).Times(1).Return([]*moira.SubscriptionData{{ThrottlingEnabled: true}}, nil)

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down Expand Up @@ -499,7 +499,7 @@ func TestEmptySubscriptions(t *testing.T) {
dataBase.EXPECT().GetTrigger(event.TriggerID).Return(trigger, nil)
dataBase.EXPECT().GetTagsSubscriptions(triggerData.Tags).Times(1).Return([]*moira.SubscriptionData{nil}, nil)

metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues())
metricString := fmt.Sprintf("%s == %s", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings))
eventBuilder.EXPECT().String("metric", metricString).Return(eventBuilder)
eventBuilder.EXPECT().String("old_state", event.OldState.String()).Return(eventBuilder)
eventBuilder.EXPECT().String("new_state", event.State.String()).Return(eventBuilder)
Expand Down
2 changes: 1 addition & 1 deletion senders/discord/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
eventsLenLimitReached := false
eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down
2 changes: 1 addition & 1 deletion senders/mail/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (sender *Sender) makeMessage(events moira.NotificationEvents, contact moira
Timestamp: time.Unix(event.Timestamp, 0).In(sender.location).Format(sender.dateTimeFormat),
Oldstate: event.OldState,
State: event.State,
Values: event.GetMetricsValues(),
Values: event.GetMetricsValues(moira.DefaultNotificationSettings),
WarnValue: strconv.FormatFloat(trigger.WarnValue, 'f', -1, 64),
ErrorValue: strconv.FormatFloat(trigger.ErrorValue, 'f', -1, 64),
Message: event.CreateMessage(sender.location),
Expand Down
2 changes: 1 addition & 1 deletion senders/mattermost/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
eventsLenLimitReached := false
eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down
2 changes: 1 addition & 1 deletion senders/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (sender *Sender) buildEventsFacts(events moira.NotificationEvents, maxEvent

eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("%s = %s (%s to %s)", event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("%s = %s (%s to %s)", event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if len(moira.UseString(event.Message)) > 0 {
line += fmt.Sprintf(". %s", moira.UseString(event.Message))
}
Expand Down
2 changes: 1 addition & 1 deletion senders/opsgenie/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
eventsLenLimitReached := false
eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s\n", msg)
} else {
Expand Down
2 changes: 1 addition & 1 deletion senders/pagerduty/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (sender *Sender) buildEvent(events moira.NotificationEvents, contact moira.
var eventList string

for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down
2 changes: 1 addition & 1 deletion senders/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (sender *Sender) buildMessage(events moira.NotificationEvents, throttled bo
if i > printEventsCount-1 {
break
}
message.WriteString(fmt.Sprintf("%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State))
message.WriteString(fmt.Sprintf("%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State))
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
message.WriteString(fmt.Sprintf(". %s\n", msg))
} else {
Expand Down
2 changes: 1 addition & 1 deletion senders/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
eventsLenLimitReached := false
eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down
2 changes: 1 addition & 1 deletion senders/telegram/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (sender *Sender) buildMessage(events moira.NotificationEvents, trigger moir
messageLimitReached := false

for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down
2 changes: 1 addition & 1 deletion senders/twilio/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (sender *twilioSenderSms) buildMessage(events moira.NotificationEvents, tri
if i > printEventsCount-1 {
break
}
message.WriteString(fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State))
message.WriteString(fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State))
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
message.WriteString(fmt.Sprintf(". %s", msg))
}
Expand Down
2 changes: 1 addition & 1 deletion senders/victorops/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
eventsLenLimitReached := false
eventsPrinted := 0
for _, event := range events {
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(), event.OldState, event.State)
line := fmt.Sprintf("\n%s: %s = %s (%s to %s)", event.FormatTimestamp(sender.location), event.Metric, event.GetMetricsValues(moira.DefaultNotificationSettings), event.OldState, event.State)
if msg := event.CreateMessage(sender.location); len(msg) > 0 {
line += fmt.Sprintf(". %s", msg)
}
Expand Down

0 comments on commit dc433ea

Please sign in to comment.