[tor-commits] [bridgedb/main] Bring back the bridges per distributor metrics

meskio at torproject.org meskio at torproject.org
Fri Jan 14 17:57:54 UTC 2022


commit 65f6e392eb38affc1617de59cc72791217984301
Author: meskio <meskio at torproject.org>
Date:   Mon Dec 13 10:16:16 2021 +0100

    Bring back the bridges per distributor metrics
    
    The right place to do that will be rdsys, but let's keep the metrics as
    well in bridgedb so the migration for the metrics team is more smooth.
    With the migration to rdsys we loose the metrics of the unasigned
    bridges and any distributor not managed by bridgedb.
    
    Related: rdsys#67
---
 bridgedb/metrics.py | 21 +++++++++++++++++++++
 bridgedb/rdsys.py   | 17 ++++++++++++++---
 2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/bridgedb/metrics.py b/bridgedb/metrics.py
index c56b945..7751e94 100644
--- a/bridgedb/metrics.py
+++ b/bridgedb/metrics.py
@@ -422,6 +422,27 @@ class InternalMetrics(Metrics):
         self.set("{}.lower-whisker".format(handoutsPrefix), lowerWhisker)
         self.set("{}.upper-whisker".format(handoutsPrefix), upperWhisker)
 
+    def recordBridgesInHashring(self, ringName, subRingName, numBridges):
+        """
+        Record the number of bridges per hashring.
+
+        :param str ringName: The name of the ring, e.g., "https".
+        :param str subRingName: The name of the subring, e.g.,
+            "byIPv6-bySubring1of4".
+        :param int numBridges: The number of bridges in the given subring.
+        """
+
+        if not ringName or not subRingName:
+            logging.warning("Ring name ({}) and subring name ({}) cannot be "
+                            "empty.".format(ringName, subRingName))
+            return
+
+        logging.info("Recording metrics for bridge (sub)rings: %s/%s/%d." %
+                     (ringName, subRingName, numBridges))
+        # E.g, concatenate "https" with "byipv6-bysubring1of4".
+        key = "{}.{}.{}".format(self.keyPrefix, ringName, subRingName.lower())
+        self.set(key, numBridges)
+
 
 class HTTPSMetrics(Metrics):
 
diff --git a/bridgedb/rdsys.py b/bridgedb/rdsys.py
index f90cf54..56fcc4c 100644
--- a/bridgedb/rdsys.py
+++ b/bridgedb/rdsys.py
@@ -1,5 +1,4 @@
 import json
-import secrets
 import logging
 from io import BytesIO
 from twisted.internet import reactor
@@ -8,6 +7,7 @@ from twisted.internet.protocol import Protocol
 from twisted.web.client import Agent, FileBodyProducer
 from twisted.web.http_headers import Headers
 
+from bridgedb import metrics
 from bridgedb.bridges import Bridge, MalformedBridgeInfo
 
 
@@ -15,13 +15,15 @@ inter_message_delimiter = b"\r"
 
 
 class RdsysProtocol(Protocol):
-    def __init__(self, finished, hashring):
+    def __init__(self, finished, hashring, distributor):
         """
         :type hashring: :class:`bridgedb.bridgerings.FilteredBridgeSplitter`
         """
         self.finished = finished
         self.hashring = hashring
+        self.distributor = distributor
         self.buff = b""
+        self.metrix = metrics.InternalMetrics()
 
     def dataReceived(self, data):
         """
@@ -40,6 +42,7 @@ class RdsysProtocol(Protocol):
         self.buff += parts[0]
         for part in parts[1:]:
             self._updateResources()
+            self._updateMetrics()
             self.buff = part
 
     def _updateResources(self):
@@ -64,6 +67,14 @@ class RdsysProtocol(Protocol):
                         logging.warning("Got a malformed bridge: %s" % e)
                     fn(bridge)
 
+    def _updateMetrics(self):
+        filterRings = self.hashring.filterRings
+        for (ringName, (_, subring)) in filterRings.items():
+            subRingName = "-".join(self.hashring.extractFilterNames(ringName))
+            self.metrix.recordBridgesInHashring(self.distributor,
+                                                subRingName,
+                                                len(subring))
+
     def connectionLost(self, reason):
         logging.info("Connection lost with rdsys backend: %s" % reason)
         self.finished.callback(None)
@@ -86,7 +97,7 @@ def start_stream(distributor, token, rdsys_address, hashring):
 
     def cbResponse(r):
         finished = Deferred()
-        r.deliverBody(RdsysProtocol(finished, hashring))
+        r.deliverBody(RdsysProtocol(finished, hashring, distributor))
         return finished
 
     def connect():



More information about the tor-commits mailing list