-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,180 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
Copyright 2022 Nethermind | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/NethermindEth/sedge/cmd/lido-exporter/metrics" | ||
"github.com/NethermindEth/sedge/configs" | ||
"github.com/NethermindEth/sedge/internal/lido/contracts" | ||
"github.com/NethermindEth/sedge/internal/lido/contracts/csmodule" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
nested "github.com/antonfisher/nested-logrus-formatter" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var logLevel string | ||
|
||
func RootCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "lido-exporter", | ||
Short: "Lido Exporter exports Lido CSM metrics to Prometheus", | ||
Long: `Lido Exporter exports Lido CSM metrics to Prometheus`, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
initLogging() | ||
}, | ||
Run: run, | ||
} | ||
|
||
viper.BindPFlag("node-operator-id", cmd.PersistentFlags().Lookup("node-operator-id")) | ||
viper.BindPFlag("reward-address", cmd.PersistentFlags().Lookup("reward-address")) | ||
viper.BindPFlag("network", cmd.PersistentFlags().Lookup("network")) | ||
viper.BindPFlag("rpc-endpoints", cmd.PersistentFlags().Lookup("rpc-endpoints")) | ||
viper.BindPFlag("port", cmd.PersistentFlags().Lookup("port")) | ||
viper.BindPFlag("scrape-time", cmd.PersistentFlags().Lookup("scrape-time")) | ||
viper.BindPFlag("log-level", cmd.PersistentFlags().Lookup("log-level")) | ||
viper.SetEnvPrefix("LIDO_EXPORTER") | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "")) | ||
|
||
// Disable completion default cmd | ||
cmd.CompletionOptions.DisableDefaultCmd = true | ||
|
||
// Persistent flags | ||
cmd.PersistentFlags().String("node-operator-id", "", "Node Operator ID") | ||
cmd.PersistentFlags().String("reward-address", "", "Reward address of Node Operator") | ||
cmd.PersistentFlags().String("network", "holesky", "Network name") | ||
cmd.PersistentFlags().StringSlice("rpc-endpoints", nil, "List of Ethereum RPC endpoints") | ||
cmd.PersistentFlags().String("port", "8080", "Port where the metrics will be exported") | ||
cmd.PersistentFlags().Duration("scrape-time", 10*time.Second, "Time interval for scraping metrics") | ||
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set Log Level, e.g panic, fatal, error, warn, warning, info, debug, trace") | ||
|
||
return cmd | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
nodeOperatorID := viper.GetString("node-operator-id") | ||
rewardAddress := viper.GetString("reward-address") | ||
if nodeOperatorID == "" && rewardAddress == "" { | ||
log.Fatal("Node Operator ID or Reward Address is required") | ||
} | ||
|
||
// Validate port | ||
port := viper.GetString("port") | ||
_, err := strconv.Atoi(port) | ||
if err != nil { | ||
log.Fatalf("Invalid port: %s", port) | ||
} | ||
|
||
network := viper.GetString("network") | ||
var nodeOperatorIDBigInt *big.Int | ||
if nodeOperatorID != "" { | ||
var ok bool | ||
nodeOperatorIDBigInt, ok = new(big.Int).SetString(nodeOperatorID, 10) | ||
if !ok { | ||
log.Fatalf("Failed to convert Node Operator ID to big.Int: %s", nodeOperatorID) | ||
} | ||
} else { | ||
var err error | ||
nodeOperatorIDBigInt, err = csmodule.NodeID(network, rewardAddress) | ||
if err != nil { | ||
log.Fatalf("Failed to get Node Operator ID: %v", err) | ||
} | ||
} | ||
|
||
rpcEndpoints := viper.GetStringSlice("rpc-endpoints") | ||
|
||
client, err := contracts.ConnectClient(network, rpcEndpoints...) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to Ethereum RPC: %v", err) | ||
} | ||
|
||
// Initialize metrics | ||
metrics.InitMetrics(nodeOperatorID, network) | ||
|
||
// Start the metrics server | ||
go func() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", viper.GetString("port")), nil)) | ||
}() | ||
|
||
// Start collecting metrics | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
go metrics.CollectMetrics(ctx, client, nodeOperatorIDBigInt, network, viper.GetDuration("scrape-time")) | ||
|
||
// Wait for interrupt signal to gracefully shutdown the exporter | ||
sigCh := make(chan os.Signal, 1) | ||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) | ||
<-sigCh | ||
fmt.Println("Shutting down Lido Exporter...") | ||
} | ||
|
||
func initLogging() { | ||
log.SetFormatter(&nested.Formatter{ | ||
HideKeys: true, | ||
FieldsOrder: []string{configs.Component}, | ||
TimestampFormat: "2006-01-02 15:04:05 --", | ||
}) | ||
|
||
level, err := log.ParseLevel(strings.ToLower(logLevel)) | ||
if err != nil { | ||
log.WithField(configs.Component, "Logger Init").Error(err) | ||
return | ||
} | ||
log.SetLevel(level) | ||
log.WithField(configs.Component, "Logger Init").Infof("Log level: %+v", logLevel) | ||
} |
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,31 @@ | ||
/* | ||
Copyright 2022 Nethermind | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"github.com/NethermindEth/sedge/cmd/lido-exporter/cli" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
rootCmd := cli.RootCmd() | ||
rootCmd.SilenceErrors = true | ||
rootCmd.SilenceUsage = true | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,70 @@ | ||
/* | ||
Copyright 2022 Nethermind | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"time" | ||
|
||
bonds "github.com/NethermindEth/sedge/internal/lido/contracts/csaccounting" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
bondCurrentGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "csm_bond_current", | ||
Help: "The current amount of bonded ETH", | ||
}, []string{"node_operator_id", "network"}) | ||
|
||
bondRequiredGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "csm_bond_required", | ||
Help: "The required amount of ETH to maintain", | ||
}, []string{"node_operator_id", "network"}) | ||
|
||
bondExcessGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "csm_bond_excess", | ||
Help: "The amount of excess bond over the required amount", | ||
}, []string{"node_operator_id", "network"}) | ||
|
||
bondMissedGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: "csm_bond_missed", | ||
Help: "The amount of bond that is missing", | ||
}, []string{"node_operator_id", "network"}) | ||
) | ||
|
||
func collectBondInfo(ctx context.Context, network string, nodeOperatorID *big.Int, scrapeTime time.Duration) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-time.After(scrapeTime): | ||
bondInfo, err := bonds.BondSummary(network, nodeOperatorID) | ||
if err != nil { | ||
log.Errorf("Failed to get bond summary: %v", err) | ||
return | ||
} | ||
|
||
bondCurrentGauge.WithLabelValues(nodeOperatorID.String(), network).Set(float64(bondInfo.Current.Int64())) | ||
bondRequiredGauge.WithLabelValues(nodeOperatorID.String(), network).Set(float64(bondInfo.Required.Int64())) | ||
bondExcessGauge.WithLabelValues(nodeOperatorID.String(), network).Set(float64(bondInfo.Excess.Int64())) | ||
bondMissedGauge.WithLabelValues(nodeOperatorID.String(), network).Set(float64(bondInfo.Missed.Int64())) | ||
} | ||
} | ||
} |
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,67 @@ | ||
/* | ||
Copyright 2022 Nethermind | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
|
||
"github.com/NethermindEth/sedge/internal/lido/contracts" | ||
"github.com/NethermindEth/sedge/internal/lido/contracts/vebo" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var exitRequestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "csm_onchain_exit_requests_total", | ||
Help: "Number of validator exit requests", | ||
}, []string{"node_operator_id", "network", "tx_hash"}) | ||
|
||
// collectExitRequests listens for ValidatorExitRequest events on the Vebo contract and updates the exit requests counter. | ||
// `filterOpts` is used to specify the start block for the event logs. | ||
func collectExitRequests(ctx context.Context, network string, nodeOperatorID *big.Int, client *ethclient.Client, filterOpts *bind.WatchOpts) { | ||
validatorExitRequestCh := make(chan *vebo.VeboValidatorExitRequest) | ||
|
||
// Get staking module ID | ||
stakingModuleID, err := contracts.StakingModuleID(network) | ||
if err != nil { | ||
log.Errorf("Failed to get staking module ID: %v", err) | ||
return | ||
} | ||
|
||
veboFilterer, err := vebo.NewVeboFilterer(common.HexToAddress(contracts.DeployedAddresses(contracts.Vebo)[network]), client) | ||
|
||
// Should ValidatorIndex be added? | ||
// Subscribe to ValidatorExitRequest events | ||
_, err = veboFilterer.WatchValidatorExitRequest(filterOpts, validatorExitRequestCh, []*big.Int{stakingModuleID}, []*big.Int{nodeOperatorID}, nil) | ||
if err != nil { | ||
log.Printf("Failed to watch ValidatorExitRequest events: %v", err) | ||
} | ||
|
||
for { | ||
select { | ||
case event := <-validatorExitRequestCh: | ||
exitRequestsTotal.WithLabelValues(nodeOperatorID.String(), network, event.Raw.TxHash.Hex()).Inc() | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.