-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add metrics for the number of triggers by source (#904)
- Loading branch information
1 parent
9d54d2f
commit 95f3170
Showing
12 changed files
with
365 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/moira-alert/moira" | ||
"github.com/moira-alert/moira/metrics" | ||
"gopkg.in/tomb.v2" | ||
) | ||
|
||
type triggerStats struct { | ||
tomb tomb.Tomb | ||
metrics *metrics.TriggersMetrics | ||
database moira.Database | ||
logger moira.Logger | ||
} | ||
|
||
func newTriggerStats( | ||
logger moira.Logger, | ||
database moira.Database, | ||
metricsRegistry metrics.Registry, | ||
) *triggerStats { | ||
return &triggerStats{ | ||
logger: logger, | ||
database: database, | ||
metrics: metrics.NewTriggersMetrics(metricsRegistry), | ||
} | ||
} | ||
|
||
func (stats *triggerStats) Start() { | ||
stats.tomb.Go(stats.startCheckingTriggerCount) | ||
} | ||
|
||
func (stats *triggerStats) startCheckingTriggerCount() error { | ||
checkTicker := time.NewTicker(time.Second * 60) | ||
for { | ||
select { | ||
case <-stats.tomb.Dying(): | ||
return nil | ||
|
||
case <-checkTicker.C: | ||
stats.checkTriggerCount() | ||
} | ||
} | ||
} | ||
|
||
func (stats *triggerStats) Stop() error { | ||
stats.tomb.Kill(nil) | ||
return stats.tomb.Wait() | ||
} | ||
|
||
func (stats *triggerStats) checkTriggerCount() { | ||
triggersCount, err := stats.database.GetTriggerCount() | ||
if err != nil { | ||
stats.logger.Warning(). | ||
Error(err). | ||
Msg("Failed to fetch triggers count") | ||
return | ||
} | ||
|
||
for source, count := range triggersCount { | ||
stats.metrics.Mark(source, count) | ||
stats.logger.Debug().Msg(fmt.Sprintf("source: %s, count: %d", string(source), count)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/moira-alert/moira" | ||
"github.com/moira-alert/moira/logging/zerolog_adapter" | ||
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" | ||
mock_metrics "github.com/moira-alert/moira/mock/moira-alert/metrics" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestTriggerStatsCheckTriggerCount(t *testing.T) { | ||
Convey("Given db returns correct results", t, func() { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
registry := mock_metrics.NewMockRegistry(mockCtrl) | ||
|
||
graphiteLocalCount := int64(12) | ||
graphiteRemoteCount := int64(24) | ||
promethteusRemoteCount := int64(42) | ||
|
||
graphiteLocalMeter := mock_metrics.NewMockMeter(mockCtrl) | ||
graphiteRemoteMeter := mock_metrics.NewMockMeter(mockCtrl) | ||
promethteusRemoteMeter := mock_metrics.NewMockMeter(mockCtrl) | ||
|
||
registry.EXPECT().NewMeter("triggers", string(moira.GraphiteLocal), "count").Return(graphiteLocalMeter) | ||
registry.EXPECT().NewMeter("triggers", string(moira.GraphiteRemote), "count").Return(graphiteRemoteMeter) | ||
registry.EXPECT().NewMeter("triggers", string(moira.PrometheusRemote), "count").Return(promethteusRemoteMeter) | ||
|
||
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl) | ||
dataBase.EXPECT().GetTriggerCount().Return(map[moira.TriggerSource]int64{ | ||
moira.GraphiteLocal: graphiteLocalCount, | ||
moira.GraphiteRemote: graphiteRemoteCount, | ||
moira.PrometheusRemote: promethteusRemoteCount, | ||
}, nil) | ||
|
||
graphiteLocalMeter.EXPECT().Mark(graphiteLocalCount) | ||
graphiteRemoteMeter.EXPECT().Mark(graphiteRemoteCount) | ||
promethteusRemoteMeter.EXPECT().Mark(promethteusRemoteCount) | ||
|
||
logger, _ := zerolog_adapter.GetLogger("Test") | ||
triggerStats := newTriggerStats(logger, dataBase, registry) | ||
|
||
triggerStats.checkTriggerCount() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package metrics | ||
|
||
import "github.com/moira-alert/moira" | ||
|
||
// Collection of metrics for trigger count metrics | ||
type TriggersMetrics struct { | ||
countByTriggerSource map[moira.TriggerSource]Meter | ||
} | ||
|
||
// Creates and configurates the instance of TriggersMetrics | ||
func NewTriggersMetrics(registry Registry) *TriggersMetrics { | ||
return &TriggersMetrics{ | ||
countByTriggerSource: map[moira.TriggerSource]Meter{ | ||
moira.GraphiteLocal: registry.NewMeter("triggers", string(moira.GraphiteLocal), "count"), | ||
moira.GraphiteRemote: registry.NewMeter("triggers", string(moira.GraphiteRemote), "count"), | ||
moira.PrometheusRemote: registry.NewMeter("triggers", string(moira.PrometheusRemote), "count"), | ||
}, | ||
} | ||
} | ||
|
||
// Marks the number of trigger for given trigger source | ||
func (metrics *TriggersMetrics) Mark(source moira.TriggerSource, count int64) { | ||
metrics.countByTriggerSource[source].Mark(count) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.