Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus client & seq metrics & Block/Batch Info #122

Open
wants to merge 33 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f4c6983
add time logs print
bylingo Aug 5, 2024
a13edff
update
zjg555543 Aug 5, 2024
c825458
Merge branch 'zjg/fix-ds-lost' into bylingo/timelog
zjg555543 Aug 5, 2024
627a317
add prometheus client
bylingo Jul 23, 2024
dc94337
add block log print
bylingo Aug 7, 2024
97ece01
add batch log print
bylingo Aug 7, 2024
7641db4
add metrics BatchExecuteTime
bylingo Aug 9, 2024
27db608
add PoolTxCount gauge
bylingo Aug 19, 2024
0809e1d
update
zjg555543 Aug 19, 2024
ff13c6c
fix(w3d): add decode kms cache
zibuyu28 Aug 19, 2024
00d48d9
fix(w3d): add decode kms cache
zibuyu28 Aug 19, 2024
0706682
fix(erigon): add log
zibuyu28 Aug 19, 2024
24711ab
update
zjg555543 Aug 20, 2024
8a52fae
update
zjg555543 Aug 20, 2024
b7d49de
updater
zjg555543 Aug 20, 2024
2773822
update
zjg555543 Aug 20, 2024
3f84757
Merge branch 'dev' into bylingo/timelog
zjg555543 Aug 20, 2024
3c795cd
update
zjg555543 Aug 20, 2024
042901d
update
zjg555543 Aug 20, 2024
e70e106
update
zjg555543 Aug 20, 2024
5b6a2de
update
zjg555543 Aug 20, 2024
06e4a20
update
zjg555543 Aug 20, 2024
f599854
update
zjg555543 Aug 20, 2024
12025be
update
zjg555543 Aug 20, 2024
637d004
update
zjg555543 Aug 20, 2024
1eaa522
code reivew 1
bylingo Aug 22, 2024
a7cf2c1
add erigon testnet genesis file
bylingo Aug 27, 2024
37aea38
txCommit only
bylingo Aug 30, 2024
a2f4642
merge dev 240902
bylingo Sep 3, 2024
d27f070
remove testnet modifications
bylingo Sep 4, 2024
9085446
code review 2
bylingo Sep 4, 2024
55cd5dd
update
zjg555543 Sep 11, 2024
2a08772
Merge branch 'dev' into bylingo/timelog
zjg555543 Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cmd/cdk-erigon/run_xlayer.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
package main

import (
"fmt"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/node/nodecfg"
"github.com/ledgerwatch/erigon/zk/apollo"
"github.com/ledgerwatch/erigon/zk/metrics"
"github.com/ledgerwatch/log/v3"
"github.com/prometheus/client_golang/prometheus/promhttp"
zjg555543 marked this conversation as resolved.
Show resolved Hide resolved
"net"
"net/http"
"time"
)

const (
MetricsEndpoint = "/metrics"
)

func initRunForXLayer(ethCfg *ethconfig.Config, nodeCfg *nodecfg.Config) {
apolloClient := apollo.NewClient(ethCfg, nodeCfg)
if apolloClient.LoadConfig() {
log.Info("apollo config loaded")
}

// Start Metrics Server
if ethCfg.Zk.XLayer.Metrics.Enabled {
metrics.XLayerMetricsInit()
go startMetricsHttpServer(ethCfg.Zk.XLayer.Metrics)
}
}

func startMetricsHttpServer(c ethconfig.MetricsConfig) {
const ten = 10
bylingo marked this conversation as resolved.
Show resolved Hide resolved
mux := http.NewServeMux()
address := fmt.Sprintf("%s:%d", c.Host, c.Port)
lis, err := net.Listen("tcp", address)
if err != nil {
log.Error("failed to create tcp listener for metrics", "err", err)
return
}
mux.Handle(MetricsEndpoint, promhttp.Handler())

metricsServer := &http.Server{
Handler: mux,
ReadHeaderTimeout: ten * time.Second,
ReadTimeout: ten * time.Second,
}
log.Info("metrics server listening on port", c.Port)
if err := metricsServer.Serve(lis); err != nil {
if err == http.ErrServerClosed {
log.Warn("http server for metrics stopped")
return
}
log.Error("closed http connection for metrics server", "err", err)
return
}
}
3 changes: 2 additions & 1 deletion cmd/rpcdaemon/commands/send_transaction_zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ledgerwatch/erigon/zk/sequencer"
"github.com/ledgerwatch/erigon/zk/zkchainconfig"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/client"
"github.com/pkg/errors"
)

