Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(metrics): add fetch notifications metric #941

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func main() {
Logger: logger,
Database: database,
Notifier: sender,
Metrics: notifierMetrics,
}
fetchNotificationsWorker.Start()
defer stopNotificationsFetcher(fetchNotificationsWorker)
Expand Down
1 change: 1 addition & 0 deletions integration_tests/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestNotifier(t *testing.T) {
fetchNotificationsWorker := notifications.FetchNotificationsWorker{
Database: database,
Logger: logger,
Metrics: notifierMetrics,
Notifier: notifierInstance,
}

Expand Down
2 changes: 2 additions & 0 deletions metrics/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type NotifierMetrics struct {
SendersFailedMetrics MetersCollection
PlotsBuildDurationMs Histogram
PlotsEvaluateTriggerDurationMs Histogram
FetchNotificationsDurationMs Histogram
kissken marked this conversation as resolved.
Show resolved Hide resolved
}

// ConfigureNotifierMetrics is notifier metrics configurator
Expand All @@ -27,5 +28,6 @@ func ConfigureNotifierMetrics(registry Registry, prefix string) *NotifierMetrics
SendersFailedMetrics: NewMetersCollection(registry),
PlotsBuildDurationMs: registry.NewHistogram("plots", "build", "duration", "ms"),
PlotsEvaluateTriggerDurationMs: registry.NewHistogram("plots", "evaluate", "trigger", "duration", "ms"),
FetchNotificationsDurationMs: registry.NewHistogram("fetch", "notifications", "duration", "ms"),
}
}
8 changes: 7 additions & 1 deletion notifier/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gopkg.in/tomb.v2"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/metrics"
"github.com/moira-alert/moira/notifier"
)

Expand All @@ -18,6 +19,7 @@ type FetchNotificationsWorker struct {
Logger moira.Logger
Database moira.Database
Notifier notifier.Notifier
Metrics *metrics.NotifierMetrics
tomb tomb.Tomb
}

Expand Down Expand Up @@ -63,14 +65,18 @@ func (worker *FetchNotificationsWorker) processScheduledNotifications() error {
if err != nil {
return notifierInBadStateError("can't get current notifier state")
}

if state != moira.SelfStateOK {
return notifierInBadStateError(fmt.Sprintf("notifier in a bad state: %v", state))
}
notifications, err := worker.Database.FetchNotifications(time.Now().Unix(), worker.Notifier.GetReadBatchSize())

fetchNotificationsStartTime := time.Now()
notifications, err := worker.Database.FetchNotifications(time.Now().Unix(), worker.Notifier.GetReadBatchSize())
if err != nil {
return err
}
worker.Metrics.FetchNotificationsDurationMs.Update(time.Since(fetchNotificationsStartTime).Milliseconds())
almostinf marked this conversation as resolved.
Show resolved Hide resolved

notificationPackages := make(map[string]*notifier.NotificationPackage)
for _, notification := range notifications {
packageKey := fmt.Sprintf("%s:%s:%s", notification.Contact.Type, notification.Contact.Value, notification.Event.TriggerID)
Expand Down
5 changes: 5 additions & 0 deletions notifier/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/mock/gomock"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metrics"
. "github.com/smartystreets/goconvey/convey"

"github.com/moira-alert/moira"
Expand All @@ -14,6 +15,8 @@ import (
notifier2 "github.com/moira-alert/moira/notifier"
)

var notifierMetrics = metrics.ConfigureNotifierMetrics(metrics.NewDummyRegistry(), "notifier")

func TestProcessScheduledEvent(t *testing.T) {
subID2 := "subscriptionID-00000000000002"
subID5 := "subscriptionID-00000000000005"
Expand Down Expand Up @@ -60,6 +63,7 @@ func TestProcessScheduledEvent(t *testing.T) {
Database: dataBase,
Logger: logger,
Notifier: notifier,
Metrics: notifierMetrics,
}

Convey("Two different notifications, should send two packages", t, func() {
Expand Down Expand Up @@ -159,6 +163,7 @@ func TestGoRoutine(t *testing.T) {
Database: dataBase,
Logger: logger,
Notifier: notifier,
Metrics: notifierMetrics,
}

shutdown := make(chan struct{})
Expand Down