fix build with libp2p_agents_metrics switch

This commit is contained in:
Giovanni Petrantoni 2021-03-15 01:42:47 +00:00
parent 70deac9e0d
commit 4760df1e31
3 changed files with 25 additions and 13 deletions

View File

@ -71,9 +71,9 @@ proc disconnectPeer(g: GossipSub, peer: PubSubPeer) {.async.} =
peer.shortAgent peer.shortAgent
else: else:
if peer.sendConn != nil: if peer.sendConn != nil:
let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii()
if KnownLibP2PAgentsSeq.contains(shortAgent): if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()):
peer.shortAgent = shortAgent peer.shortAgent = shortAgent.get()
else: else:
peer.shortAgent = "unknown" peer.shortAgent = "unknown"
peer.shortAgent peer.shortAgent
@ -165,9 +165,9 @@ proc updateScores*(g: GossipSub) = # avoid async
peer.shortAgent peer.shortAgent
else: else:
if peer.sendConn != nil: if peer.sendConn != nil:
let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii()
if KnownLibP2PAgentsSeq.contains(shortAgent): if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()):
peer.shortAgent = shortAgent peer.shortAgent = shortAgent.get()
else: else:
peer.shortAgent = "unknown" peer.shortAgent = "unknown"
peer.shortAgent peer.shortAgent
@ -221,9 +221,9 @@ proc updateScores*(g: GossipSub) = # avoid async
peer.shortAgent peer.shortAgent
else: else:
if peer.sendConn != nil: if peer.sendConn != nil:
let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].toLowerAscii() let shortAgent = peer.sendConn.peerInfo.agentVersion.split("/")[0].safeToLowerAscii()
if KnownLibP2PAgentsSeq.contains(shortAgent): if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()):
peer.shortAgent = shortAgent peer.shortAgent = shortAgent.get()
else: else:
peer.shortAgent = "unknown" peer.shortAgent = "unknown"
peer.shortAgent peer.shortAgent

View File

@ -79,9 +79,9 @@ when defined(libp2p_agents_metrics):
if not s.tracked: if not s.tracked:
if not isNil(s.peerInfo) and s.peerInfo.agentVersion.len > 0: if not isNil(s.peerInfo) and s.peerInfo.agentVersion.len > 0:
# / seems a weak "standard" so for now it's reliable # / seems a weak "standard" so for now it's reliable
let shortAgent = s.peerInfo.agentVersion.split("/")[0].toLowerAscii() let shortAgent = s.peerInfo.agentVersion.split("/")[0].safeToLowerAscii()
if KnownLibP2PAgentsSeq.contains(shortAgent): if shortAgent.isOk() and KnownLibP2PAgentsSeq.contains(shortAgent.get()):
s.shortAgent = shortAgent s.shortAgent = shortAgent.get()
else: else:
s.shortAgent = "unknown" s.shortAgent = "unknown"
libp2p_peers_identity.inc(labelValues = [s.shortAgent]) libp2p_peers_identity.inc(labelValues = [s.shortAgent])

View File

@ -39,6 +39,18 @@ func shortLog*(item: string): string =
result &= item[(item.len - split)..item.high] result &= item[(item.len - split)..item.high]
when defined(libp2p_agents_metrics): when defined(libp2p_agents_metrics):
import strutils
export split
import stew/results
export results
proc safeToLowerAscii*(s: string): Result[string, cstring] =
try:
ok(s.toLowerAscii())
except:
err("toLowerAscii failed")
const const
KnownLibP2PAgents* {.strdefine.} = "" KnownLibP2PAgents* {.strdefine.} = ""
KnownLibP2PAgentsSeq* = KnownLibP2PAgents.toLowerAscii().split(",") KnownLibP2PAgentsSeq* = KnownLibP2PAgents.safeToLowerAscii().tryGet().split(",")