func (api *APIImpl) isPoolManagerAddressSet() bool {
Expand All @@ -24,7 +25,7 @@ func (api *APIImpl) isZkNonSequencer(chainId *big.Int) bool {
func (api *APIImpl) sendTxZk(rpcUrl string, encodedTx hexutility.Bytes, chainId uint64) (common.Hash, error) {
res, err := client.JSONRPCCall(rpcUrl, "eth_sendRawTransaction", encodedTx)
if err != nil {
return common.Hash{}, err
return common.Hash{}, errors.Wrap(err, "failed to send transaction with zk rpc call")
}

if res.Error != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/utils/flags_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ var (
Usage: "Full batch sleep duration is the time the sequencer sleeps between each full batch iteration.",
Value: 0 * time.Second,
}
// X Layer Prometheus Metrics
XLMetricsHostFlag = cli.StringFlag{
Name: "zkevm.metrics-host",
Usage: "prometheus metrics host",
Value: "",
}
zjg555543 marked this conversation as resolved.
Show resolved Hide resolved
XLMetricsPortFlag = cli.StringFlag{
Name: "zkevm.metrics-port",
Usage: "prometheus metrics port",
Value: "",
}
XLMetricsEnabledFlag = cli.StringFlag{
Name: "zkevm.metrics-enabled",
Usage: "prometheus metrics enabled",
Value: "",
}
)

func setGPOXLayer(ctx *cli.Context, cfg *gaspricecfg.Config) {
Expand Down
11 changes: 11 additions & 0 deletions eth/ethconfig/config_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type XLayerConfig struct {
Apollo ApolloClientConfig
Nacos NacosConfig
EnableInnerTx bool
Metrics MetricsConfig
// Sequencer
SequencerBatchSleepDuration time.Duration
}
Expand All @@ -28,3 +29,13 @@ type ApolloClientConfig struct {
AppID string
NamespaceName string
}

// MetricsConfig is the config for prometheus metrics
type MetricsConfig struct {
// Host is the address to bind the metrics server
Host string
// Port is the port to bind the metrics server
Port int
// Enabled is the flag to enable/disable the metrics server
Enabled bool
}
17 changes: 16 additions & 1 deletion test/config/test.erigon.rpc.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ http.port: 8545
http.vhosts: any
http.corsdomain: any
ws: true

zkevm.apollo-enabled: false
zkevm.apollo-ip-addr: ""
zkevm.apollo-app-id: ""
zkevm.apollo-namespace-name: ""

zkevm.nacos-urls: ""
zkevm.nacos-namespace-id: ""
zkevm.nacos-application-name: ""
zkevm.nacos-external-listen-addr: ""

zkevm.metrics-host: "0.0.0.0"
zkevm.metrics-port: "9095"
zkevm.metrics-enabled: true

db.read.concurrency: 20000
txpool.globalslots: 100000
networkid: 195
Expand All @@ -55,4 +70,4 @@ zkevm.pool-manager-url: http://erigon-pool-manager:8545

http.methodratelimit: "{\"methods\":[\"eth_syncing\"],\"count\":10,\"bucket\":1}"
#http.apikeys: |
# {"project":"project1","key":"944cd2a6939eb23053289d9b91d6c498","timeout":"2033-12-12","methods":["eth_syncing"],"count":5,"bucket":1}
# {"project":"project1","key":"944cd2a6939eb23053289d9b91d6c498","timeout":"2033-12-12","methods":["eth_syncing"],"count":5,"bucket":1}
4 changes: 4 additions & 0 deletions test/config/test.erigon.seq.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ zkevm.nacos-namespace-id: ""
zkevm.nacos-application-name: ""
zkevm.nacos-external-listen-addr: ""

zkevm.metrics-host: "0.0.0.0"
zkevm.metrics-port: "9095"
zkevm.metrics-enabled: true

db.read.concurrency: 20000
txpool.globalslots: 100000
txpool.globalbasefeeslots: 100000
Expand Down
2 changes: 2 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ services:
ports:
- 8123:8545
- 6900:6900
- 9092:9095
volumes:
- ./config/test.erigon.seq.config.yaml:/usr/src/app/config.yaml
- ./config/dynamic-mynetwork-allocs.json:/usr/src/app/dynamic-mynetwork-allocs.json
Expand All @@ -132,6 +133,7 @@ services:
ports:
- 8124:8545
- 6901:6900
- 9091:9095
volumes:
- ./config/test.erigon.rpc.config.yaml:/usr/src/app/config.yaml
- ./config/dynamic-mynetwork-allocs.json:/usr/src/app/dynamic-mynetwork-allocs.json
Expand Down
3 changes: 3 additions & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,7 @@ var DefaultFlags = []cli.Flag{
&utils.TxPoolWhiteList,
&utils.TxPoolBlockedList,
&utils.SequencerBatchSleepDuration,
&utils.XLMetricsEnabledFlag,
&utils.XLMetricsHostFlag,
&utils.XLMetricsPortFlag,
}
5 changes: 5 additions & 0 deletions turbo/cli/flags_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ func ApplyFlagsForXLayerConfig(ctx *cli.Context, cfg *ethconfig.Config) {
},
EnableInnerTx: ctx.Bool(utils.AllowInternalTransactions.Name),
SequencerBatchSleepDuration: ctx.Duration(utils.SequencerBatchSleepDuration.Name),
Metrics: ethconfig.MetricsConfig{
Enabled: ctx.Bool(utils.XLMetricsEnabledFlag.Name),
Host: ctx.String(utils.XLMetricsHostFlag.Name),
Port: ctx.Int(utils.XLMetricsPortFlag.Name),
},
}
}
24 changes: 12 additions & 12 deletions zk/datastream/server/datastream_populate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func (srv *DataStreamServer) WriteWholeBatchToStream(
batchNum uint64,
) error {
var err error
if err = srv.stream.StartAtomicOp(); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

blocksForBatch, err := reader.GetL2BlockNosByBatch(batchNum)
if err != nil {
Expand All @@ -58,6 +54,11 @@ func (srv *DataStreamServer) WriteWholeBatchToStream(
return err
}

if err = srv.stream.StartAtomicOp(); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

blocks := make([]eritypes.Block, 0)
txsPerBlock := make(map[uint64][]eritypes.Transaction)
for blockNumber := fromBlockNum; blockNumber <= toBlockNum; blockNumber++ {
Expand Down Expand Up @@ -116,14 +117,14 @@ func (srv *DataStreamServer) WriteBlocksToStreamConsecutively(
return err
}

if err = srv.stream.StartAtomicOp(); err != nil {
if err = srv.UnwindIfNecessary(logPrefix, reader, from, latestbatchNum, batchNum); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

if err = srv.UnwindIfNecessary(logPrefix, reader, from, latestbatchNum, batchNum); err != nil {
if err = srv.stream.StartAtomicOp(); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

// check if a new batch starts and the old needs closing before that
// if it is already closed with a batch end, do not add a new batch end
Expand Down Expand Up @@ -220,16 +221,15 @@ func (srv *DataStreamServer) WriteBlockWithBatchStartToStream(
t := utils.StartTimer("write-stream", "writeblockstostream")
defer t.LogTimer()

if err = srv.stream.StartAtomicOp(); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

blockNum := block.NumberU64()

if err = srv.UnwindIfNecessary(logPrefix, reader, blockNum, prevBlockBatchNum, batchNum); err != nil {
return err
}
if err = srv.stream.StartAtomicOp(); err != nil {
return err
}
defer srv.stream.RollbackAtomicOp()

// if start of new batch add batch start entries
var batchStartEntries *DataStreamEntries
Expand Down
65 changes: 65 additions & 0 deletions zk/metrics/metrics_xlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package metrics

import (
"fmt"
"github.com/ledgerwatch/log/v3"
"github.com/prometheus/client_golang/prometheus"
"time"
)

// BatchFinalizeTypeLabel batch finalize type label
type BatchFinalizeTypeLabel string

const (
// BatchFinalizeTypeLabelDeadline batch finalize type deadline label
BatchFinalizeTypeLabelDeadline BatchFinalizeTypeLabel = "deadline"
// BatchFinalizeTypeLabelFullBatch batch finalize type full batch label
BatchFinalizeTypeLabelFullBatch BatchFinalizeTypeLabel = "full_batch"
)

const (
CounterOverflow = "BatchCounterOverflow"
EmptyTimeOut = "EmptyBatchTimeOut"
NonEmptyTimeOut = "NonEmptyBatchTimeOut"
)

var (
SeqPrefix = "sequencer_"
// BatchExecuteTimeName is the name of the metric that shows the batch execution time.
BatchExecuteTimeName = SeqPrefix + "batch_execute_time"
PoolTxCountName = SeqPrefix + "pool_tx_count"
)

func XLayerMetricsInit() {
prometheus.MustRegister(BatchExecuteTimeGauge)
prometheus.MustRegister(PoolTxCount)
}

var BatchExecuteTimeGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: BatchExecuteTimeName,
Help: "[SEQUENCER] batch execution time in second",
},
[]string{"closingReason"},
)

var PoolTxCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: PoolTxCountName,
Help: "[SEQUENCER] txcount of each pool in txpool",
},
[]string{"poolName"},
)

// BatchExecuteTime sets the gauge vector to the given batch type and time.
func BatchExecuteTime(closingReason string, duration time.Duration) {
log.Info(fmt.Sprintf("[BatchExecuteTime] ClosingReason: %s, Duration: %.2fs", closingReason, duration.Seconds()))
BatchExecuteTimeGauge.WithLabelValues(closingReason).Set(duration.Seconds())
}

func AddPoolTxCount(pending, basefee, queued int) {
log.Info(fmt.Sprintf("[PoolTxCount] pending: %d, basefee: %d, queued: %d", pending, basefee, queued))
PoolTxCount.WithLabelValues("pending").Set(float64(pending))
PoolTxCount.WithLabelValues("basefee").Set(float64(basefee))
PoolTxCount.WithLabelValues("queued").Set(float64(queued))
}
Loading