Show route churn as a whole count per minute (closes #160)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The status page showed route churn per second as a one-minute moving average, so a quiet feed gave a fraction such as 0.1. The average was correct. The metrics tracker now also counts prefix announcements and withdrawals, from all peers, in each clock minute. /api/v1/stats reports the count for the last full minute as route_churn_per_min in stream, in place of route_churn_per_sec. The status page shows it as "Route Churn/min". The status and home pages round messages and updates per second to whole numbers. Model: opus-5-5
This commit was merged in pull request #169.
This commit is contained in:
@@ -34,7 +34,7 @@ type Tracker struct {
|
||||
// Announcement/withdrawal metrics
|
||||
announcementCounter metrics.Counter
|
||||
withdrawalCounter metrics.Counter
|
||||
churnRate metrics.Meter // combined announcements + withdrawals per second
|
||||
churnPerMinute minuteCounter // prefix announcements and withdrawals
|
||||
|
||||
// BGP peer tracking
|
||||
bgpPeerCount atomic.Int32
|
||||
@@ -56,7 +56,7 @@ func New() *Tracker {
|
||||
ipv6UpdateRate: metrics.NewMeter(),
|
||||
announcementCounter: metrics.NewCounter(),
|
||||
withdrawalCounter: metrics.NewCounter(),
|
||||
churnRate: metrics.NewMeter(),
|
||||
churnPerMinute: minuteCounter{now: time.Now},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,13 +156,13 @@ func (t *Tracker) RecordIPv6Update() {
|
||||
// RecordAnnouncement records a route announcement
|
||||
func (t *Tracker) RecordAnnouncement() {
|
||||
t.announcementCounter.Inc(1)
|
||||
t.churnRate.Mark(1)
|
||||
t.churnPerMinute.add()
|
||||
}
|
||||
|
||||
// RecordWithdrawal records a route withdrawal
|
||||
func (t *Tracker) RecordWithdrawal() {
|
||||
t.withdrawalCounter.Inc(1)
|
||||
t.churnRate.Mark(1)
|
||||
t.churnPerMinute.add()
|
||||
}
|
||||
|
||||
// SetBGPPeerCount updates the current BGP peer count
|
||||
@@ -198,9 +198,10 @@ func (t *Tracker) GetWithdrawalCount() uint64 {
|
||||
return uint64(count)
|
||||
}
|
||||
|
||||
// GetChurnRate returns the route churn rate per second
|
||||
func (t *Tracker) GetChurnRate() float64 {
|
||||
return t.churnRate.Rate1()
|
||||
// GetChurnPerMinute returns the number of prefix announcements and
|
||||
// withdrawals, from all peers, in the last full clock minute.
|
||||
func (t *Tracker) GetChurnPerMinute() int {
|
||||
return t.churnPerMinute.lastFullMinute()
|
||||
}
|
||||
|
||||
// GetRouteMetrics returns current route update metrics
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// minuteCounter counts events in each clock minute and reports the count of
|
||||
// the last full minute. It is safe for concurrent use.
|
||||
type minuteCounter struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
|
||||
// minute is the start of the minute that count belongs to.
|
||||
minute time.Time
|
||||
count int
|
||||
// lastMinuteCount is the count of the minute just before minute.
|
||||
lastMinuteCount int
|
||||
}
|
||||
|
||||
// add counts one event in the current minute.
|
||||
func (c *minuteCounter) add() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.moveToCurrentMinute()
|
||||
c.count++
|
||||
}
|
||||
|
||||
// lastFullMinute returns the number of events in the minute that ended most
|
||||
// recently.
|
||||
func (c *minuteCounter) lastFullMinute() int {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.moveToCurrentMinute()
|
||||
|
||||
return c.lastMinuteCount
|
||||
}
|
||||
|
||||
// moveToCurrentMinute starts a new count when the clock has moved into
|
||||
// another minute. The count it ends becomes the last full minute's count when
|
||||
// its minute is the one just before the current minute. Otherwise the last
|
||||
// full minute had no events. The caller holds mu.
|
||||
func (c *minuteCounter) moveToCurrentMinute() {
|
||||
minute := c.now().Truncate(time.Minute)
|
||||
if minute.Equal(c.minute) {
|
||||
return
|
||||
}
|
||||
|
||||
if minute.Sub(c.minute) == time.Minute {
|
||||
c.lastMinuteCount = c.count
|
||||
} else {
|
||||
c.lastMinuteCount = 0
|
||||
}
|
||||
|
||||
c.minute = minute
|
||||
c.count = 0
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestChurnPerMinuteCountsLastFullMinute announces and withdraws prefixes on
|
||||
// an injected clock and expects each minute's whole count once that minute
|
||||
// has ended, and 0 for a minute with none.
|
||||
func TestChurnPerMinuteCountsLastFullMinute(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
now := time.Date(2026, 10, 26, 12, 0, 30, 0, time.UTC)
|
||||
tracker := New()
|
||||
tracker.churnPerMinute.now = func() time.Time { return now }
|
||||
|
||||
expectChurn := func(want int, why string) {
|
||||
t.Helper()
|
||||
|
||||
got := tracker.GetChurnPerMinute()
|
||||
if got != want {
|
||||
t.Fatalf("at %s churn = %d, want %d: %s",
|
||||
now.Format(time.TimeOnly), got, want, why)
|
||||
}
|
||||
}
|
||||
|
||||
const firstMinuteChurn = 2
|
||||
|
||||
tracker.RecordAnnouncement()
|
||||
tracker.RecordWithdrawal()
|
||||
expectChurn(0, "12:00 has not ended")
|
||||
|
||||
now = time.Date(2026, 10, 26, 12, 1, 59, 0, time.UTC)
|
||||
|
||||
tracker.RecordAnnouncement()
|
||||
expectChurn(firstMinuteChurn, "12:00 had one announcement and one withdrawal")
|
||||
|
||||
now = time.Date(2026, 10, 26, 12, 2, 0, 0, time.UTC)
|
||||
|
||||
expectChurn(1, "12:01 had one announcement")
|
||||
|
||||
now = time.Date(2026, 10, 26, 12, 4, 0, 0, time.UTC)
|
||||
|
||||
expectChurn(0, "12:03 had none")
|
||||
|
||||
tracker.RecordWithdrawal()
|
||||
|
||||
now = time.Date(2026, 10, 26, 12, 6, 0, 0, time.UTC)
|
||||
|
||||
expectChurn(0, "12:05 had none, though 12:04 had one withdrawal")
|
||||
}
|
||||
|
||||
// TestChurnPerMinuteCountsConcurrentRecords announces prefixes from several
|
||||
// goroutines at once and expects every one counted.
|
||||
func TestChurnPerMinuteCountsConcurrentRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
goroutines = 8
|
||||
recordsPerGoroutine = 1000
|
||||
)
|
||||
|
||||
now := time.Date(2026, 10, 26, 12, 0, 0, 0, time.UTC)
|
||||
tracker := New()
|
||||
tracker.churnPerMinute.now = func() time.Time { return now }
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for range goroutines {
|
||||
wg.Go(func() {
|
||||
for range recordsPerGoroutine {
|
||||
tracker.RecordAnnouncement()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
now = now.Add(time.Minute)
|
||||
|
||||
want := goroutines * recordsPerGoroutine
|
||||
if got := tracker.GetChurnPerMinute(); got != want {
|
||||
t.Fatalf("churn = %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user