diff --git a/api/service/prometheus/service.go b/api/service/prometheus/service.go index 3e43bdc0a..43f33d9ad 100644 --- a/api/service/prometheus/service.go +++ b/api/service/prometheus/service.go @@ -11,6 +11,8 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/metrics" + eth_prometheus "github.com/ethereum/go-ethereum/metrics/prometheus" "github.com/harmony-one/harmony/internal/utils" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -104,6 +106,7 @@ func newService(cfg Config, additionalHandlers ...Handler) *Service { mux := http.NewServeMux() mux.Handle("/metrics", handler) + mux.Handle("/metrics/eth", eth_prometheus.Handler(metrics.DefaultRegistry)) mux.HandleFunc("/goroutinez", svc.goroutinezHandler) // Register additional handlers. diff --git a/p2p/host.go b/p2p/host.go index 6b88a0d6b..c0d765a9a 100644 --- a/p2p/host.go +++ b/p2p/host.go @@ -106,6 +106,7 @@ func NewHost(cfg HostConfig) (Host, error) { libp2p.Identity(key), libp2p.EnableNATService(), libp2p.ForceReachabilityPublic(), + libp2p.BandwidthReporter(newCounter()), ) if err != nil { return nil, errors.Wrapf(err, "cannot initialize libp2p host") diff --git a/p2p/metrics.go b/p2p/metrics.go new file mode 100644 index 000000000..ca9e17ac2 --- /dev/null +++ b/p2p/metrics.go @@ -0,0 +1,39 @@ +//package p2p +package p2p + +import ( + eth_metrics "github.com/ethereum/go-ethereum/metrics" + "github.com/libp2p/go-libp2p-core/metrics" +) + +const ( + // ingressMeterName is the prefix of the per-packet inbound metrics. + ingressMeterName = "p2p/ingress" + + // egressMeterName is the prefix of the per-packet outbound metrics. + egressMeterName = "p2p/egress" +) + +var ( + ingressTrafficMeter = eth_metrics.NewRegisteredMeter(ingressMeterName, nil) + egressTrafficMeter = eth_metrics.NewRegisteredMeter(egressMeterName, nil) + activePeerGauge = eth_metrics.NewRegisteredGauge("p2p/peers", nil) +) + +// Counter is a wrapper around a metrics.BandwidthCounter that meters both the +// inbound and outbound network traffic. +type Counter struct { + *metrics.BandwidthCounter +} + +func newCounter() *Counter { + return &Counter{metrics.NewBandwidthCounter()} +} + +func (c *Counter) LogRecvMessage(size int64) { + ingressTrafficMeter.Mark(size) +} + +func (c *Counter) LogSentMessage(size int64) { + egressTrafficMeter.Mark(size) +}