Skip to content

Commit

Permalink
chore: add metrics manager tests (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Aug 30, 2024
1 parent a6f7622 commit dd30e82
Show file tree
Hide file tree
Showing 4 changed files with 492 additions and 40 deletions.
4 changes: 2 additions & 2 deletions pkg/data/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestGetValidatorsConsumerAssignedKeysOk(t *testing.T) {
LCDEndpoints: []string{"https://consumer.com"},
ProviderLCDEndpoints: []string{"https://provider.com"},
}
logger := loggerPkg.GetDefaultLogger()
logger := loggerPkg.GetNopLogger()

metricsManager := metrics.NewManager(*logger, configPkg.MetricsConfig{Enabled: null.BoolFrom(false)})
dataManager := NewManager(*logger, config, metricsManager)
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestGetValidatorsConsumerAssignedKeysOkAnotherPrefix(t *testing.T) {
LCDEndpoints: []string{"https://consumer.com"},
ProviderLCDEndpoints: []string{"https://provider.com"},
}
logger := loggerPkg.GetDefaultLogger()
logger := loggerPkg.GetNopLogger()

metricsManager := metrics.NewManager(*logger, configPkg.MetricsConfig{Enabled: null.BoolFrom(false)})
dataManager := NewManager(*logger, config, metricsManager)
Expand Down
73 changes: 36 additions & 37 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package metrics

import (
"context"
"errors"
configPkg "main/pkg/config"
"main/pkg/constants"
"main/pkg/types"
Expand All @@ -19,6 +21,7 @@ type Manager struct {
config configPkg.MetricsConfig

registry *prometheus.Registry
server *http.Server

lastBlockHeightCollector *prometheus.GaugeVec
lastBlockTimeCollector *prometheus.GaugeVec
Expand All @@ -38,7 +41,6 @@ type Manager struct {

missingBlocksGauge *prometheus.GaugeVec
activeBlocksGauge *prometheus.GaugeVec
needsToSignGauge *prometheus.GaugeVec
votingPowerGauge *prometheus.GaugeVec
cumulativeVotingPowerGauge *prometheus.GaugeVec
validatorRankGauge *prometheus.GaugeVec
Expand Down Expand Up @@ -124,10 +126,6 @@ func NewManager(logger zerolog.Logger, config configPkg.MetricsConfig) *Manager
Name: constants.PrometheusMetricsPrefix + "active_blocks",
Help: "Count of each validator's blocks during which they were active",
}, []string{"chain", "moniker", "address"})
needsToSignGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: constants.PrometheusMetricsPrefix + "needs_to_sign",
Help: "Whether the validator needs to sign blocks (for consumer chains)",
}, []string{"chain", "moniker", "address"})
votingPowerGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: constants.PrometheusMetricsPrefix + "voting_power",
Help: "Voting power % of the validator",
Expand Down Expand Up @@ -190,7 +188,6 @@ func NewManager(logger zerolog.Logger, config configPkg.MetricsConfig) *Manager
registry.MustRegister(validatorRankGauge)
registry.MustRegister(isActiveGauge)
registry.MustRegister(isJailedGauge)
registry.MustRegister(needsToSignGauge)
registry.MustRegister(isTombstonedGauge)
registry.MustRegister(signedBlocksWindowGauge)
registry.MustRegister(storeBlocksGauge)
Expand All @@ -201,6 +198,8 @@ func NewManager(logger zerolog.Logger, config configPkg.MetricsConfig) *Manager
With(prometheus.Labels{}).
Set(float64(time.Now().Unix()))

server := &http.Server{Addr: config.ListenAddr, Handler: nil}

return &Manager{
logger: logger.With().Str("component", "metrics").Logger(),
config: config,
Expand All @@ -226,12 +225,12 @@ func NewManager(logger zerolog.Logger, config configPkg.MetricsConfig) *Manager
validatorRankGauge: validatorRankGauge,
isActiveGauge: isActiveGauge,
isJailedGauge: isJailedGauge,
needsToSignGauge: needsToSignGauge,
isTombstonedGauge: isTombstonedGauge,
signedBlocksWindowGauge: signedBlocksWindowGauge,
storeBlocksGauge: storeBlocksGauge,
minSignedPerWindowGauge: minSignedPerWindowGauge,
chainInfoGauge: chainInfoGauge,
server: server,
}
}

Expand All @@ -240,13 +239,6 @@ func (m *Manager) SetDefaultMetrics(chain *configPkg.ChainConfig) {
With(prometheus.Labels{"chain": chain.Name}).
Add(0)

m.reportEntriesCounter.
With(prometheus.Labels{
"chain": chain.Name,
"type": string(constants.EventValidatorActive),
}).
Add(0)

for _, eventName := range constants.GetEventNames() {
m.reportEntriesCounter.
With(prometheus.Labels{
Expand Down Expand Up @@ -277,15 +269,32 @@ func (m *Manager) Start() {
Str("addr", m.config.ListenAddr).
Msg("Metrics handler listening")

http.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{Registry: m.registry}))
if err := http.ListenAndServe(m.config.ListenAddr, nil); err != nil {
m.logger.Fatal().
handler := http.NewServeMux()
handler.Handle("/metrics", promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
handler.HandleFunc("/healthcheck", m.Healthcheck)
m.server.Handler = handler

if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
m.logger.Panic().
Err(err).
Str("addr", m.config.ListenAddr).
Msg("Cannot start metrics handler")
}
}

func (m *Manager) Healthcheck(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
}

func (m *Manager) Stop() {
m.logger.Info().Str("addr", m.config.ListenAddr).Msg("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = m.server.Shutdown(ctx)
}

func (m *Manager) LogLastHeight(chain string, height int64, blockTime time.Time) {
m.lastBlockHeightCollector.
With(prometheus.Labels{"chain": chain}).
Expand Down Expand Up @@ -366,44 +375,44 @@ func (m *Manager) LogNodeReconnect(chain string, node string) {
}

func (m *Manager) LogValidatorStats(
chain *configPkg.ChainConfig,
chain string,
entry *types.Entry,
) {
m.missingBlocksGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(float64(entry.SignatureInfo.GetNotSigned()))

m.activeBlocksGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(float64(entry.SignatureInfo.Active))

m.isActiveGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(utils.BoolToFloat64(entry.IsActive))

m.isJailedGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(utils.BoolToFloat64(entry.Validator.Jailed))

m.missingBlocksGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Expand All @@ -412,43 +421,33 @@ func (m *Manager) LogValidatorStats(
if entry.Validator.SigningInfo != nil {
m.isTombstonedGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(utils.BoolToFloat64(entry.Validator.SigningInfo.Tombstoned))
}

if entry.IsActive {
if chain.IsConsumer.Bool {
m.needsToSignGauge.
With(prometheus.Labels{
"chain": chain.Name,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(utils.BoolToFloat64(entry.NeedsToSign))
}

m.votingPowerGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(entry.Validator.VotingPowerPercent)

m.cumulativeVotingPowerGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Set(entry.Validator.CumulativeVotingPowerPercent)

m.validatorRankGauge.
With(prometheus.Labels{
"chain": chain.Name,
"chain": chain,
"moniker": entry.Validator.Moniker,
"address": entry.Validator.OperatorAddress,
}).
Expand Down
Loading

0 comments on commit dd30e82

Please sign in to comment.