Skip to content

Commit

Permalink
Merge branch 'feat/add-selfstate-heartbeats' into feat/add-selfstate-…
Browse files Browse the repository at this point in the history
…monitor
  • Loading branch information
almostinf committed Nov 6, 2024
2 parents d1181f1 + 877228a commit 27c5959
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 27 deletions.
10 changes: 1 addition & 9 deletions notifier/selfstate/heartbeat/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package heartbeat
import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/datatypes"
"github.com/moira-alert/moira/metrics"
)

// Verify that notifierHeartbeater matches the Heartbeater interface.
Expand All @@ -17,38 +16,31 @@ type NotifierHeartbeaterConfig struct {
type notifierHeartbeater struct {
*heartbeaterBase

metrics *metrics.HeartBeatMetrics
cfg NotifierHeartbeaterConfig
cfg NotifierHeartbeaterConfig
}

// NewNotifierHeartbeater is a function that creates a new notifierHeartbeater.
func NewNotifierHeartbeater(
cfg NotifierHeartbeaterConfig,
base *heartbeaterBase,
metrics *metrics.HeartBeatMetrics,
) (*notifierHeartbeater, error) {
return &notifierHeartbeater{
cfg: cfg,
heartbeaterBase: base,
metrics: metrics,
}, nil
}

// Check is a function that returns the state of the notifier.
func (heartbeater *notifierHeartbeater) Check() (State, error) {
notifierState, err := heartbeater.database.GetNotifierState()
if err != nil {
heartbeater.metrics.MarkNotifierIsAlive(false)
return StateError, err
}

if notifierState != moira.SelfStateOK {
heartbeater.metrics.MarkNotifierIsAlive(false)
return StateError, nil
}

heartbeater.metrics.MarkNotifierIsAlive(true)

return StateOK, nil
}

Expand Down
22 changes: 4 additions & 18 deletions notifier/selfstate/heartbeat/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ import (

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/datatypes"
"github.com/moira-alert/moira/metrics"

. "github.com/smartystreets/goconvey/convey"
)

func TestNewNotifierHeartbeater(t *testing.T) {
_, _, _, heartbeaterBase := heartbeaterHelper(t) //nolint:dogsled

dummyRegistry := metrics.NewDummyRegistry()
heartbeatMetrics := metrics.ConfigureHeartBeatMetrics(dummyRegistry)

Convey("Test NewNotifierHeartbeater", t, func() {
Convey("With correct local checker heartbeater config", func() {
cfg := NotifierHeartbeaterConfig{}

expected := &notifierHeartbeater{
heartbeaterBase: heartbeaterBase,
cfg: cfg,
metrics: heartbeatMetrics,
}

notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase, heartbeatMetrics)
notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase)
So(err, ShouldBeNil)
So(notifierHeartbeater, ShouldResemble, expected)
})
Expand All @@ -37,12 +32,9 @@ func TestNewNotifierHeartbeater(t *testing.T) {
func TestNotifierHeartbeaterCheck(t *testing.T) {
database, _, _, heartbeaterBase := heartbeaterHelper(t)

dummyRegistry := metrics.NewDummyRegistry()
heartbeatMetrics := metrics.ConfigureHeartBeatMetrics(dummyRegistry)

cfg := NotifierHeartbeaterConfig{}

notifierHeartbeater, _ := NewNotifierHeartbeater(cfg, heartbeaterBase, heartbeatMetrics)
notifierHeartbeater, _ := NewNotifierHeartbeater(cfg, heartbeaterBase)

testErr := errors.New("test error")

Expand Down Expand Up @@ -76,13 +68,10 @@ func TestNotifierHeartbeaterCheck(t *testing.T) {
func TestNotifierHeartbeaterType(t *testing.T) {
_, _, _, heartbeaterBase := heartbeaterHelper(t) //nolint:dogsled

dummyRegistry := metrics.NewDummyRegistry()
heartbeatMetrics := metrics.ConfigureHeartBeatMetrics(dummyRegistry)

Convey("Test notifierHeartbeater.Type", t, func() {
cfg := NotifierHeartbeaterConfig{}

notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase, heartbeatMetrics)
notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase)
So(err, ShouldBeNil)

notifierHeartbeaterType := notifierHeartbeater.Type()
Expand All @@ -93,9 +82,6 @@ func TestNotifierHeartbeaterType(t *testing.T) {
func TestNotifierHeartbeaterAlertSettings(t *testing.T) {
_, _, _, heartbeaterBase := heartbeaterHelper(t) //nolint:dogsled

dummyRegistry := metrics.NewDummyRegistry()
heartbeatMetrics := metrics.ConfigureHeartBeatMetrics(dummyRegistry)

Convey("Test notifierHeartbeater.AlertSettings", t, func() {
alertCfg := AlertConfig{
Name: "test name",
Expand All @@ -108,7 +94,7 @@ func TestNotifierHeartbeaterAlertSettings(t *testing.T) {
},
}

notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase, heartbeatMetrics)
notifierHeartbeater, err := NewNotifierHeartbeater(cfg, heartbeaterBase)
So(err, ShouldBeNil)

alertSettings := notifierHeartbeater.AlertSettings()
Expand Down

0 comments on commit 27c5959

Please sign in to comment.