Add p2p metrics

pull/4042/head
harjas 3 years ago
parent 9e12516327
commit a82ab83ac3
  1. 3
      api/service/prometheus/service.go
  2. 1
      p2p/host.go
  3. 39
      p2p/metrics.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.

@ -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")

@ -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)
}
Loading…
Cancel
Save