From 688cc66ed8f1563fe490cd7e916ab1deed0f5379 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Thu, 11 Jun 2026 19:31:39 +0530 Subject: [PATCH] Add API to get latest node stats. (#4589) --- pkg/routing/nodestats.go | 11 ++++++++++- pkg/service/wire_gen.go | 4 ---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/routing/nodestats.go b/pkg/routing/nodestats.go index 999ae9cb3..ac16bb0ef 100644 --- a/pkg/routing/nodestats.go +++ b/pkg/routing/nodestats.go @@ -20,6 +20,7 @@ import ( "github.com/livekit/protocol/livekit" "github.com/livekit/protocol/logger" + "github.com/livekit/protocol/utils" "github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/telemetry/prometheus" @@ -29,7 +30,7 @@ type NodeStats struct { config config.NodeStatsConfig startedAt int64 - lock sync.Mutex + lock sync.RWMutex statsHistory []*livekit.NodeStats statsHistoryWritePtr int } @@ -80,3 +81,11 @@ func (n *NodeStats) UpdateAndGetNodeStats() (*livekit.NodeStats, error) { n.statsHistoryWritePtr = (n.statsHistoryWritePtr + 1) % len(n.statsHistory) return stats, nil } + +func (n *NodeStats) GetLatestNodeStats() *livekit.NodeStats { + n.lock.RLock() + defer n.lock.RUnlock() + + latestPtr := (n.statsHistoryWritePtr - 1 + len(n.statsHistory)) % len(n.statsHistory) + return utils.CloneProto(n.statsHistory[latestPtr]) +} diff --git a/pkg/service/wire_gen.go b/pkg/service/wire_gen.go index 5e9eff96c..45ebcf4d2 100644 --- a/pkg/service/wire_gen.go +++ b/pkg/service/wire_gen.go @@ -29,10 +29,6 @@ import ( "os" ) -import ( - _ "net/http/pprof" -) - // Injectors from wire.go: func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*LivekitServer, error) {