improving connection estblishing metrics (#500)

This commit is contained in:
Dmitriy Ryajov 2021-01-07 17:06:41 -06:00 committed by GitHub
parent b2ea5a3c77
commit 8e57746f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 7 deletions

View File

@ -42,9 +42,11 @@ logScope:
# and only if the channel has been secured (i.e. if a secure manager has been # and only if the channel has been secured (i.e. if a secure manager has been
# previously provided) # previously provided)
declareCounter(libp2p_dialed_peers, "dialed peers") declareCounter(libp2p_total_dial_attempts, "total attempted dials")
declareCounter(libp2p_successful_dials, "dialed successful peers")
declareCounter(libp2p_failed_dials, "failed dials") declareCounter(libp2p_failed_dials, "failed dials")
declareCounter(libp2p_failed_upgrade, "peers failed upgrade") declareCounter(libp2p_failed_upgrades_incoming, "incoming connections failed upgrades")
declareCounter(libp2p_failed_upgrades_outgoing, "outgoing connections failed upgrades")
const const
ConcurrentUpgrades* = 4 ConcurrentUpgrades* = 4
@ -276,6 +278,7 @@ proc dialAndUpgrade(s: Switch,
if t.handles(a): # check if it can dial it if t.handles(a): # check if it can dial it
trace "Dialing address", address = $a, peerId trace "Dialing address", address = $a, peerId
let dialed = try: let dialed = try:
libp2p_total_dial_attempts.inc()
await t.dial(a) await t.dial(a)
except CancelledError as exc: except CancelledError as exc:
debug "Dialing canceled", msg = exc.msg, peerId debug "Dialing canceled", msg = exc.msg, peerId
@ -288,7 +291,7 @@ proc dialAndUpgrade(s: Switch,
# make sure to assign the peer to the connection # make sure to assign the peer to the connection
dialed.peerInfo = PeerInfo.init(peerId, addrs) dialed.peerInfo = PeerInfo.init(peerId, addrs)
libp2p_dialed_peers.inc() libp2p_successful_dials.inc()
let conn = try: let conn = try:
await s.upgradeOutgoing(dialed) await s.upgradeOutgoing(dialed)
@ -298,7 +301,7 @@ proc dialAndUpgrade(s: Switch,
await dialed.close() await dialed.close()
debug "Upgrade failed", msg = exc.msg, peerId debug "Upgrade failed", msg = exc.msg, peerId
if exc isnot CancelledError: if exc isnot CancelledError:
libp2p_failed_upgrade.inc() libp2p_failed_upgrades_outgoing.inc()
raise exc raise exc
doAssert not isNil(conn), "connection died after upgradeOutgoing" doAssert not isNil(conn), "connection died after upgradeOutgoing"
@ -436,7 +439,8 @@ proc upgradeMonitor(conn: Connection, upgrades: AsyncSemaphore) {.async.} =
await conn.upgraded.wait(30.seconds) # wait for connection to be upgraded await conn.upgraded.wait(30.seconds) # wait for connection to be upgraded
trace "Connection upgrade succeeded" trace "Connection upgrade succeeded"
except CatchableError as exc: except CatchableError as exc:
# if not isNil(conn): # for some reason, this can be nil libp2p_failed_upgrades_incoming.inc()
if not isNil(conn):
await conn.close() await conn.close()
trace "Exception awaiting connection upgrade", exc = exc.msg, conn trace "Exception awaiting connection upgrade", exc = exc.msg, conn
@ -555,7 +559,6 @@ proc muxerHandler(s: Switch, muxer: Muxer) {.async, gcsafe.} =
raise exc raise exc
except CatchableError as exc: except CatchableError as exc:
await muxer.close() await muxer.close()
libp2p_failed_upgrade.inc()
trace "Exception in muxer handler", conn, msg = exc.msg trace "Exception in muxer handler", conn, msg = exc.msg
proc newSwitch*(peerInfo: PeerInfo, proc newSwitch*(peerInfo: PeerInfo,