fix collecting p2p/Peers metric (#1269)

This commit is contained in:
Adam Babik 2018-11-12 10:27:09 +01:00 committed by GitHub
parent 5d0ed0303e
commit 04ba4d44e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 6 deletions

View File

@ -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() {
if err := nodemetrics.SubscribeServerEvents(ctx, gethNode); err != nil {
logger.Error("Failed to subscribe server events", "error", err)
// 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)
}
}()

View File

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

View File

@ -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
}
}