-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from samcm/feat/net-web3-modules
add net and web3 modules, disable admin module by default
- Loading branch information
Showing
7 changed files
with
189 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters