fix collecting p2p/Peers metric (#1269)
This commit is contained in:
parent
5d0ed0303e
commit
04ba4d44e8
|
@ -139,8 +139,10 @@ func main() {
|
|||
profiling.NewProfiler(*pprofPort).Go()
|
||||
}
|
||||
|
||||
// Run stats server.
|
||||
if *metrics {
|
||||
// Start collecting metrics. Metrics can be enabled by providing `-metrics` flag
|
||||
// or setting `gethmetrics.Enabled` to true during compilation time:
|
||||
// https://github.com/status-im/go-ethereum/pull/76.
|
||||
if *metrics || gethmetrics.Enabled {
|
||||
go startCollectingNodeMetrics(interruptCh, backend.StatusNode())
|
||||
go gethmetrics.CollectProcessMetrics(3 * time.Second)
|
||||
}
|
||||
|
@ -202,8 +204,16 @@ func startCollectingNodeMetrics(interruptCh <-chan struct{}, statusNode *node.St
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
// Try to subscribe and collect metrics. In case of an error, retry.
|
||||
for {
|
||||
if err := nodemetrics.SubscribeServerEvents(ctx, gethNode); err != nil {
|
||||
logger.Error("Failed to subscribe server events", "error", err)
|
||||
} else {
|
||||
// no error means that the subscription was terminated by purpose
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
var (
|
||||
nodePeersCounter metrics.Counter
|
||||
nodePeersGauge metrics.Gauge
|
||||
nodeMaxPeersGauge metrics.Gauge
|
||||
)
|
||||
|
||||
|
@ -23,6 +24,7 @@ func init() {
|
|||
}
|
||||
|
||||
nodePeersCounter = metrics.NewRegisteredCounter("p2p/Peers", nil)
|
||||
nodePeersGauge = metrics.NewRegisteredGauge("p2p/PeersAbsolute", nil)
|
||||
nodeMaxPeersGauge = metrics.NewRegisteredGauge("p2p/MaxPeers", nil)
|
||||
}
|
||||
|
||||
|
@ -38,6 +40,7 @@ func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
|
|||
nodePeersCounter.Dec(1)
|
||||
}
|
||||
|
||||
nodePeersGauge.Update(int64(server.PeerCount()))
|
||||
nodeMaxPeersGauge.Update(int64(server.MaxPeers))
|
||||
|
||||
return nil
|
||||
|
|
|
@ -23,6 +23,7 @@ func SubscribeServerEvents(ctx context.Context, node *node.Node) error {
|
|||
|
||||
ch := make(chan *p2p.PeerEvent, server.MaxPeers)
|
||||
subscription := server.SubscribeEvents(ch)
|
||||
defer subscription.Unsubscribe()
|
||||
|
||||
logger.Debug("Subscribed to server events")
|
||||
|
||||
|
@ -38,10 +39,8 @@ func SubscribeServerEvents(ctx context.Context, node *node.Node) error {
|
|||
if err != nil {
|
||||
logger.Error("Subscription failed", "err", err)
|
||||
}
|
||||
subscription.Unsubscribe()
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
subscription.Unsubscribe()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue