From d0de1691e2bda081ed04a4c2745640e7e02eff4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Thu, 27 Jun 2024 12:01:46 +0200 Subject: [PATCH] feat: refactor --- aggregator/aggregator.go | 8 +- aggregator/config.go | 6 +- aggregator/interfaces.go | 2 +- aggregator/metrics/metrics.go | 51 -- aggregator/prover/prover.go | 9 - .../types/finalproofinputs.go | 0 {etherman => aggregator}/types/sequence.go | 0 dataavailability/dataavailability.go | 2 +- encoding/encoding.go | 110 --- etherman/aggregator.go | 14 +- etherman/etherman.go | 2 +- etherman/etherscan/etherscan.go | 11 +- etherman/ethgasstation/ethgasstation.go | 7 +- metrics/api.go | 16 - metrics/config.go | 17 - metrics/prometheus.go | 677 ------------------ metrics/prometheus_test.go | 371 ---------- sequencesender/sequencesender.go | 2 +- 18 files changed, 33 insertions(+), 1272 deletions(-) delete mode 100644 aggregator/metrics/metrics.go rename {etherman => aggregator}/types/finalproofinputs.go (100%) rename {etherman => aggregator}/types/sequence.go (100%) delete mode 100644 encoding/encoding.go delete mode 100644 metrics/api.go delete mode 100644 metrics/config.go delete mode 100644 metrics/prometheus.go delete mode 100644 metrics/prometheus_test.go diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index 7527f9a6..36524e98 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -17,10 +17,9 @@ import ( "github.com/0xPolygon/cdk-rpc/rpc" cdkTypes "github.com/0xPolygon/cdk-rpc/types" - "github.com/0xPolygon/cdk/aggregator/metrics" "github.com/0xPolygon/cdk/aggregator/prover" + ethmanTypes "github.com/0xPolygon/cdk/aggregator/types" "github.com/0xPolygon/cdk/config/types" - ethmanTypes "github.com/0xPolygon/cdk/etherman/types" "github.com/0xPolygon/cdk/l1infotree" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/state" @@ -419,8 +418,6 @@ func (a *Aggregator) Start(ctx context.Context) error { a.ctx = ctx a.exit = cancel - metrics.Register() - address := fmt.Sprintf("%s:%d", a.cfg.Host, a.cfg.Port) lis, err := net.Listen("tcp", address) if err != nil { @@ -530,9 +527,6 @@ func (a *Aggregator) Stop() { // Channel implements the bi-directional communication channel between the // Prover client and the Aggregator server. func (a *Aggregator) Channel(stream prover.AggregatorService_ChannelServer) error { - metrics.ConnectedProver() - defer metrics.DisconnectedProver() - ctx := stream.Context() var proverAddr net.Addr p, ok := peer.FromContext(ctx) diff --git a/aggregator/config.go b/aggregator/config.go index 64132f2f..51d84381 100644 --- a/aggregator/config.go +++ b/aggregator/config.go @@ -9,7 +9,6 @@ import ( "github.com/0xPolygon/cdk/config/types" "github.com/0xPolygon/cdk/db" - "github.com/0xPolygon/cdk/encoding" "github.com/0xPolygon/cdk/log" "github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager" syncronizerConfig "github.com/0xPolygonHermez/zkevm-synchronizer-l1/config" @@ -25,6 +24,9 @@ const ( // L1 settlement backend L1 SettlementBackend = "l1" + + // TenToThePowerOf18 represents 1000000000000000000 + TenToThePowerOf18 = 1000000000000000000 ) // TokenAmountWithDecimals is a wrapper type that parses token amount with decimals to big int @@ -38,7 +40,7 @@ func (t *TokenAmountWithDecimals) UnmarshalText(data []byte) error { if !ok { return fmt.Errorf("failed to unmarshal string to float") } - coin := new(big.Float).SetInt(big.NewInt(encoding.TenToThePowerOf18)) + coin := new(big.Float).SetInt(big.NewInt(TenToThePowerOf18)) bigval := new(big.Float).Mul(amount, coin) result := new(big.Int) bigval.Int(result) diff --git a/aggregator/interfaces.go b/aggregator/interfaces.go index e0372975..6ca90ad1 100644 --- a/aggregator/interfaces.go +++ b/aggregator/interfaces.go @@ -5,7 +5,7 @@ import ( "math/big" "github.com/0xPolygon/cdk/aggregator/prover" - ethmanTypes "github.com/0xPolygon/cdk/etherman/types" + ethmanTypes "github.com/0xPolygon/cdk/aggregator/types" "github.com/0xPolygon/cdk/state" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" diff --git a/aggregator/metrics/metrics.go b/aggregator/metrics/metrics.go deleted file mode 100644 index 98d3477d..00000000 --- a/aggregator/metrics/metrics.go +++ /dev/null @@ -1,51 +0,0 @@ -package metrics - -import ( - "github.com/0xPolygon/cdk/metrics" - "github.com/prometheus/client_golang/prometheus" -) - -const ( - prefix = "aggregator_" - currentConnectedProversName = prefix + "current_connected_provers" - currentWorkingProversName = prefix + "current_working_provers" -) - -// Register the metrics for the sequencer package. -func Register() { - gauges := []prometheus.GaugeOpts{ - { - Name: currentConnectedProversName, - Help: "[AGGREGATOR] current connected provers", - }, - { - Name: currentWorkingProversName, - Help: "[AGGREGATOR] current working provers", - }, - } - - metrics.RegisterGauges(gauges...) -} - -// ConnectedProver increments the gauge for the current number of connected -// provers. -func ConnectedProver() { - metrics.GaugeInc(currentConnectedProversName) -} - -// DisconnectedProver decrements the gauge for the current number of connected -// provers. -func DisconnectedProver() { - metrics.GaugeDec(currentConnectedProversName) -} - -// WorkingProver increments the gauge for the current number of working -// provers. -func WorkingProver() { - metrics.GaugeInc(currentWorkingProversName) -} - -// IdlingProver decrements the gauge for the current number of working provers. -func IdlingProver() { - metrics.GaugeDec(currentWorkingProversName) -} diff --git a/aggregator/prover/prover.go b/aggregator/prover/prover.go index b6da22d4..b8734984 100644 --- a/aggregator/prover/prover.go +++ b/aggregator/prover/prover.go @@ -7,7 +7,6 @@ import ( "net" "time" - "github.com/0xPolygon/cdk/aggregator/metrics" "github.com/0xPolygon/cdk/config/types" "github.com/0xPolygon/cdk/log" ) @@ -103,8 +102,6 @@ func (p *Prover) SupportsForkID(forkID uint64) bool { // BatchProof instructs the prover to generate a batch proof for the provided // input. It returns the ID of the proof being computed. func (p *Prover) BatchProof(input *StatelessInputProver) (*string, error) { - metrics.WorkingProver() - req := &AggregatorMessage{ Request: &AggregatorMessage_GenStatelessBatchProofRequest{ GenStatelessBatchProofRequest: &GenStatelessBatchProofRequest{Input: input}, @@ -136,8 +133,6 @@ func (p *Prover) BatchProof(input *StatelessInputProver) (*string, error) { // AggregatedProof instructs the prover to generate an aggregated proof from // the two inputs provided. It returns the ID of the proof being computed. func (p *Prover) AggregatedProof(inputProof1, inputProof2 string) (*string, error) { - metrics.WorkingProver() - req := &AggregatorMessage{ Request: &AggregatorMessage_GenAggregatedProofRequest{ GenAggregatedProofRequest: &GenAggregatedProofRequest{ @@ -176,8 +171,6 @@ func (p *Prover) AggregatedProof(inputProof1, inputProof2 string) (*string, erro // FinalProof instructs the prover to generate a final proof for the given // input. It returns the ID of the proof being computed. func (p *Prover) FinalProof(inputProof string, aggregatorAddr string) (*string, error) { - metrics.WorkingProver() - req := &AggregatorMessage{ Request: &AggregatorMessage_GenFinalProofRequest{ GenFinalProofRequest: &GenFinalProofRequest{ @@ -270,8 +263,6 @@ func (p *Prover) WaitFinalProof(ctx context.Context, proofID string) (*FinalProo // waitProof waits for a proof to be generated by the prover and returns the // prover response. func (p *Prover) waitProof(ctx context.Context, proofID string) (*GetProofResponse, error) { - defer metrics.IdlingProver() - req := &AggregatorMessage{ Request: &AggregatorMessage_GetProofRequest{ GetProofRequest: &GetProofRequest{ diff --git a/etherman/types/finalproofinputs.go b/aggregator/types/finalproofinputs.go similarity index 100% rename from etherman/types/finalproofinputs.go rename to aggregator/types/finalproofinputs.go diff --git a/etherman/types/sequence.go b/aggregator/types/sequence.go similarity index 100% rename from etherman/types/sequence.go rename to aggregator/types/sequence.go diff --git a/dataavailability/dataavailability.go b/dataavailability/dataavailability.go index f385d09d..d5968eb3 100644 --- a/dataavailability/dataavailability.go +++ b/dataavailability/dataavailability.go @@ -3,7 +3,7 @@ package dataavailability import ( "context" - "github.com/0xPolygon/cdk/etherman/types" + "github.com/0xPolygon/cdk/aggregator/types" ) // DataAvailability implements an abstract data availability integration diff --git a/encoding/encoding.go b/encoding/encoding.go deleted file mode 100644 index 5568746c..00000000 --- a/encoding/encoding.go +++ /dev/null @@ -1,110 +0,0 @@ -package encoding - -import ( - "fmt" - "math/big" - "strconv" - "strings" - - "github.com/0xPolygon/cdk/hex" - "github.com/ethereum/go-ethereum/common" -) - -const ( - // Base10 decimal base - Base10 = 10 - // TenToThePowerOf18 represents 1000000000000000000 - TenToThePowerOf18 = 1000000000000000000 - // Gwei represents 1000000000 wei - Gwei = 1000000000 - // MaxUint256StrNumber represents 2**256 -1 as string - MaxUint256StrNumber = "115792089237316195423570985008687907853269984665640564039457584007913129639935" -) - -// DecodeUint64orHex decodes a string uint64 or hex string into a uint64 -func DecodeUint64orHex(val *string) (uint64, error) { - if val == nil { - return 0, nil - } - - str := *val - base := 10 - if strings.HasPrefix(str, "0x") { - str = str[2:] - base = 16 - } - return strconv.ParseUint(str, base, hex.BitSize64) -} - -// DecodeUint256orHex decodes a string uint256 or hex string into a bit.Int -func DecodeUint256orHex(val *string) (*big.Int, error) { - if val == nil { - return nil, nil - } - - str := *val - base := 10 - if strings.HasPrefix(str, "0x") { - str = str[2:] - base = 16 - } - b, ok := new(big.Int).SetString(str, base) - if !ok { - return nil, fmt.Errorf("could not parse") - } - return b, nil -} - -// DecodeInt64orHex decodes a string int64 or hex string into a int64 -func DecodeInt64orHex(val *string) (int64, error) { - i, err := DecodeUint64orHex(val) - return int64(i), err -} - -// DecodeBytes decodes a hex string into a []byte -func DecodeBytes(val *string) ([]byte, error) { - if val == nil { - return []byte{}, nil - } - - str := strings.TrimPrefix(*val, "0x") - - return hex.DecodeString(str) -} - -// EncodeUint64 encodes a uint64 into a hex string -func EncodeUint64(b uint64) *string { - res := fmt.Sprintf("0x%x", b) - return &res -} - -// EncodeBytes encodes a []bytes into a hex string -func EncodeBytes(b []byte) *string { - res := "0x" + hex.EncodeToString(b) - return &res -} - -// EncodeBigInt encodes a big.Int into a hex string -func EncodeBigInt(b *big.Int) *string { - res := "0x" + b.Text(hex.Base) - return &res -} - -// DecodeBigIntHexOrDecimal parses a string that can be decimal or hexa (starts with 0x) -// into a *big.Int -func DecodeBigIntHexOrDecimal(s string) (*big.Int, error) { - var r *big.Int - if strings.HasPrefix(s, "0x") { // nolint - // Value in hex format - s = s[2:] - r = new(big.Int).SetBytes(common.Hex2Bytes(s)) - } else { - // Value in decimal format - value, ok := new(big.Int).SetString(s, Base10) - if !ok { - return nil, fmt.Errorf("Could not set base10 %q to big.Int", s) - } - r = value - } - return r, nil -} diff --git a/etherman/aggregator.go b/etherman/aggregator.go index e653c2db..c3ef0382 100644 --- a/etherman/aggregator.go +++ b/etherman/aggregator.go @@ -2,13 +2,13 @@ package etherman import ( "context" + "encoding/hex" "errors" "fmt" "math/big" "strings" - "github.com/0xPolygon/cdk/encoding" - ethmanTypes "github.com/0xPolygon/cdk/etherman/types" + ethmanTypes "github.com/0xPolygon/cdk/aggregator/types" "github.com/0xPolygon/cdk/log" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -101,7 +101,7 @@ func convertProof(p string) ([24][32]byte, error) { proof := [24][32]byte{} for i := 0; i < 24; i++ { data := p[i*64 : (i+1)*64] - p, err := encoding.DecodeBytes(&data) + p, err := DecodeBytes(&data) if err != nil { return [24][32]byte{}, fmt.Errorf("failed to decode proof, err: %w", err) } @@ -111,3 +111,11 @@ func convertProof(p string) ([24][32]byte, error) { } return proof, nil } + +// DecodeBytes decodes a hex string into a []byte +func DecodeBytes(val *string) ([]byte, error) { + if val == nil { + return []byte{}, nil + } + return hex.DecodeString(strings.TrimPrefix(*val, "0x")) +} diff --git a/etherman/etherman.go b/etherman/etherman.go index d9c6476d..b2e61a84 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -12,6 +12,7 @@ import ( "strings" "time" + ethmanTypes "github.com/0xPolygon/cdk/aggregator/types" "github.com/0xPolygon/cdk/etherman/etherscan" "github.com/0xPolygon/cdk/etherman/ethgasstation" "github.com/0xPolygon/cdk/etherman/smartcontracts/dataavailabilityprotocol" @@ -21,7 +22,6 @@ import ( "github.com/0xPolygon/cdk/etherman/smartcontracts/polygonrollupmanager" "github.com/0xPolygon/cdk/etherman/smartcontracts/polygonzkevm" "github.com/0xPolygon/cdk/etherman/smartcontracts/polygonzkevmglobalexitroot" - ethmanTypes "github.com/0xPolygon/cdk/etherman/types" "github.com/0xPolygon/cdk/log" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" diff --git a/etherman/etherscan/etherscan.go b/etherman/etherscan/etherscan.go index a8e98c27..51d3563e 100644 --- a/etherman/etherscan/etherscan.go +++ b/etherman/etherscan/etherscan.go @@ -7,8 +7,13 @@ import ( "io" "math/big" "net/http" +) - "github.com/0xPolygon/cdk/encoding" +const ( + // Base10 decimal base + Base10 = 10 + // Gwei represents 1000000000 wei + Gwei = 1000000000 ) type etherscanResponse struct { @@ -72,6 +77,6 @@ func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) { if err != nil { return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err) } - fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, encoding.Base10) - return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil + fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, Base10) + return new(big.Int).Mul(fgp, big.NewInt(Gwei)), nil } diff --git a/etherman/ethgasstation/ethgasstation.go b/etherman/ethgasstation/ethgasstation.go index cf642299..3d5a82f2 100644 --- a/etherman/ethgasstation/ethgasstation.go +++ b/etherman/ethgasstation/ethgasstation.go @@ -7,8 +7,11 @@ import ( "io" "math/big" "net/http" +) - "github.com/0xPolygon/cdk/encoding" +const ( + // Gwei represents 1000000000 wei + Gwei = 1000000000 ) type ethGasStationResponse struct { @@ -60,5 +63,5 @@ func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) { return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err) } fgp := big.NewInt(0).SetUint64(resBody.GasPrice.Instant) - return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil + return new(big.Int).Mul(fgp, big.NewInt(Gwei)), nil } diff --git a/metrics/api.go b/metrics/api.go deleted file mode 100644 index c21e2cca..00000000 --- a/metrics/api.go +++ /dev/null @@ -1,16 +0,0 @@ -package metrics - -const ( - //Endpoint the endpoint for exposing the metrics - Endpoint = "/metrics" - // ProfilingIndexEndpoint the endpoint for exposing the profiling metrics - ProfilingIndexEndpoint = "/debug/pprof/" - // ProfileEndpoint the endpoint for exposing the profile of the profiling metrics - ProfileEndpoint = "/debug/pprof/profile" - // ProfilingCmdEndpoint the endpoint for exposing the command-line of profiling metrics - ProfilingCmdEndpoint = "/debug/pprof/cmdline" - // ProfilingSymbolEndpoint the endpoint for exposing the symbol of profiling metrics - ProfilingSymbolEndpoint = "/debug/pprof/symbol" - // ProfilingTraceEndpoint the endpoint for exposing the trace of profiling metrics - ProfilingTraceEndpoint = "/debug/pprof/trace" -) diff --git a/metrics/config.go b/metrics/config.go deleted file mode 100644 index 9fe7ef62..00000000 --- a/metrics/config.go +++ /dev/null @@ -1,17 +0,0 @@ -package metrics - -// Config represents the configuration of the metrics -type Config struct { - // Host is the address to bind the metrics server - Host string `mapstructure:"Host"` - // Port is the port to bind the metrics server - Port int `mapstructure:"Port"` - // Enabled is the flag to enable/disable the metrics server - Enabled bool `mapstructure:"Enabled"` - // ProfilingHost is the address to bind the profiling server - ProfilingHost string `mapstructure:"ProfilingHost"` - // ProfilingPort is the port to bind the profiling server - ProfilingPort int `mapstructure:"ProfilingPort"` - // ProfilingEnabled is the flag to enable/disable the profiling server - ProfilingEnabled bool `mapstructure:"ProfilingEnabled"` -} diff --git a/metrics/prometheus.go b/metrics/prometheus.go deleted file mode 100644 index f260d67b..00000000 --- a/metrics/prometheus.go +++ /dev/null @@ -1,677 +0,0 @@ -package metrics - -import ( - "net/http" - "sync" - - "github.com/0xPolygon/cdk/log" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" -) - -var ( - storageMutex sync.RWMutex - registerer prometheus.Registerer - gauges map[string]prometheus.Gauge - counters map[string]prometheus.Counter - counterVecs map[string]*prometheus.CounterVec - histograms map[string]prometheus.Histogram - histogramVecs map[string]*prometheus.HistogramVec - summaries map[string]prometheus.Summary - initialized bool - initOnce sync.Once -) - -// CounterVecOpts holds options for the CounterVec type. -type CounterVecOpts struct { - prometheus.CounterOpts - Labels []string -} - -// HistogramVecOpts holds options for the HistogramVec type. -type HistogramVecOpts struct { - prometheus.HistogramOpts - Labels []string -} - -// Init initializes the package variables. -func Init() { - initOnce.Do(func() { - storageMutex = sync.RWMutex{} - registerer = prometheus.DefaultRegisterer - gauges = make(map[string]prometheus.Gauge) - counters = make(map[string]prometheus.Counter) - counterVecs = make(map[string]*prometheus.CounterVec) - histograms = make(map[string]prometheus.Histogram) - histogramVecs = make(map[string]*prometheus.HistogramVec) - summaries = make(map[string]prometheus.Summary) - initialized = true - }) -} - -// Handler returns the Prometheus http handler. -func Handler() http.Handler { - return promhttp.Handler() -} - -// RegisterGauges registers the provided gauge metrics to the Prometheus -// registerer. -func RegisterGauges(opts ...prometheus.GaugeOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerGaugeIfNotExists(options) - } -} - -// UnregisterGauges unregisters the provided gauge metrics from the Prometheus -// registerer. -func UnregisterGauges(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterGaugeIfExists(name) - } -} - -// Gauge retrieves gauge metric by name -func Gauge(name string) (gauge prometheus.Gauge, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - if gauge, exist = gauges[name]; !exist { - return nil, exist - } - - return gauge, exist -} - -// GaugeSet sets the value for gauge with the given name. -func GaugeSet(name string, value float64) { - if !initialized { - return - } - - if c, ok := Gauge(name); ok { - c.Set(value) - } -} - -// GaugeInc increments the gauge with the given name. -func GaugeInc(name string) { - if !initialized { - return - } - - if g, ok := Gauge(name); ok { - g.Inc() - } -} - -// GaugeDec decrements the gauge with the given name. -func GaugeDec(name string) { - if !initialized { - return - } - - if g, ok := Gauge(name); ok { - g.Dec() - } -} - -// RegisterCounters registers the provided counter metrics to the Prometheus -// registerer. -func RegisterCounters(opts ...prometheus.CounterOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerCounterIfNotExists(options) - } -} - -// Counter retrieves counter metric by name -func Counter(name string) (counter prometheus.Counter, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - if counter, exist = counters[name]; !exist { - return nil, exist - } - - return counter, exist -} - -// CounterInc increments the counter with the given name. -func CounterInc(name string) { - if !initialized { - return - } - - if c, ok := Counter(name); ok { - c.Inc() - } -} - -// CounterAdd increments the counter with the given name. -func CounterAdd(name string, value float64) { - if !initialized { - return - } - - if c, ok := Counter(name); ok { - c.Add(value) - } -} - -// UnregisterCounters unregisters the provided counter metrics from the -// Prometheus registerer. -func UnregisterCounters(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterCounterIfExists(name) - } -} - -// RegisterCounterVecs registers the provided counter vec metrics to the -// Prometheus registerer. -func RegisterCounterVecs(opts ...CounterVecOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerCounterVecIfNotExists(options) - } -} - -// CounterVec retrieves counter ver metric by name -func CounterVec(name string) (counterVec *prometheus.CounterVec, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - counterVec, exist = counterVecs[name] - - return counterVec, exist -} - -// CounterVecInc increments the counter vec with the given name and label. -func CounterVecInc(name string, label string) { - if !initialized { - return - } - - if cv, ok := CounterVec(name); ok { - cv.WithLabelValues(label).Inc() - } -} - -// CounterVecAdd increments the counter vec by the given value, with the given -// name and label. -func CounterVecAdd(name string, label string, value float64) { - if !initialized { - return - } - - if cv, ok := CounterVec(name); ok { - cv.WithLabelValues(label).Add(value) - } -} - -// UnregisterCounterVecs unregisters the provided counter vec metrics from the -// Prometheus registerer. -func UnregisterCounterVecs(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterCounterVecIfExists(name) - } -} - -// RegisterHistograms registers the provided histogram metrics to the -// Prometheus registerer. -func RegisterHistograms(opts ...prometheus.HistogramOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerHistogramIfNotExists(options) - } -} - -// Histogram retrieves histogram metric by name -func Histogram(name string) (histogram prometheus.Histogram, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - if histogram, exist = histograms[name]; !exist { - return nil, exist - } - - return histogram, exist -} - -// HistogramObserve observes the histogram from the given start time. -func HistogramObserve(name string, value float64) { - if !initialized { - return - } - - if histo, ok := Histogram(name); ok { - histo.Observe(value) - } -} - -// UnregisterHistogram unregisters the provided histogram metrics from the -// Prometheus registerer. -func UnregisterHistogram(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterHistogramIfExists(name) - } -} - -// RegisterHistogramVecs registers the provided histogram vec metrics to the -// Prometheus registerer. -func RegisterHistogramVecs(opts ...HistogramVecOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerHistogramVecIfNotExists(options) - } -} - -// HistogramVec retrieves histogram ver metric by name -func HistogramVec(name string) (histgramVec *prometheus.HistogramVec, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - histgramVec, exist = histogramVecs[name] - - return histgramVec, exist -} - -// HistogramVecObserve observes the histogram vec with the given name, label and value. -func HistogramVecObserve(name string, label string, value float64) { - if !initialized { - return - } - - if cv, ok := HistogramVec(name); ok { - cv.WithLabelValues(label).Observe(value) - } -} - -// UnregisterHistogramVecs unregisters the provided histogram vec metrics from the -// Prometheus registerer. -func UnregisterHistogramVecs(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterHistogramVecIfExists(name) - } -} - -// RegisterSummaries registers the provided summary metrics to the Prometheus -// registerer. -func RegisterSummaries(opts ...prometheus.SummaryOpts) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, options := range opts { - registerSummaryIfNotExists(options) - } -} - -// Summary retrieves summary metric by name -func Summary(name string) (summary prometheus.Summary, exist bool) { - if !initialized { - return - } - - storageMutex.RLock() - defer storageMutex.RUnlock() - - if summary, exist = summaries[name]; !exist { - return nil, exist - } - - return summary, exist -} - -// UnregisterSummaries unregisters the provided summary metrics from the -// Prometheus registerer. -func UnregisterSummaries(names ...string) { - if !initialized { - return - } - - storageMutex.Lock() - defer storageMutex.Unlock() - - for _, name := range names { - unregisterSummaryIfExists(name) - } -} - -// registerGaugeIfNotExists registers single gauge metric if not exists -func registerGaugeIfNotExists(opts prometheus.GaugeOpts) { - log := log.WithFields("metricName", opts.Name) - if _, exist := gauges[opts.Name]; exist { - log.Warn("Gauge metric already exists.") - return - } - - log.Debug("Creating Gauge Metric...") - gauge := prometheus.NewGauge(opts) - log.Debugf("Gauge Metric successfully created! Labels: %p", opts.ConstLabels) - - log.Debug("Registering Gauge Metric...") - registerer.MustRegister(gauge) - log.Debug("Gauge Metric successfully registered!") - - gauges[opts.Name] = gauge -} - -// unregisterGaugeIfExists unregisters single gauge metric if exists -func unregisterGaugeIfExists(name string) { - var ( - gauge prometheus.Gauge - ok bool - ) - - log := log.WithFields("metricName", name) - if gauge, ok = gauges[name]; !ok { - log.Warn("Trying to delete non-existing Gauge metrics.") - return - } - - log.Debug("Unregistering Gauge Metric...") - ok = registerer.Unregister(gauge) - if !ok { - log.Error("Failed to unregister Gauge Metric.") - return - } - delete(gauges, name) - log.Debug("Gauge Metric successfully unregistered!") -} - -// registerCounterIfNotExists registers single counter metric if not exists -func registerCounterIfNotExists(opts prometheus.CounterOpts) { - log := log.WithFields("metricName", opts.Name) - if _, exist := counters[opts.Name]; exist { - log.Infof("Counter metric already exists. %s", opts.Name) - return - } - - log.Debug("Creating Counter Metric...") - counter := prometheus.NewCounter(opts) - log.Debugf("Counter Metric successfully created! Labels: %p", opts.ConstLabels) - - log.Debug("Registering Counter Metric...") - registerer.MustRegister(counter) - log.Debug("Counter Metric successfully registered!") - - counters[opts.Name] = counter -} - -// unregisterCounterIfExists unregisters single counter metric if exists -func unregisterCounterIfExists(name string) { - var ( - counter prometheus.Counter - ok bool - ) - - log := log.WithFields("metricName", name) - if counter, ok = counters[name]; !ok { - log.Warn("Trying to delete non-existing Counter counter.") - return - } - - log.Debug("Unregistering Counter Metric...") - ok = registerer.Unregister(counter) - if !ok { - log.Error("Failed to unregister Counter Metric.") - return - } - delete(counters, name) - log.Debugf("Counter Metric '%v' successfully unregistered!", name) -} - -// registerCounterVecIfNotExists registers single counter vec metric if not exists -func registerCounterVecIfNotExists(opts CounterVecOpts) { - log := log.WithFields("metricName", opts.Name) - if _, exist := counterVecs[opts.Name]; exist { - log.Warn("Counter vec metric already exists.") - return - } - - log.Debug("Creating Counter Vec Metric...") - counterVec := prometheus.NewCounterVec(opts.CounterOpts, opts.Labels) - log.Debugf("Counter Vec Metric successfully created! Labels: %p", opts.ConstLabels) - - log.Debug("Registering Counter Vec Metric...") - registerer.MustRegister(counterVec) - log.Debug("Counter Vec Metric successfully registered!") - - counterVecs[opts.Name] = counterVec -} - -// unregisterCounterVecIfExists unregisters single counter metric if exists -func unregisterCounterVecIfExists(name string) { - var ( - counterVec *prometheus.CounterVec - ok bool - ) - - log := log.WithFields("metricName", name) - if counterVec, ok = counterVecs[name]; !ok { - log.Warn("Trying to delete non-existing Counter Vec counter.") - return - } - - log.Debug("Unregistering Counter Vec Metric...") - ok = registerer.Unregister(counterVec) - if !ok { - log.Error("Failed to unregister Counter Vec Metric.") - return - } - delete(counterVecs, name) - log.Debug("Counter Vec Metric successfully unregistered!") -} - -// registerHistogramIfNotExists registers single histogram metric if not exists -func registerHistogramIfNotExists(opts prometheus.HistogramOpts) { - log := log.WithFields("metricName", opts.Name) - if _, exist := histograms[opts.Name]; exist { - log.Infof("Histogram metric already exists. %s", opts.Name) - return - } - - log.Debug("Creating Histogram Metric...") - histogram := prometheus.NewHistogram(opts) - log.Debugf("Histogram Metric successfully created! Labels: %p", opts.ConstLabels) - - log.Debug("Registering Histogram Metric...") - registerer.MustRegister(histogram) - log.Debug("Histogram Metric successfully registered!") - - histograms[opts.Name] = histogram -} - -// unregisterHistogramIfExists unregisters single histogram metric if exists -func unregisterHistogramIfExists(name string) { - var ( - histogram prometheus.Histogram - ok bool - ) - - log := log.WithFields("metricName", name) - if histogram, ok = histograms[name]; !ok { - log.Warn("Trying to delete non-existing Histogram histogram.") - return - } - - log.Debug("Unregistering Histogram Metric...") - ok = registerer.Unregister(histogram) - if !ok { - log.Error("Failed to unregister Histogram Metric.") - return - } - delete(histograms, name) - log.Debug("Histogram Metric successfully unregistered!") -} - -// registerHistogramVecIfNotExists unregisters single counter metric if exists -func registerHistogramVecIfNotExists(opts HistogramVecOpts) { - if _, exist := histogramVecs[opts.Name]; exist { - log.Warnf("Histogram vec metric '%v' already exists.", opts.Name) - return - } - - log.Infof("Creating Histogram Vec Metric '%v' ...", opts.Name) - histogramVec := prometheus.NewHistogramVec(opts.HistogramOpts, opts.Labels) - log.Infof("Histogram Vec Metric '%v' successfully created! Labels: %p", opts.Name, opts.ConstLabels) - - log.Infof("Registering Histogram Vec Metric '%v' ...", opts.Name) - registerer.MustRegister(histogramVec) - log.Infof("Histogram Vec Metric '%v' successfully registered!", opts.Name) - - histogramVecs[opts.Name] = histogramVec -} - -// unregisterHistogramVecIfExists unregisters single histogram metric if exists -func unregisterHistogramVecIfExists(name string) { - var ( - histogramVec *prometheus.HistogramVec - ok bool - ) - - if histogramVec, ok = histogramVecs[name]; !ok { - log.Warnf("Trying to delete non-existing Histogram Vec '%v'.", name) - return - } - - log.Infof("Unregistering Histogram Vec Metric '%v' ...", name) - ok = registerer.Unregister(histogramVec) - if !ok { - log.Errorf("Failed to unregister Histogram Vec Metric '%v'.", name) - return - } - delete(histogramVecs, name) - log.Infof("Histogram Vec Metric '%v' successfully unregistered!", name) -} - -// registerSummaryIfNotExists registers single summary metric if not exists -func registerSummaryIfNotExists(opts prometheus.SummaryOpts) { - log := log.WithFields("metricName", opts.Name) - if _, exist := summaries[opts.Name]; exist { - log.Warn("Summary metric already exists.") - return - } - - log.Debug("Creating Summary Metric...") - summary := prometheus.NewSummary(opts) - log.Debugf("Summary Metric successfully created! Labels: %p", opts.ConstLabels) - - log.Debug("Registering Summary Metric...") - registerer.MustRegister(summary) - log.Debug("Summary Metric successfully registered!") - - summaries[opts.Name] = summary -} - -// unregisterSummaryIfExists unregisters single summary metric if exists -func unregisterSummaryIfExists(name string) { - var ( - summary prometheus.Summary - ok bool - ) - - log := log.WithFields("metricName", name) - if summary, ok = summaries[name]; !ok { - log.Warn("Trying to delete non-existing Summary summary.") - return - } - - log.Debug("Unregistering Summary Metric...") - ok = registerer.Unregister(summary) - if !ok { - log.Error("Failed to unregister Summary Metric.") - return - } - delete(summaries, name) - log.Debug("Summary Metric successfully unregistered!") -} diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go deleted file mode 100644 index 3cf5683e..00000000 --- a/metrics/prometheus_test.go +++ /dev/null @@ -1,371 +0,0 @@ -package metrics - -import ( - "sync" - "testing" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/testutil" - dto "github.com/prometheus/client_model/go" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -var ( - gaugeName = "gaugeName" - gaugeOpts = prometheus.GaugeOpts{Name: gaugeName} - gauge prometheus.Gauge - counterName = "counterName" - counterOpts = prometheus.CounterOpts{Name: counterName} - counter prometheus.Counter - counterVecName = "counterVecName" - counterVecLabelName = "counterVecLabelName" - counterVecLabelVal = "counterVecLabelVal" - counterVecOpts = CounterVecOpts{prometheus.CounterOpts{Name: counterVecName}, []string{counterVecLabelName}} - counterVec *prometheus.CounterVec - histogramName = "histogramName" - histogramOpts = prometheus.HistogramOpts{Name: histogramName, Buckets: []float64{0.5, 10, 20}} - histogram prometheus.Histogram - histogramVecName = "histogramVecName" - histogramVecLabelName = "histogramVecLabelName" - histogramVecLabelVal = "histogramVecLabelVal" - histogramVecOpts = HistogramVecOpts{prometheus.HistogramOpts{Name: histogramVecName}, []string{histogramVecLabelName}} - histogramVec *prometheus.HistogramVec - summaryName = "summaryName" - summaryOpts = prometheus.SummaryOpts{Name: summaryName} - summary = prometheus.NewSummary(summaryOpts) -) - -func setup() { - Init() - gauge = prometheus.NewGauge(gaugeOpts) - counter = prometheus.NewCounter(counterOpts) - counterVec = prometheus.NewCounterVec(counterVecOpts.CounterOpts, counterVecOpts.Labels) - histogram = prometheus.NewHistogram(histogramOpts) - histogramVec = prometheus.NewHistogramVec(histogramVecOpts.HistogramOpts, histogramVecOpts.Labels) - summary = prometheus.NewSummary(summaryOpts) - - // Overriding registerer to be able to do the unit tests independently - registerer = prometheus.NewRegistry() -} - -func cleanup() { - initialized = false - initOnce = sync.Once{} -} - -func TestHandler(t *testing.T) { - setup() - defer cleanup() - - actual := Handler() - - assert.NotNil(t, actual) -} - -func TestRegisterGauges(t *testing.T) { - setup() - defer cleanup() - gaugesOpts := []prometheus.GaugeOpts{gaugeOpts} - - RegisterGauges(gaugesOpts...) - - assert.Len(t, gauges, 1) -} - -func TestGauge(t *testing.T) { - setup() - defer cleanup() - gauges[gaugeName] = gauge - - actual, exist := Gauge(gaugeName) - - assert.True(t, exist) - assert.Equal(t, gauge, actual) -} - -func TestGaugeSet(t *testing.T) { - setup() - defer cleanup() - gauges[gaugeName] = gauge - expected := float64(2) - - GaugeSet(gaugeName, expected) - actual := testutil.ToFloat64(gauge) - - assert.Equal(t, expected, actual) -} - -func TestGaugeInc(t *testing.T) { - setup() - defer cleanup() - gauges[gaugeName] = gauge - expected := float64(1) - - GaugeInc(gaugeName) - actual := testutil.ToFloat64(gauge) - - assert.Equal(t, expected, actual) -} - -func TestGaugeDec(t *testing.T) { - setup() - defer cleanup() - gauges[gaugeName] = gauge - gauge.Set(2) - expected := float64(1) - - GaugeDec(gaugeName) - actual := testutil.ToFloat64(gauge) - - assert.Equal(t, expected, actual) -} - -func TestUnregisterGauges(t *testing.T) { - setup() - defer cleanup() - RegisterGauges(gaugeOpts) - - UnregisterGauges(gaugeName) - - assert.Len(t, gauges, 0) -} - -func TestRegisterCounters(t *testing.T) { - setup() - defer cleanup() - countersOpts := []prometheus.CounterOpts{counterOpts} - - RegisterCounters(countersOpts...) - - assert.Len(t, counters, 1) -} - -func TestCounter(t *testing.T) { - setup() - defer cleanup() - counters[counterName] = counter - - actual, exist := Counter(counterName) - - assert.True(t, exist) - assert.Equal(t, counter, actual) -} - -func TestCounterInc(t *testing.T) { - setup() - defer cleanup() - counters[counterName] = counter - expected := float64(1) - - CounterInc(counterName) - actual := testutil.ToFloat64(counter) - - assert.Equal(t, expected, actual) -} - -func TestCounterAdd(t *testing.T) { - setup() - defer cleanup() - counters[counterName] = counter - expected := float64(2) - - CounterAdd(counterName, expected) - actual := testutil.ToFloat64(counter) - - assert.Equal(t, expected, actual) -} - -func TestUnregisterCounters(t *testing.T) { - setup() - defer cleanup() - RegisterCounters(counterOpts) - - UnregisterCounters(counterName) - - assert.Len(t, counters, 0) -} - -func TestRegisterCounterVecs(t *testing.T) { - setup() - defer cleanup() - counterVecsOpts := []CounterVecOpts{counterVecOpts} - - RegisterCounterVecs(counterVecsOpts...) - - assert.Len(t, counterVecs, 1) -} - -func TestCounterVec(t *testing.T) { - setup() - defer cleanup() - counterVecs[counterVecName] = counterVec - - actual, exist := CounterVec(counterVecName) - - assert.True(t, exist) - assert.Equal(t, counterVec, actual) -} - -func TestCounterVecInc(t *testing.T) { - setup() - defer cleanup() - counterVecs[counterVecName] = counterVec - expected := float64(1) - - CounterVecInc(counterVecName, counterVecLabelVal) - currCounterVec, err := counterVec.GetMetricWithLabelValues(counterVecLabelVal) - require.NoError(t, err) - actual := testutil.ToFloat64(currCounterVec) - - assert.Equal(t, expected, actual) -} - -func TestCounterVecAdd(t *testing.T) { - setup() - defer cleanup() - counterVecs[counterVecName] = counterVec - expected := float64(2) - - CounterVecAdd(counterVecName, counterVecLabelVal, expected) - currCounterVec, err := counterVec.GetMetricWithLabelValues(counterVecLabelVal) - require.NoError(t, err) - actual := testutil.ToFloat64(currCounterVec) - - assert.Equal(t, expected, actual) -} - -func TestUnregisterCounterVecs(t *testing.T) { - setup() - defer cleanup() - RegisterCounterVecs(counterVecOpts) - - UnregisterCounterVecs(counterVecName) - - assert.Len(t, counterVecs, 0) -} - -func TestRegisterHistograms(t *testing.T) { - setup() - defer cleanup() - histogramsOpts := []prometheus.HistogramOpts{histogramOpts} - - RegisterHistograms(histogramsOpts...) - - assert.Len(t, histograms, 1) -} - -func TestHistogram(t *testing.T) { - setup() - defer cleanup() - histograms[histogramName] = histogram - - actual, exist := Histogram(histogramName) - - assert.True(t, exist) - assert.Equal(t, histogram, actual) -} - -func TestHistogramObserve(t *testing.T) { - setup() - defer cleanup() - histograms[histogramName] = histogram - - expected := 42.0 - - HistogramObserve(histogramName, expected) - - m := &dto.Metric{} - require.NoError(t, histogram.Write(m)) - h := m.GetHistogram() - actual := h.GetSampleSum() - assert.Equal(t, expected, actual) -} - -func TestUnregisterHistograms(t *testing.T) { - setup() - defer cleanup() - RegisterHistograms(histogramOpts) - - UnregisterHistogram(histogramName) - - assert.Len(t, histograms, 0) -} - -func TestRegisterHistogramVecs(t *testing.T) { - setup() - defer cleanup() - histogramVecsOpts := []HistogramVecOpts{histogramVecOpts} - - RegisterHistogramVecs(histogramVecsOpts...) - - assert.Len(t, histogramVecs, 1) -} - -func TestHistogramVec(t *testing.T) { - setup() - defer cleanup() - histogramVecs[histogramVecName] = histogramVec - - actual, exist := HistogramVec(histogramVecName) - - assert.True(t, exist) - assert.Equal(t, histogramVec, actual) -} - -func TestHistogramVecObserve(t *testing.T) { - setup() - defer cleanup() - histogramVecs[histogramVecName] = histogramVec - expected := float64(2) - - HistogramVecObserve(histogramVecName, histogramVecLabelVal, expected) - - currHistogramVec := histogramVec.WithLabelValues(histogramVecLabelVal) - m := &dto.Metric{} - require.NoError(t, currHistogramVec.(prometheus.Histogram).Write(m)) - h := m.GetHistogram() - actual := h.GetSampleSum() - assert.Equal(t, expected, actual) -} - -func TestUnregisterHistogramVecs(t *testing.T) { - setup() - defer cleanup() - RegisterHistogramVecs(histogramVecOpts) - - UnregisterHistogramVecs(histogramVecName) - - assert.Len(t, histogramVecs, 0) -} - -func TestRegisterSummaries(t *testing.T) { - setup() - defer cleanup() - summariesOpts := []prometheus.SummaryOpts{summaryOpts} - - RegisterSummaries(summariesOpts...) - - assert.Len(t, summaries, 1) -} - -func TestSummary(t *testing.T) { - setup() - defer cleanup() - summaries[summaryName] = summary - - actual, exist := Summary(summaryName) - - assert.True(t, exist) - assert.Equal(t, summary, actual) -} - -func TestUnregisterSummaries(t *testing.T) { - setup() - defer cleanup() - RegisterSummaries(summaryOpts) - - UnregisterSummaries(summaryName) - - assert.Len(t, summaries, 0) -} diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index cea66a97..239fcd34 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -12,9 +12,9 @@ import ( "sync" "time" + "github.com/0xPolygon/cdk/aggregator/types" "github.com/0xPolygon/cdk/dataavailability" "github.com/0xPolygon/cdk/etherman" - "github.com/0xPolygon/cdk/etherman/types" "github.com/0xPolygon/cdk/log" "github.com/0xPolygon/cdk/state" "github.com/0xPolygon/cdk/state/datastream"