Skip to content

Commit

Permalink
chore: add basic metrics for rendezvous
Browse files Browse the repository at this point in the history
  • Loading branch information
vpavlin committed Jun 6, 2023
1 parent 6710287 commit b4448d1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libp2p/protocols/rendezvous.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ else:
{.push raises: [].}

import tables, sequtils, sugar, sets, options
import metrics except collect
import chronos,
chronicles,
bearssl/rand,
Expand All @@ -30,6 +31,11 @@ export chronicles
logScope:
topics = "libp2p discovery rendezvous"

declareCounter(libp2p_rendezvous_register, "number of advertise requests")
declareCounter(libp2p_rendezvous_discover, "number of discovery requests")
declareGauge(libp2p_rendezvous_registered, "number of registered peers")
declareGauge(libp2p_rendezvous_namespaces, "number of registered namespaces")

const
RendezVousCodec* = "/rendezvous/1.0.0"
MinimumDuration* = 2.hours
Expand Down Expand Up @@ -378,6 +384,7 @@ proc save(rdv: RendezVous,

proc register(rdv: RendezVous, conn: Connection, r: Register): Future[void] =
trace "Received Register", peerId = conn.peerId, ns = r.ns
libp2p_rendezvous_register.inc()
if r.ns.len notin 1..255:
return conn.sendRegisterResponseError(InvalidNamespace)
let ttl = r.ttl.get(MinimumTTL)
Expand All @@ -389,6 +396,8 @@ proc register(rdv: RendezVous, conn: Connection, r: Register): Future[void] =
if rdv.countRegister(conn.peerId) >= RegistrationLimitPerPeer:
return conn.sendRegisterResponseError(NotAuthorized, "Registration limit reached")
rdv.save(r.ns, conn.peerId, r)
libp2p_rendezvous_registered.inc()
libp2p_rendezvous_namespaces.set(int64(rdv.namespaces.len))
conn.sendRegisterResponse(ttl)

proc unregister(rdv: RendezVous, conn: Connection, u: Unregister) =
Expand All @@ -398,11 +407,13 @@ proc unregister(rdv: RendezVous, conn: Connection, u: Unregister) =
for index in rdv.namespaces[nsSalted]:
if rdv.registered[index].peerId == conn.peerId:
rdv.registered[index].expiration = rdv.defaultDT
libp2p_rendezvous_registered.dec()
except KeyError:
return

proc discover(rdv: RendezVous, conn: Connection, d: Discover) {.async.} =
trace "Received Discover", peerId = conn.peerId, ns = d.ns
libp2p_rendezvous_discover.inc()
if d.ns.len notin 0..255:
await conn.sendDiscoverResponseError(InvalidNamespace)
return
Expand Down Expand Up @@ -657,9 +668,13 @@ proc new*(T: typedesc[RendezVous],
proc deletesRegister(rdv: RendezVous) {.async.} =
heartbeat "Register timeout", 1.minutes:
let n = Moment.now()
var total = 0
rdv.registered.flushIfIt(it.expiration < n)
for data in rdv.namespaces.mvalues():
data.keepItIf(it >= rdv.registered.offset)
total += data.len
libp2p_rendezvous_registered.set(int64(total))
libp2p_rendezvous_namespaces.set(int64(rdv.namespaces.len))

method start*(rdv: RendezVous) {.async.} =
if not rdv.registerDeletionLoop.isNil:
Expand Down

0 comments on commit b4448d1

Please sign in to comment.