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

read metrics config from yaml file #7089

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion cmd/sentry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/spf13/cobra"

"github.com/ledgerwatch/erigon/cmd/sentry/sentry"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/common/paths"
"github.com/ledgerwatch/erigon/turbo/debug"
logging2 "github.com/ledgerwatch/erigon/turbo/logging"
node2 "github.com/ledgerwatch/erigon/turbo/node"
"github.com/spf13/cobra"
)

// generate the messages
Expand All @@ -33,6 +34,7 @@ var (
maxPeers int
maxPendPeers int
healthCheck bool
metrics bool
)

func init() {
Expand All @@ -52,10 +54,15 @@ func init() {
rootCmd.Flags().IntVar(&maxPeers, utils.MaxPeersFlag.Name, utils.MaxPeersFlag.Value, utils.MaxPeersFlag.Usage)
rootCmd.Flags().IntVar(&maxPendPeers, utils.MaxPendingPeersFlag.Name, utils.MaxPendingPeersFlag.Value, utils.MaxPendingPeersFlag.Usage)
rootCmd.Flags().BoolVar(&healthCheck, utils.HealthCheckFlag.Name, false, utils.HealthCheckFlag.Usage)
rootCmd.Flags().BoolVar(&metrics, utils.MetricsEnabledFlag.Name, false, utils.MetricsEnabledFlag.Usage)

if err := rootCmd.MarkFlagDirname(utils.DataDirFlag.Name); err != nil {
panic(err)
}

if err := debug.SetCobraFlagsFromConfigFile(rootCmd); err != nil {
panic(err)
}
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -85,6 +92,7 @@ var rootCmd = &cobra.Command{
uint(port),
protocol,
allowedPorts,
metrics,
)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ func NewP2PConfig(
port uint,
protocol uint,
allowedPorts []uint,
metricsEnabled bool,
) (*p2p.Config, error) {
var enodeDBPath string
switch protocol {
Expand Down Expand Up @@ -935,6 +936,7 @@ func NewP2PConfig(
NodeDatabase: enodeDBPath,
AllowedPorts: allowedPorts,
TmpDir: dirs.Tmp,
MetricsEnabled: metricsEnabled,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure p2pconfig is right place for global metrics enabling. but it's fine

}
if netRestrict != "" {
cfg.NetRestrict = new(netutil.Netlist)
Expand Down Expand Up @@ -1098,6 +1100,10 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config, nodeName, datadir string) {
cfg.DiscoveryV5 = ctx.Bool(DiscoveryV5Flag.Name)
}

if ctx.IsSet(MetricsEnabledFlag.Name) {
cfg.MetricsEnabled = ctx.Bool(MetricsEnabledFlag.Name)
}

ethPeers := cfg.MaxPeers
cfg.Name = nodeName
log.Info("Maximum peer count", "ETH", ethPeers, "total", cfg.MaxPeers)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.18

require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230313075942-ae07c2744483
github.com/ledgerwatch/erigon-lib v0.0.0-20230313125449-2155965bd62d
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3
github.com/ledgerwatch/log/v3 v3.7.0
github.com/ledgerwatch/secp256k1 v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230313075942-ae07c2744483 h1:59UeMgiynV+JflMPfQCH+I4qxVIer0wSgtEJvX+sUV8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230313075942-ae07c2744483/go.mod h1:sKLWgIyFuajTu7nu+cahKhReP+CZW57R5jFUYtzvn44=
github.com/ledgerwatch/erigon-lib v0.0.0-20230313125449-2155965bd62d h1:TEbIpIPOXtLvBd9WLCGFRHmSGAF+GJ0y1TX3g5L78jo=
github.com/ledgerwatch/erigon-lib v0.0.0-20230313125449-2155965bd62d/go.mod h1:sKLWgIyFuajTu7nu+cahKhReP+CZW57R5jFUYtzvn44=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3 h1:tfzawK1gIIgRjVZeANXOr0Ziu+kqCIBuKMe0TXfl5Aw=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230306083105-1391330d62a3/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=
Expand Down
9 changes: 0 additions & 9 deletions metrics/init_test.go

This file was deleted.

34 changes: 18 additions & 16 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"time"

metrics2 "github.com/VictoriaMetrics/metrics"
"github.com/ledgerwatch/erigon-lib/common/metrics"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/common/mclock"
"github.com/ledgerwatch/erigon/event"
"github.com/ledgerwatch/erigon/p2p/enode"
"github.com/ledgerwatch/erigon/p2p/enr"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/log/v3"
)

var (
Expand Down Expand Up @@ -117,16 +117,17 @@ type Peer struct {
disc chan DiscReason

// events receives message send / receive events if set
events *event.Feed
pubkey [64]byte
events *event.Feed
pubkey [64]byte
metricsEnabled bool
}

// NewPeer returns a peer for testing purposes.
func NewPeer(id enode.ID, pubkey [64]byte, name string, caps []Cap) *Peer {
func NewPeer(id enode.ID, pubkey [64]byte, name string, caps []Cap, metricsEnabled bool) *Peer {
pipe, _ := net.Pipe()
node := enode.SignNull(new(enr.Record), id)
conn := &conn{fd: pipe, transport: nil, node: node, caps: caps, name: name}
peer := newPeer(log.Root(), conn, nil, pubkey)
peer := newPeer(log.Root(), conn, nil, pubkey, metricsEnabled)
close(peer.closed) // ensures Disconnect doesn't block
return peer
}
Expand Down Expand Up @@ -209,17 +210,18 @@ func (p *Peer) Inbound() bool {
return p.rw.is(inboundConn)
}

func newPeer(logger log.Logger, conn *conn, protocols []Protocol, pubkey [64]byte) *Peer {
func newPeer(logger log.Logger, conn *conn, protocols []Protocol, pubkey [64]byte, metricsEnabled bool) *Peer {
protomap := matchProtocols(protocols, conn.caps, conn)
p := &Peer{
rw: conn,
running: protomap,
created: mclock.Now(),
disc: make(chan DiscReason),
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
closed: make(chan struct{}),
log: logger.New("id", conn.node.ID(), "conn", conn.flags),
pubkey: pubkey,
rw: conn,
running: protomap,
created: mclock.Now(),
disc: make(chan DiscReason),
protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
closed: make(chan struct{}),
log: logger.New("id", conn.node.ID(), "conn", conn.flags),
pubkey: pubkey,
metricsEnabled: metricsEnabled,
}
return p
}
Expand Down Expand Up @@ -335,7 +337,7 @@ func (p *Peer) handle(msg Msg) error {
if err != nil {
return fmt.Errorf("msg code out of range: %v", msg.Code)
}
if metrics.Enabled {
if p.metricsEnabled {
m := fmt.Sprintf("%s_%s_%d_%#02x", ingressMeterName, proto.Name, proto.Version, msg.Code-proto.offset)
metrics2.GetOrCreateCounter(m).Set(uint64(msg.meterSize))
metrics2.GetOrCreateCounter(m + "_packets").Set(1)
Expand Down
7 changes: 4 additions & 3 deletions p2p/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (
"testing"
"time"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/p2p/enode"
"github.com/ledgerwatch/erigon/p2p/enr"
"github.com/ledgerwatch/log/v3"
)

var discard = Protocol{
Expand Down Expand Up @@ -98,7 +99,7 @@ func testPeer(protos []Protocol) (func(), *conn, *Peer, <-chan error) {
c2.caps = append(c2.caps, p.cap())
}

peer := newPeer(log.Root(), c1, protos, [64]byte{1})
peer := newPeer(log.Root(), c1, protos, [64]byte{1}, true)
errc := make(chan error, 1)
go func() {
_, err := peer.run()
Expand Down Expand Up @@ -250,7 +251,7 @@ func TestNewPeer(t *testing.T) {
name := "nodename"
caps := []Cap{{"foo", 2}, {"bar", 3}}
id := randomID()
p := NewPeer(id, [64]byte{1}, name, caps)
p := NewPeer(id, [64]byte{1}, name, caps, true)
if p.ID() != id {
t.Errorf("ID mismatch: got %v, expected %v", p.ID(), id)
}
Expand Down
7 changes: 5 additions & 2 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

"golang.org/x/sync/semaphore"

"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/common/mclock"
Expand All @@ -42,7 +44,6 @@ import (
"github.com/ledgerwatch/erigon/p2p/enr"
"github.com/ledgerwatch/erigon/p2p/nat"
"github.com/ledgerwatch/erigon/p2p/netutil"
"github.com/ledgerwatch/log/v3"
)

const (
Expand Down Expand Up @@ -171,6 +172,8 @@ type Config struct {
clock mclock.Clock //nolint:structcheck

TmpDir string

MetricsEnabled bool
}

// Server manages all peer connections.
Expand Down Expand Up @@ -1057,7 +1060,7 @@ func (srv *Server) checkpoint(c *conn, stage chan<- *conn) error {
}

func (srv *Server) launchPeer(c *conn, pubkey [64]byte) *Peer {
p := newPeer(srv.log, c, srv.Protocols, pubkey)
p := newPeer(srv.log, c, srv.Protocols, pubkey, srv.MetricsEnabled)
if srv.EnableMsgEvents {
// If message events are enabled, pass the peerFeed
// to the peer.
Expand Down
Loading