Skip to content

Commit

Permalink
EVM-850 Validator rootchain balance metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Sep 27, 2023
1 parent 4b2770e commit 61433c2
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Config struct {

ConcurrentRequestsDebug uint64 `json:"concurrent_requests_debug" yaml:"concurrent_requests_debug"`
WebSocketReadLimit uint64 `json:"web_socket_read_limit" yaml:"web_socket_read_limit"`

MetricsInterval time.Duration `json:"metrics_interval" yaml:"metrics_interval"`
}

// Telemetry holds the config details for metric services.
Expand Down Expand Up @@ -96,6 +98,10 @@ const (
// DefaultRelayerTrackerPollInterval specifies time interval after which relayer node's event tracker
// polls child chain to get the latest block
DefaultRelayerTrackerPollInterval time.Duration = time.Second

// DefaultMetricsInterval specifies the time interval after which Prometheus metrics will be generated.
// A value of 0 means the metrics are disabled.
DefaultMetricsInterval time.Duration = time.Second * 8
)

// DefaultConfig returns the default server configuration
Expand Down Expand Up @@ -136,6 +142,7 @@ func DefaultConfig() *Config {
ConcurrentRequestsDebug: DefaultConcurrentRequestsDebug,
WebSocketReadLimit: DefaultWebSocketReadLimit,
RelayerTrackerPollInterval: DefaultRelayerTrackerPollInterval,
MetricsInterval: DefaultMetricsInterval,
}
}

Expand Down
3 changes: 3 additions & 0 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
webSocketReadLimitFlag = "websocket-read-limit"

relayerTrackerPollIntervalFlag = "relayer-poll-interval"

metricsIntervalFlag = "metrics-interval"
)

// Flags that are deprecated, but need to be preserved for
Expand Down Expand Up @@ -190,5 +192,6 @@ func (p *serverParams) generateConfig() *server.Config {
Relayer: p.relayer,
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
RelayerTrackerPollInterval: p.rawConfig.RelayerTrackerPollInterval,
MetricsInterval: p.rawConfig.MetricsInterval,
}
}
7 changes: 7 additions & 0 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ func setFlags(cmd *cobra.Command) {
"interval (number of seconds) at which relayer's tracker polls for latest block at childchain",
)

cmd.Flags().DurationVar(
&params.rawConfig.MetricsInterval,
metricsIntervalFlag,
defaultConfig.MetricsInterval,
"interval (number of seconds) at which special metrics is generated. zero means the metrics is disabled",
)

setLegacyFlags(cmd)

setDevFlags(cmd)
Expand Down
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consensus
import (
"context"
"log"
"time"

"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/chain"
Expand Down Expand Up @@ -78,6 +79,7 @@ type Params struct {
BlockTime uint64

NumBlockConfirmations uint64
MetricsInterval time.Duration
}

// Factory is the factory function to create a discovery consensus
Expand Down
6 changes: 6 additions & 0 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ func (p *Polybft) Start() error {
// start state DB process
go p.state.startStatsReleasing()

// additional polybft metrics
go polybftMetrics(
p.consensusConfig.Bridge.JSONRPCEndpoint,
p.key.Address(), p.closeCh,
p.logger, p.config.MetricsInterval)

return nil
}

Expand Down
45 changes: 45 additions & 0 deletions consensus/polybft/state_stats.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package polybft

import (
"math/big"
"time"

"github.com/armon/go-metrics"
"github.com/hashicorp/go-hclog"
"github.com/prometheus/client_golang/prometheus"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
)

// startStatsReleasing starts the process that releases BoltDB stats into prometheus periodically.
Expand Down Expand Up @@ -154,3 +158,44 @@ func (s *State) startStatsReleasing() {
prev = stats
}
}

func polybftMetrics(rootnodeURL string,
validatorAddress ethgo.Address,
closeCh <-chan struct{},
logger hclog.Logger,
interval time.Duration) {
// zero means metrics are disabled
if interval <= 0 {
return
}

gweiPerWei := new(big.Int).Exp(big.NewInt(10), big.NewInt(9), nil) // 10^9

ticker := time.NewTicker(interval)
defer ticker.Stop()

rpcClient, err := jsonrpc.NewClient(rootnodeURL)
if err != nil {
logger.Error("metrics - connection to root node failed", "err", err)

return
}

for {
select {
case <-closeCh:
return
case <-ticker.C:
balance, err := rpcClient.Eth().GetBalance(validatorAddress, ethgo.Latest)
if err != nil {
logger.Error("metrics get balance call failed", "err", err)

continue
}

balanceInGwei := new(big.Int).Div(balance, gweiPerWei).Uint64()
metrics.SetGauge([]string{"bridge", "validator_balance_gwei", validatorAddress.String()},
float32(balanceInGwei))
}
}
}
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Config struct {

NumBlockConfirmations uint64
RelayerTrackerPollInterval time.Duration
MetricsInterval time.Duration
}

// Telemetry holds the config details for metric services
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ func (s *Server) setupConsensus() error {
SecretsManager: s.secretsManager,
BlockTime: uint64(blockTime.Seconds()),
NumBlockConfirmations: s.config.NumBlockConfirmations,
MetricsInterval: s.config.MetricsInterval,
},
)

Expand Down

0 comments on commit 61433c2

Please sign in to comment.