Skip to content

Commit 7571c3f

Browse files
committed
Implement RTT measurement
This commit implements a Round-Trip-Time measurement by measuring the ping-pong delay. When a ping is sent to a peer `p`, the time is sampled. When a pong is received, the elapsed time is measured and the round_trip_count and round_trip_sum counter and gauge are emitted. Signed-off-by: Yacov Manevich <yacov.manevich@avalabs.org>
1 parent edff599 commit 7571c3f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

network/peer/metrics.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type Metrics struct {
3131
ClockSkewCount prometheus.Counter
3232
ClockSkewSum prometheus.Gauge
3333

34+
RTTCount prometheus.Counter
35+
RTTSum prometheus.Gauge
36+
3437
NumFailedToParse prometheus.Counter
3538
NumSendFailed *prometheus.CounterVec // op
3639

@@ -41,6 +44,14 @@ type Metrics struct {
4144

4245
func NewMetrics(registerer prometheus.Registerer) (*Metrics, error) {
4346
m := &Metrics{
47+
RTTCount: prometheus.NewCounter(prometheus.CounterOpts{
48+
Name: "round_trip_count",
49+
Help: "number of RTT samples taken (n)",
50+
}),
51+
RTTSum: prometheus.NewGauge(prometheus.GaugeOpts{
52+
Name: "round_trip_sum",
53+
Help: "sum of RTT samples taken (ms)",
54+
}),
4455
ClockSkewCount: prometheus.NewCounter(prometheus.CounterOpts{
4556
Name: "clock_skew_count",
4657
Help: "number of handshake timestamps inspected (n)",
@@ -83,6 +94,8 @@ func NewMetrics(registerer prometheus.Registerer) (*Metrics, error) {
8394
),
8495
}
8596
return m, errors.Join(
97+
registerer.Register(m.RTTCount),
98+
registerer.Register(m.RTTSum),
8699
registerer.Register(m.ClockSkewCount),
87100
registerer.Register(m.ClockSkewSum),
88101
registerer.Register(m.NumFailedToParse),

network/peer/peer.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ type peer struct {
191191
// Must only be accessed atomically
192192
lastSent, lastReceived int64
193193

194+
// lastPingSent is the milliseconds since 1970-01-01 UTC when the last ping was sent
195+
lastPingSent int64
196+
194197
// getPeerListChan signals that we should attempt to send a GetPeerList to
195198
// this peer
196199
getPeerListChan chan struct{}
@@ -626,6 +629,10 @@ func (p *peer) writeMessage(writer io.Writer, msg message.OutboundMessage) {
626629
now := p.Clock.Time()
627630
p.storeLastSent(now)
628631
p.Metrics.Sent(msg)
632+
633+
if msg.Op() == message.PingOp {
634+
atomic.StoreInt64(&p.lastPingSent, now.UnixMilli())
635+
}
629636
}
630637

631638
func (p *peer) sendNetworkMessages() {
@@ -819,7 +826,17 @@ func (p *peer) getUptime() uint32 {
819826
return primaryUptimePercent
820827
}
821828

822-
func (*peer) handlePong(*p2p.Pong) {}
829+
func (p *peer) handlePong(*p2p.Pong) {
830+
lastPing := atomic.LoadInt64(&p.lastPingSent)
831+
if lastPing == 0 {
832+
return
833+
}
834+
835+
elapsed := time.Now().UnixMilli() - lastPing
836+
837+
p.Metrics.RTTCount.Inc()
838+
p.Metrics.RTTSum.Add(float64(elapsed))
839+
}
823840

824841
func (p *peer) handleHandshake(msg *p2p.Handshake) {
825842
if p.gotHandshake.Get() {

0 commit comments

Comments
 (0)