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

feat: Add metrics for the number of triggers by source #904

Merged
merged 8 commits into from
Sep 4, 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
4 changes: 4 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func main() {
}
defer searchIndex.Stop() //nolint

stats := newTriggerStats(logger, database, telemetry.Metrics)
stats.Start()
defer stats.Stop() //nolint

if !searchIndex.IsReady() {
logger.Fatal().Msg("Search index is not ready, exit")
}
Expand Down
66 changes: 66 additions & 0 deletions cmd/api/trigger_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main
kissken marked this conversation as resolved.
Show resolved Hide resolved

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.ConfigureTriggersMetrics(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.Update(source, count)
stats.logger.Debug().Msg(fmt.Sprintf("source: %s, count: %d", string(source), count))
}
}
32 changes: 32 additions & 0 deletions database/redis/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ func (connector *DbConnector) GetPrometheusTriggerIDs() ([]string, error) {
return triggerIds, nil
}

func (connector *DbConnector) GetTriggerCount() (map[moira.TriggerSource]int64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test

pipe := (*connector.client).TxPipeline()

total := pipe.SCard(connector.context, triggersListKey)
remote := pipe.SCard(connector.context, remoteTriggersListKey)
prometheus := pipe.SCard(connector.context, prometheusTriggersListKey)

_, err := pipe.Exec(connector.context)
if err != nil {
return nil, err
}

totalCount, err := total.Result()
if err != nil {
return nil, err
}
remoteCount, err := remote.Result()
if err != nil {
return nil, err
}
prometheusCount, err := prometheus.Result()
if err != nil {
return nil, err
}

return map[moira.TriggerSource]int64{
moira.GraphiteLocal: totalCount - remoteCount - prometheusCount,
moira.GraphiteRemote: remoteCount,
moira.PrometheusRemote: prometheusCount,
}, nil
}

// GetTrigger gets trigger and trigger tags by given ID and return it in merged object
func (connector *DbConnector) GetTrigger(triggerID string) (moira.Trigger, error) {
pipe := (*connector.client).TxPipeline()
Expand Down
2 changes: 2 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Database interface {
GetRemoteTriggerIDs() ([]string, error)
GetPrometheusTriggerIDs() ([]string, error)

GetTriggerCount() (map[TriggerSource]int64, error)

GetTrigger(triggerID string) (Trigger, error)
GetTriggers(triggerIDs []string) ([]*Trigger, error)
GetTriggerChecks(triggerIDs []string) ([]*TriggerCheck, error)
Expand Down
23 changes: 23 additions & 0 deletions metrics/triggers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package metrics

import "github.com/moira-alert/moira"

const triggersMetricsPrefix = "triggersMetrics"

type TriggersMetrics struct {
countByTriggerSource map[moira.TriggerSource]Histogram
}

func ConfigureTriggersMetrics(registry Registry) *TriggersMetrics {
return &TriggersMetrics{
countByTriggerSource: map[moira.TriggerSource]Histogram{
moira.GraphiteLocal: registry.NewHistogram(triggersMetricsPrefix, string(moira.GraphiteLocal)),
moira.GraphiteRemote: registry.NewHistogram(triggersMetricsPrefix, string(moira.GraphiteRemote)),
moira.PrometheusRemote: registry.NewHistogram(triggersMetricsPrefix, string(moira.PrometheusRemote)),
Copy link
Member

@kissken kissken Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

triggers.count.source better i think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

},
}
}

func (metrics *TriggersMetrics) Update(source moira.TriggerSource, count int64) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

godoc as always

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

metrics.countByTriggerSource[source].Update(count)
}
15 changes: 15 additions & 0 deletions mock/moira-alert/database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading