Skip to content

Commit

Permalink
Merge pull request #15 from samcm/feat/net-web3-modules
Browse files Browse the repository at this point in the history
add net and web3 modules, disable admin module by default
  • Loading branch information
samcm authored May 18, 2022
2 parents fce073c + cf3ba99 commit b1bafea
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 13 deletions.
7 changes: 4 additions & 3 deletions example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ execution:
url: "http://localhost:8545"
name: "execution-client"
modules:
- "eth"
- "net"
- "admin"
- "eth"
- "net"
- "web3"
- "txpool"
diskUsage:
enabled: false
directories:
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func DefaultConfig() *Config {
Enabled: true,
Name: "execution",
URL: "http://localhost:8545",
Modules: []string{"eth", "net", "admin", "txpool"},
Modules: []string{"eth", "net", "web3", "txpool"},
},
Consensus: ConsensusNode{
Enabled: true,
Expand Down
8 changes: 0 additions & 8 deletions pkg/exporter/consensus/jobs/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ func NewGeneralJob(client eth2client.Service, log logrus.FieldLogger, namespace
"version",
},
),
NetworkdID: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "network_id",
Help: "The network id of the node.",
ConstLabels: constLabels,
},
),
ReOrgs: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Expand Down
2 changes: 1 addition & 1 deletion pkg/exporter/execution/jobs/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (b *BlockMetrics) getHeadBlockStats(ctx context.Context) error {
b.currentHeadBlockNumber = mostRecentBlockNumber
b.MostRecentBlockNumber.WithLabelValues("head").Set(float64(mostRecentBlockNumber))

block, err := b.ethRpcClient.EthGetBlockByNumber(int(mostRecentBlockNumber), true)
block, err := b.ethRpcClient.EthGetBlockByNumber(int(mostRecentBlockNumber), false)
if err != nil {
return err
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/exporter/execution/jobs/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package jobs

import (
"context"
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
)

// Net exposes metrics defined by the net module.
type Net struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
ethrpcClient *ethrpc.EthRPC
log logrus.FieldLogger
PeerCount prometheus.Gauge
}

const (
NameNet = "net"
)

func (n *Net) Name() string {
return NameNet
}

func (n *Net) RequiredModules() []string {
return []string{"net"}
}

// NewNet returns a new Net instance.
func NewNet(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) Net {
namespace = namespace + "_net"
constLabels["module"] = NameWeb3

return Net{
client: client,
api: internalApi,
ethrpcClient: ethRpcClient,
log: log.WithField("module", NameNet),
PeerCount: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "peer_count",
Help: "The amount of peers connected to the node.",
ConstLabels: constLabels,
},
),
}
}

func (n *Net) Start(ctx context.Context) {
n.tick(ctx)
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second * 15):
n.tick(ctx)
}
}
}

func (n *Net) tick(ctx context.Context) {
count, err := n.ethrpcClient.NetPeerCount()
if err != nil {
n.log.WithError(err).Error("Failed to get peer count")
} else {
n.PeerCount.Set(float64(count))
}

}
80 changes: 80 additions & 0 deletions pkg/exporter/execution/jobs/web3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package jobs

import (
"context"
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
)

// Web3 exposes metrics defined by the Web3 module.
type Web3 struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
ethrpcClient *ethrpc.EthRPC
log logrus.FieldLogger
ClientVersion prometheus.GaugeVec
}

const (
NameWeb3 = "web3"
)

func (w *Web3) Name() string {
return NameWeb3
}

func (w *Web3) RequiredModules() []string {
return []string{"web3"}
}

// NewWeb3 returns a new Web3 instance.
func NewWeb3(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) Web3 {
namespace = namespace + "_web3"
constLabels["module"] = NameWeb3

return Web3{
client: client,
api: internalApi,
ethrpcClient: ethRpcClient,
log: log.WithField("module", NameWeb3),
ClientVersion: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "client_version",
Help: "Client version.",
ConstLabels: constLabels,
},
[]string{
"version",
},
),
}
}

func (a *Web3) Start(ctx context.Context) {
a.tick(ctx)
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second * 15):
a.tick(ctx)
}
}
}

func (a *Web3) tick(ctx context.Context) {
clientVersion, err := a.ethrpcClient.Web3ClientVersion()
if err != nil {
a.log.WithError(err).Error("Failed to get node info")
} else {
a.ClientVersion.WithLabelValues(clientVersion).Set(1)
}

}
26 changes: 26 additions & 0 deletions pkg/exporter/execution/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type metrics struct {
txpoolMetrics jobs.TXPool
adminMetrics jobs.Admin
blockMetrics jobs.BlockMetrics
web3Metrics jobs.Web3
netMetrics jobs.Net

enabledJobs map[string]bool
}
Expand All @@ -41,6 +43,8 @@ func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, ethRp
txpoolMetrics: jobs.NewTXPool(client, internalApi, ethRpcClient, log, namespace, constLabels),
adminMetrics: jobs.NewAdmin(client, internalApi, ethRpcClient, log, namespace, constLabels),
blockMetrics: jobs.NewBlockMetrics(client, internalApi, ethRpcClient, log, namespace, constLabels),
web3Metrics: jobs.NewWeb3(client, internalApi, ethRpcClient, log, namespace, constLabels),
netMetrics: jobs.NewNet(client, internalApi, ethRpcClient, log, namespace, constLabels),

enabledJobs: make(map[string]bool),
}
Expand Down Expand Up @@ -104,6 +108,20 @@ func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, ethRp
prometheus.MustRegister(m.adminMetrics.Peers)
}

if able := jobs.ExporterCanRun(enabledModules, m.web3Metrics.RequiredModules()); able {
m.log.Info("Enabling web3 metrics")
m.enabledJobs[m.web3Metrics.Name()] = true

prometheus.MustRegister(m.web3Metrics.ClientVersion)
}

if able := jobs.ExporterCanRun(enabledModules, m.netMetrics.RequiredModules()); able {
m.log.Info("Enabling net metrics")
m.enabledJobs[m.netMetrics.Name()] = true

prometheus.MustRegister(m.netMetrics.PeerCount)
}

return m
}

Expand All @@ -128,5 +146,13 @@ func (m *metrics) StartAsync(ctx context.Context) {
go m.blockMetrics.Start(ctx)
}

if m.enabledJobs[m.web3Metrics.Name()] {
go m.web3Metrics.Start(ctx)
}

if m.enabledJobs[m.netMetrics.Name()] {
go m.netMetrics.Start(ctx)
}

m.log.Info("Started metrics exporter jobs")
}

0 comments on commit b1bafea

Please sign in to comment.