Skip to content

Commit

Permalink
Add metric
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Dec 14, 2022
1 parent a4999bf commit c36fbb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion libp2p/services/autonatservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ else:
{.push raises: [].}

import std/[options, deques, sequtils]
import chronos
import chronos, metrics
import ../switch
import ../protocols/[connectivity/autonat]
import ../utils/heartbeat
import ../crypto/crypto

declarePublicGauge(libp2p_autonat_reachability_confidence, "autonat reachability confidence", labels = ["reachability"])

type
AutonatService* = ref object of Service
newConnectedPeerHandler: PeerEventHandler
Expand Down Expand Up @@ -73,12 +75,14 @@ proc handleAnswer(self: AutonatService, ans: NetworkReachability) {.async.} =
if confidence >= self.minConfidence:
self.networkReachability = NetworkReachability.Reachable
self.confidence = some(confidence)
libp2p_autonat_reachability_confidence.set(value = confidence, labelValues = ["Reachable"])
else:
let notReachableCount = toSeq(self.answerDeque.items).countIt(it == NetworkReachability.NotReachable)
let confidence = notReachableCount / self.maxQueueSize
if confidence >= self.minConfidence:
self.networkReachability = NetworkReachability.NotReachable
self.confidence = some(confidence)
libp2p_autonat_reachability_confidence.set(confidence, ["NotReachable"])
else:
self.networkReachability = NetworkReachability.Unknown
self.confidence = none(float)
Expand Down
6 changes: 5 additions & 1 deletion tests/testautonatservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# those terms.

import std/options
import chronos
import chronos, metrics
import unittest2
import ../libp2p/[builders,
switch,
Expand Down Expand Up @@ -60,6 +60,7 @@ suite "Autonat Service":
await autonatStub.finished

check autonatService.networkReachability() == NetworkReachability.NotReachable
check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 0.3

await allFuturesThrowing(
switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())
Expand Down Expand Up @@ -90,6 +91,7 @@ suite "Autonat Service":
await autonatStub.finished

check autonatService.networkReachability() == NetworkReachability.Reachable
check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3

await allFuturesThrowing(
switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())
Expand Down Expand Up @@ -130,10 +132,12 @@ suite "Autonat Service":
await awaiter.finished

check autonatService.networkReachability() == NetworkReachability.NotReachable
check libp2p_autonat_reachability_confidence.value(["NotReachable"]) == 0.3

await autonatStub.finished

check autonatService.networkReachability() == NetworkReachability.Reachable
check libp2p_autonat_reachability_confidence.value(["Reachable"]) == 0.3

await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())

Expand Down

0 comments on commit c36fbb2

Please sign in to comment.