Add -debug flag

This commit is contained in:
Pim van Pelt
2025-06-09 18:00:59 +02:00
parent a96b853d06
commit 6371e8eee2
3 changed files with 42 additions and 23 deletions

View File

@ -12,12 +12,14 @@ import (
type StatsCallback func(*api.InterfaceStats)
// StartStatsRoutine starts a goroutine that queries VPP interface stats at the specified interval
func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
go statsRoutine(statsSocketPath, period, callback)
func StartStatsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) {
go statsRoutine(statsSocketPath, period, callback, debug)
}
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback) {
log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period)
func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCallback, debug bool) {
if debug {
log.Printf("Starting VPP stats routine with socket: %s, period: %v", statsSocketPath, period)
}
// Create stats client
client := statsclient.NewStatsClient(statsSocketPath)
@ -31,7 +33,7 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa
defer c.Disconnect()
// Query stats immediately on startup
queryInterfaceStats(c, callback)
queryInterfaceStats(c, callback, debug)
ticker := time.NewTicker(period)
defer ticker.Stop()
@ -39,12 +41,12 @@ func statsRoutine(statsSocketPath string, period time.Duration, callback StatsCa
for {
select {
case <-ticker.C:
queryInterfaceStats(c, callback)
queryInterfaceStats(c, callback, debug)
}
}
}
func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) {
func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback, debug bool) {
// Create the proper struct for interface stats
stats := new(api.InterfaceStats)
@ -54,17 +56,21 @@ func queryInterfaceStats(c *core.StatsConnection, callback StatsCallback) {
return
}
// Log basic info
// Always log basic info
log.Printf("Retrieved stats for %d interfaces", len(stats.Interfaces))
for _, iface := range stats.Interfaces {
log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes",
iface.InterfaceIndex, iface.InterfaceName,
iface.Rx.Packets, iface.Rx.Bytes,
iface.Tx.Packets, iface.Tx.Bytes)
// Debug logging for individual interfaces
if debug {
for _, iface := range stats.Interfaces {
log.Printf("Interface %d (%s): RX %d pkts/%d bytes, TX %d pkts/%d bytes",
iface.InterfaceIndex, iface.InterfaceName,
iface.Rx.Packets, iface.Rx.Bytes,
iface.Tx.Packets, iface.Tx.Bytes)
}
}
// Call the callback to update the MIB
if callback != nil {
callback(stats)
}
}
}