implement observability for room metrics (#3712)

* implement observability for room metrics

* deps

* test

* test

* Raja feedback

* cleanup
This commit is contained in:
Paul Wells
2025-06-09 09:32:58 -07:00
committed by GitHub
parent e7f0294e3f
commit 630aa7d970
19 changed files with 556 additions and 23 deletions
+6
View File
@@ -22,6 +22,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils/guid"
@@ -34,6 +35,7 @@ type AnalyticsService interface {
SendStats(ctx context.Context, stats []*livekit.AnalyticsStat)
SendEvent(ctx context.Context, events *livekit.AnalyticsEvent)
SendNodeRoomStates(ctx context.Context, nodeRooms *livekit.AnalyticsNodeRooms)
RoomProjectReporter(ctx context.Context) roomobs.ProjectReporter
}
type analyticsService struct {
@@ -95,3 +97,7 @@ func (a *analyticsService) SendNodeRoomStates(_ context.Context, nodeRooms *live
logger.Errorw("failed to send node room states", err)
}
}
func (a *analyticsService) RoomProjectReporter(_ context.Context) roomobs.ProjectReporter {
return roomobs.NewNoopProjectReporter()
}
+75 -14
View File
@@ -24,6 +24,7 @@ import (
"go.uber.org/atomic"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/utils"
)
@@ -55,16 +56,23 @@ type BytesTrackStats struct {
totalSendBytes, totalRecvBytes atomic.Uint64
totalSendMessages, totalRecvMessages atomic.Uint32
telemetry TelemetryService
reporter roomobs.TrackReporter
done core.Fuse
}
func NewBytesTrackStats(trackID livekit.TrackID, pID livekit.ParticipantID, telemetry TelemetryService) *BytesTrackStats {
func NewBytesTrackStats(
trackID livekit.TrackID,
pID livekit.ParticipantID,
telemetry TelemetryService,
participantReporter roomobs.ParticipantSessionReporter,
) *BytesTrackStats {
s := &BytesTrackStats{
trackID: trackID,
pID: pID,
telemetry: telemetry,
reporter: participantReporter.WithTrack(trackID.String()),
}
go s.reporter()
go s.worker()
return s
}
@@ -74,11 +82,23 @@ func (s *BytesTrackStats) AddBytes(bytes uint64, isSend bool) {
s.sendMessages.Inc()
s.totalSendBytes.Add(bytes)
s.totalSendMessages.Inc()
s.reporter.Tx(func(tx roomobs.TrackTx) {
tx.ReportType(roomobs.TrackTypeData)
tx.ReportSendBytes(uint32(bytes))
tx.ReportSendPackets(1)
})
} else {
s.recv.Add(bytes)
s.recvMessages.Inc()
s.totalRecvBytes.Add(bytes)
s.totalRecvMessages.Inc()
s.reporter.Tx(func(tx roomobs.TrackTx) {
tx.ReportType(roomobs.TrackTypeData)
tx.ReportRecvBytes(uint32(bytes))
tx.ReportRecvPackets(1)
})
}
}
@@ -98,29 +118,31 @@ func (s *BytesTrackStats) Stop() {
func (s *BytesTrackStats) report() {
if recv := s.recv.Swap(0); recv > 0 {
packets := s.recvMessages.Swap(0)
s.telemetry.TrackStats(StatsKeyForData(livekit.StreamType_UPSTREAM, s.pID, s.trackID), &livekit.AnalyticsStat{
Streams: []*livekit.AnalyticsStream{
{
PrimaryBytes: recv,
PrimaryPackets: s.recvMessages.Swap(0),
PrimaryPackets: packets,
},
},
})
}
if send := s.send.Swap(0); send > 0 {
packets := s.sendMessages.Swap(0)
s.telemetry.TrackStats(StatsKeyForData(livekit.StreamType_DOWNSTREAM, s.pID, s.trackID), &livekit.AnalyticsStat{
Streams: []*livekit.AnalyticsStream{
{
PrimaryBytes: send,
PrimaryPackets: s.sendMessages.Swap(0),
PrimaryPackets: packets,
},
},
})
}
}
func (s *BytesTrackStats) reporter() {
func (s *BytesTrackStats) worker() {
ticker := time.NewTicker(5 * time.Second)
defer func() {
ticker.Stop()
@@ -143,17 +165,30 @@ type BytesSignalStats struct {
BytesTrackStats
ctx context.Context
mu sync.Mutex
ri *livekit.Room
pi *livekit.ParticipantInfo
participantResolver roomobs.ParticipantReporterResolver
trackResolver roomobs.KeyResolver
mu sync.Mutex
ri *livekit.Room
pi *livekit.ParticipantInfo
stopped chan struct{}
}
func NewBytesSignalStats(ctx context.Context, telemetry TelemetryService) *BytesSignalStats {
func NewBytesSignalStats(
ctx context.Context,
telemetry TelemetryService,
) *BytesSignalStats {
projectReporter := telemetry.RoomProjectReporter(ctx)
participantReporter, participantReporterResolver := roomobs.DeferredParticipantReporter(projectReporter)
trackReporter, trackReporterResolver := participantReporter.WithDeferredTrack()
return &BytesSignalStats{
BytesTrackStats: BytesTrackStats{
telemetry: telemetry,
reporter: trackReporter,
},
ctx: ctx,
ctx: ctx,
participantResolver: participantReporterResolver,
trackResolver: trackReporterResolver,
}
}
@@ -181,6 +216,22 @@ func (s *BytesSignalStats) ResolveParticipant(pi *livekit.ParticipantInfo) {
}
}
func (s *BytesSignalStats) Reset() {
s.mu.Lock()
defer s.mu.Unlock()
if s.stopped != nil {
s.done.Break()
<-s.stopped
s.stopped = nil
s.done = core.Fuse{}
}
s.ri = nil
s.pi = nil
s.participantResolver.Reset()
s.trackResolver.Reset()
}
func (s *BytesSignalStats) maybeStart() {
if s.ri == nil || s.pi == nil {
return
@@ -189,17 +240,27 @@ func (s *BytesSignalStats) maybeStart() {
s.pID = livekit.ParticipantID(s.pi.Sid)
s.trackID = BytesTrackIDForParticipantID(BytesTrackTypeSignal, s.pID)
s.participantResolver.Resolve(
livekit.RoomName(s.ri.Name),
livekit.RoomID(s.ri.Sid),
livekit.ParticipantIdentity(s.pi.Identity),
livekit.ParticipantID(s.pi.Sid),
)
s.trackResolver.Resolve(string(s.trackID))
s.telemetry.ParticipantJoined(s.ctx, s.ri, s.pi, nil, nil, false)
go s.reporter()
s.stopped = make(chan struct{})
go s.worker()
}
func (s *BytesSignalStats) reporter() {
s.BytesTrackStats.reporter()
func (s *BytesSignalStats) worker() {
s.BytesTrackStats.worker()
s.telemetry.ParticipantLeft(s.ctx, s.ri, s.pi, false)
close(s.stopped)
}
// -----------------------------------------------------------------------
func BytesTrackIDForParticipantID(typ BytesTrackType, participantID livekit.ParticipantID) livekit.TrackID {
return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID))
return livekit.TrackID(fmt.Sprintf("%s%s%s", utils.TrackPrefix, typ, participantID))
}
+33
View File
@@ -264,6 +264,39 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
return stat
}
type CondensedStat struct {
StartTime time.Time
EndTime time.Time
Bytes uint64
Packets uint32
PacketsLost uint32
Frames uint32
}
func CondenseStat(stat *livekit.AnalyticsStat) (ps CondensedStat, ok bool) {
if ok = isValid(stat); !ok {
return
}
for _, stream := range stat.Streams {
startTime := stream.StartTime.AsTime()
endTime := stream.EndTime.AsTime()
if ps.StartTime.IsZero() || startTime.Before(ps.StartTime) {
ps.StartTime = startTime
}
if endTime.After(ps.EndTime) {
ps.EndTime = endTime
}
ps.Bytes += stream.PrimaryBytes
ps.Packets += stream.PrimaryPackets
ps.PacketsLost += stream.PacketsLost
ps.Frames += stream.Frames
}
return
}
func isValid(stat *livekit.AnalyticsStat) bool {
for _, analyticsStream := range stat.Streams {
if int32(analyticsStream.PrimaryPackets) < 0 ||
@@ -7,9 +7,21 @@ import (
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/observability/roomobs"
)
type FakeAnalyticsService struct {
RoomProjectReporterStub func(context.Context) roomobs.ProjectReporter
roomProjectReporterMutex sync.RWMutex
roomProjectReporterArgsForCall []struct {
arg1 context.Context
}
roomProjectReporterReturns struct {
result1 roomobs.ProjectReporter
}
roomProjectReporterReturnsOnCall map[int]struct {
result1 roomobs.ProjectReporter
}
SendEventStub func(context.Context, *livekit.AnalyticsEvent)
sendEventMutex sync.RWMutex
sendEventArgsForCall []struct {
@@ -32,6 +44,67 @@ type FakeAnalyticsService struct {
invocationsMutex sync.RWMutex
}
func (fake *FakeAnalyticsService) RoomProjectReporter(arg1 context.Context) roomobs.ProjectReporter {
fake.roomProjectReporterMutex.Lock()
ret, specificReturn := fake.roomProjectReporterReturnsOnCall[len(fake.roomProjectReporterArgsForCall)]
fake.roomProjectReporterArgsForCall = append(fake.roomProjectReporterArgsForCall, struct {
arg1 context.Context
}{arg1})
stub := fake.RoomProjectReporterStub
fakeReturns := fake.roomProjectReporterReturns
fake.recordInvocation("RoomProjectReporter", []interface{}{arg1})
fake.roomProjectReporterMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeAnalyticsService) RoomProjectReporterCallCount() int {
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
return len(fake.roomProjectReporterArgsForCall)
}
func (fake *FakeAnalyticsService) RoomProjectReporterCalls(stub func(context.Context) roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = stub
}
func (fake *FakeAnalyticsService) RoomProjectReporterArgsForCall(i int) context.Context {
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
argsForCall := fake.roomProjectReporterArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeAnalyticsService) RoomProjectReporterReturns(result1 roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = nil
fake.roomProjectReporterReturns = struct {
result1 roomobs.ProjectReporter
}{result1}
}
func (fake *FakeAnalyticsService) RoomProjectReporterReturnsOnCall(i int, result1 roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = nil
if fake.roomProjectReporterReturnsOnCall == nil {
fake.roomProjectReporterReturnsOnCall = make(map[int]struct {
result1 roomobs.ProjectReporter
})
}
fake.roomProjectReporterReturnsOnCall[i] = struct {
result1 roomobs.ProjectReporter
}{result1}
}
func (fake *FakeAnalyticsService) SendEvent(arg1 context.Context, arg2 *livekit.AnalyticsEvent) {
fake.sendEventMutex.Lock()
fake.sendEventArgsForCall = append(fake.sendEventArgsForCall, struct {
@@ -139,6 +212,8 @@ func (fake *FakeAnalyticsService) SendStatsArgsForCall(i int) (context.Context,
func (fake *FakeAnalyticsService) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
fake.sendEventMutex.RLock()
defer fake.sendEventMutex.RUnlock()
fake.sendNodeRoomStatesMutex.RLock()
@@ -8,6 +8,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/mime"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/observability/roomobs"
)
type FakeTelemetryService struct {
@@ -130,6 +131,17 @@ type FakeTelemetryService struct {
arg1 context.Context
arg2 *livekit.Room
}
RoomProjectReporterStub func(context.Context) roomobs.ProjectReporter
roomProjectReporterMutex sync.RWMutex
roomProjectReporterArgsForCall []struct {
arg1 context.Context
}
roomProjectReporterReturns struct {
result1 roomobs.ProjectReporter
}
roomProjectReporterReturnsOnCall map[int]struct {
result1 roomobs.ProjectReporter
}
RoomStartedStub func(context.Context, *livekit.Room)
roomStartedMutex sync.RWMutex
roomStartedArgsForCall []struct {
@@ -875,6 +887,67 @@ func (fake *FakeTelemetryService) RoomEndedArgsForCall(i int) (context.Context,
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeTelemetryService) RoomProjectReporter(arg1 context.Context) roomobs.ProjectReporter {
fake.roomProjectReporterMutex.Lock()
ret, specificReturn := fake.roomProjectReporterReturnsOnCall[len(fake.roomProjectReporterArgsForCall)]
fake.roomProjectReporterArgsForCall = append(fake.roomProjectReporterArgsForCall, struct {
arg1 context.Context
}{arg1})
stub := fake.RoomProjectReporterStub
fakeReturns := fake.roomProjectReporterReturns
fake.recordInvocation("RoomProjectReporter", []interface{}{arg1})
fake.roomProjectReporterMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeTelemetryService) RoomProjectReporterCallCount() int {
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
return len(fake.roomProjectReporterArgsForCall)
}
func (fake *FakeTelemetryService) RoomProjectReporterCalls(stub func(context.Context) roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = stub
}
func (fake *FakeTelemetryService) RoomProjectReporterArgsForCall(i int) context.Context {
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
argsForCall := fake.roomProjectReporterArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeTelemetryService) RoomProjectReporterReturns(result1 roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = nil
fake.roomProjectReporterReturns = struct {
result1 roomobs.ProjectReporter
}{result1}
}
func (fake *FakeTelemetryService) RoomProjectReporterReturnsOnCall(i int, result1 roomobs.ProjectReporter) {
fake.roomProjectReporterMutex.Lock()
defer fake.roomProjectReporterMutex.Unlock()
fake.RoomProjectReporterStub = nil
if fake.roomProjectReporterReturnsOnCall == nil {
fake.roomProjectReporterReturnsOnCall = make(map[int]struct {
result1 roomobs.ProjectReporter
})
}
fake.roomProjectReporterReturnsOnCall[i] = struct {
result1 roomobs.ProjectReporter
}{result1}
}
func (fake *FakeTelemetryService) RoomStarted(arg1 context.Context, arg2 *livekit.Room) {
fake.roomStartedMutex.Lock()
fake.roomStartedArgsForCall = append(fake.roomStartedArgsForCall, struct {
@@ -1575,6 +1648,8 @@ func (fake *FakeTelemetryService) Invocations() map[string][][]interface{} {
defer fake.reportMutex.RUnlock()
fake.roomEndedMutex.RLock()
defer fake.roomEndedMutex.RUnlock()
fake.roomProjectReporterMutex.RLock()
defer fake.roomProjectReporterMutex.RUnlock()
fake.roomStartedMutex.RLock()
defer fake.roomStartedMutex.RUnlock()
fake.sendEventMutex.RLock()