Skip to content

Commit 5d6c84f

Browse files
committed
feat: gossipsub metrics infra
1 parent f4b738b commit 5d6c84f

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

libp2p/metrics/gossipsub.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from prometheus_client import Counter, Histogram
2+
3+
from libp2p.pubsub.gossipsub import GossipsubEvent
4+
5+
6+
class GossipsubMetrics:
7+
delivered: Counter
8+
dropped: Counter
9+
validated_fail: Counter
10+
msg_size: Histogram
11+
12+
def __init__(self):
13+
self.delivered = Counter(
14+
"gossipsub_delivered_total",
15+
"Messages successfully delivered",
16+
labelnames=["topic"],
17+
)
18+
19+
self.dropped = Counter(
20+
"gossipsub_dropped_total",
21+
"Messages dropped",
22+
labelnames=["topic", "reason"],
23+
)
24+
25+
self.validated_fail = Counter(
26+
"gossipsub_validation_failed_total",
27+
"Messages rejected by validator",
28+
labelnames=["topic", "error"],
29+
)
30+
31+
self.msg_size = Histogram(
32+
"gossipsub_message_bytes",
33+
"Message size in bytes",
34+
buckets=[64, 128, 256, 512, 1024, 2048, 4096],
35+
)
36+
37+
def record(self, event: GossipsubEvent) -> None:
38+
if event.delivered:
39+
self.delivered.labels(topic=event.topic).inc()
40+
41+
if event.message_size is not None:
42+
self.msg_size.observe(event.message_size)
43+
44+
if event.dropped_reason:
45+
self.dropped.labels(
46+
topic=event.topic,
47+
reason=event.dropped_reason,
48+
).inc()
49+
50+
if event.validation_error:
51+
self.validated_fail.labels(
52+
topic=event.topic,
53+
error=type(event.validation_error).__name__,
54+
).inc()

libp2p/metrics/kad_dht.py

Whitespace-only changes.

libp2p/metrics/swarm.py

Whitespace-only changes.

libp2p/pubsub/gossipsub.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import (
1313
Any,
1414
DefaultDict,
15+
Optional,
1516
)
1617

1718
import trio
@@ -68,6 +69,16 @@
6869

6970
logger = logging.getLogger(__name__)
7071

72+
class GossipsubEvent:
73+
peer_id: str
74+
topic: str
75+
76+
# one of these should be set
77+
message_size: Optional[int] = None
78+
delivered: bool = False
79+
dropped_reason: Optional[str] = None
80+
validation_error: Optional[Exception] = None
81+
7182

7283
class GossipSub(IPubsubRouter, Service):
7384
protocols: list[TProtocol]

0 commit comments

Comments
 (0)