From d4454122975c7b9b43273212a84553e2459d6e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Wed, 10 Jul 2019 02:08:35 +0200 Subject: [PATCH] use nim-metrics --- eth.nimble | 3 ++- eth/common/eth_types.nim | 3 --- eth/common/utils.nim | 6 ++++-- eth/p2p/peer_pool.nim | 5 +++-- eth/p2p/rlpx.nim | 3 ++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/eth.nimble b/eth.nimble index e0310bb..8cc9aeb 100644 --- a/eth.nimble +++ b/eth.nimble @@ -13,7 +13,8 @@ requires "nim >= 0.19.0", "chronicles", "stew", "result", - "nat_traversal" + "nat_traversal", + "https://github.com/status-im/nim-metrics" proc runTest(path: string) = echo "\nRunning: ", path diff --git a/eth/common/eth_types.nim b/eth/common/eth_types.nim index 043b22c..6fbde9f 100644 --- a/eth/common/eth_types.nim +++ b/eth/common/eth_types.nim @@ -168,9 +168,6 @@ type OK Error - NimbusStats* = object - num_peers*: int - when BlockNumber is int64: ## The goal of these templates is to make it easier to switch ## the block number type to a different representation diff --git a/eth/common/utils.nim b/eth/common/utils.nim index 2b8677b..b3cc9f5 100644 --- a/eth/common/utils.nim +++ b/eth/common/utils.nim @@ -1,4 +1,6 @@ -import nimcrypto, hashes, stew/byteutils, eth_types +import nimcrypto, hashes, stew/byteutils, eth_types, metrics + +export metrics proc hash*(d: MDigest): Hash {.inline.} = hash(d.data) @@ -8,5 +10,5 @@ proc parseAddress*(hexString: string): EthAddress = proc `$`*(a: EthAddress): string = a.toHex() -var nimbusStats*: NimbusStats +var peerGauge* = newGauge("connected peers", "number of peers in the pool") diff --git a/eth/p2p/peer_pool.nim b/eth/p2p/peer_pool.nim index 490f4eb..9365587 100644 --- a/eth/p2p/peer_pool.nim +++ b/eth/p2p/peer_pool.nim @@ -107,10 +107,11 @@ proc getRandomBootnode(p: PeerPool): Option[Node] = if p.discovery.bootstrapNodes.len != 0: result = option(p.discovery.bootstrapNodes.rand()) -proc addPeer*(pool: PeerPool, peer: Peer) = +proc addPeer*(pool: PeerPool, peer: Peer) {.gcsafe.} = doAssert(peer.remote notin pool.connectedNodes) pool.connectedNodes[peer.remote] = peer - inc(nimbusStats.num_peers) + {.gcsafe.}: + peerGauge.inc() for o in pool.observers.values: if not o.onPeerConnected.isNil: if o.protocol.isNil or peer.supports(o.protocol): diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index 4ca04cd..574fa3c 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -795,7 +795,8 @@ proc removePeer(network: EthereumNode, peer: Peer) = # between which side disconnects first. if network.peerPool != nil and not peer.remote.isNil and peer.remote in network.peerPool.connectedNodes: network.peerPool.connectedNodes.del(peer.remote) - dec(nimbusStats.num_peers) + {.gcsafe.}: + peerGauge.dec() # Note: we need to do this check as disconnect (and thus removePeer) # currently can get called before the dispatcher is initialized.