Skip to content

Commit

Permalink
feat: add subscriptions metrics (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Feb 25, 2024
1 parent 8da2491 commit db00ad0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type App struct {
Logger zerolog.Logger
Config *config.AppConfig
Chains []*configTypes.Chain
NodesManager *nodesManagerPkg.NodesManager
Reporters reportersPkg.Reporters
Expand Down Expand Up @@ -62,6 +63,7 @@ func NewApp(config *config.AppConfig, version string) *App {

return &App{
Logger: logger.With().Str("component", "app").Logger(),
Config: config,
Chains: config.Chains,
Reporters: reporters,
NodesManager: nodesManager,
Expand All @@ -75,7 +77,7 @@ func NewApp(config *config.AppConfig, version string) *App {
func (a *App) Start() {
go a.MetricsManager.Start()
a.MetricsManager.LogAppVersion(a.Version)
a.MetricsManager.SetAllDefaultMetrics(a.Chains)
a.MetricsManager.SetAllDefaultMetrics(a.Config)

for _, reporter := range a.Reporters {
reporter.Init()
Expand Down
22 changes: 18 additions & 4 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Manager struct {
reporterEnabledGauge *prometheus.GaugeVec

// Subscriptions metrics
eventsMatchedCounter *prometheus.CounterVec
subscriptionsInfoCounter *prometheus.GaugeVec
eventsMatchedCounter *prometheus.CounterVec

// App metrics
appVersionGauge *prometheus.GaugeVec
Expand Down Expand Up @@ -96,7 +97,7 @@ func NewManager(logger *zerolog.Logger, config configPkg.MetricsConfig) *Manager
// Reporter metrics
reporterEnabledGauge: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: constants.PrometheusMetricsPrefix + "reporter_enabled",
Help: "Whether the reporter is enabled (1 if yes, 0 if no)",
Help: "Reporter info, with name and type, always returns 1 as value",
}, []string{"name", "type"}),
reporterReportsCounter: promauto.NewCounterVec(prometheus.CounterOpts{
Name: constants.PrometheusMetricsPrefix + "reporter_reports",
Expand All @@ -112,6 +113,10 @@ func NewManager(logger *zerolog.Logger, config configPkg.MetricsConfig) *Manager
}, []string{"chain", "reporter", "type", "subscription"}),

// Subscription metrics
subscriptionsInfoCounter: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: constants.PrometheusMetricsPrefix + "subscriptions",
Help: "Count of chain subscriptions per subscription",
}, []string{"name", "reporter"}),
eventsMatchedCounter: promauto.NewCounterVec(prometheus.CounterOpts{
Name: constants.PrometheusMetricsPrefix + "events_matched",
Help: "WebSocket events matching filters by chain",
Expand All @@ -129,14 +134,23 @@ func NewManager(logger *zerolog.Logger, config configPkg.MetricsConfig) *Manager
}
}

func (m *Manager) SetAllDefaultMetrics(chains []*configTypes.Chain) {
func (m *Manager) SetAllDefaultMetrics(config *configPkg.AppConfig) {
m.startTimeGauge.
With(prometheus.Labels{}).
Set(float64(time.Now().Unix()))

for _, chain := range chains {
for _, chain := range config.Chains {
m.SetDefaultMetrics(chain)
}

for _, subscription := range config.Subscriptions {
m.subscriptionsInfoCounter.
With(prometheus.Labels{
"name": subscription.Name,
"reporter": subscription.Reporter,
}).
Set(float64(len(subscription.ChainSubscriptions)))
}
}
func (m *Manager) SetDefaultMetrics(chain *configTypes.Chain) {
m.chainInfoGauge.
Expand Down

0 comments on commit db00ad0

Please sign in to comment.