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
+19
View File
@@ -26,6 +26,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/rtc/dynacast"
@@ -74,6 +75,7 @@ type MediaTrackParams struct {
VideoConfig config.VideoConfig
Telemetry telemetry.TelemetryService
Logger logger.Logger
Reporter roomobs.TrackReporter
SimTracks map[uint32]SimulcastTrackInfo
OnRTCP func([]rtcp.Packet)
ForwardStats *sfu.ForwardStats
@@ -328,6 +330,23 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track sfu.TrackRe
if priority == 0 || regressionTargetCodecReceived {
key := telemetry.StatsKeyForTrack(livekit.StreamType_UPSTREAM, t.PublisherID(), t.ID(), ti.Source, ti.Type)
t.params.Telemetry.TrackStats(key, stat)
if cs, ok := telemetry.CondenseStat(stat); ok {
t.params.Reporter.Tx(func(tx roomobs.TrackTx) {
tx.ReportName(ti.Name)
tx.ReportKind(roomobs.TrackKindSub)
tx.ReportType(roomobs.TrackTypeFromProto(ti.Type))
tx.ReportSource(roomobs.TrackSourceFromProto(ti.Source))
tx.ReportMime(mime.NormalizeMimeType(ti.MimeType).ReporterType())
tx.ReportLayer(roomobs.PackTrackLayer(ti.Height, ti.Width))
tx.ReportDuration(uint16(cs.EndTime.Sub(cs.StartTime).Milliseconds()))
tx.ReportFrames(uint16(cs.Frames))
tx.ReportRecvBytes(uint32(cs.Bytes))
tx.ReportRecvPackets(cs.Packets)
tx.ReportPacketsLost(cs.PacketsLost)
tx.ReportScore(stat.Score)
})
}
}
})
+20
View File
@@ -27,6 +27,7 @@ import (
sutils "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu"
@@ -209,9 +210,28 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
subTrack.SetPublisherMuted(t.params.MediaTrack.IsMuted())
})
reporter := sub.GetReporter().WithTrack(trackID.String())
downTrack.OnStatsUpdate(func(_ *sfu.DownTrack, stat *livekit.AnalyticsStat) {
key := telemetry.StatsKeyForTrack(livekit.StreamType_DOWNSTREAM, subscriberID, trackID, t.params.MediaTrack.Source(), t.params.MediaTrack.Kind())
t.params.Telemetry.TrackStats(key, stat)
if cs, ok := telemetry.CondenseStat(stat); ok {
reporter.Tx(func(tx roomobs.TrackTx) {
ti := wr.TrackInfo()
tx.ReportName(ti.Name)
tx.ReportKind(roomobs.TrackKindPub)
tx.ReportType(roomobs.TrackTypeFromProto(ti.Type))
tx.ReportSource(roomobs.TrackSourceFromProto(ti.Source))
tx.ReportMime(mime.NormalizeMimeType(ti.MimeType).ReporterType())
tx.ReportLayer(roomobs.PackTrackLayer(ti.Height, ti.Width))
tx.ReportDuration(uint16(cs.EndTime.Sub(cs.StartTime).Milliseconds()))
tx.ReportFrames(uint16(cs.Frames))
tx.ReportSendBytes(uint32(cs.Bytes))
tx.ReportSendPackets(cs.Packets)
tx.ReportPacketsLost(cs.PacketsLost)
tx.ReportScore(stat.Score)
})
}
})
downTrack.OnMaxLayerChanged(func(dt *sfu.DownTrack, layer int32) {
+32 -1
View File
@@ -37,6 +37,8 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid"
"github.com/livekit/protocol/utils/pointer"
@@ -130,6 +132,8 @@ type ParticipantParams struct {
SubscribeEnabledCodecs []*livekit.Codec
Logger logger.Logger
LoggerResolver logger.DeferredFieldResolver
Reporter roomobs.ParticipantSessionReporter
ReporterResolver roomobs.ParticipantReporterResolver
SimTracks map[uint32]SimulcastTrackInfo
Grants *auth.ClaimGrants
InitialVersion uint32
@@ -191,7 +195,8 @@ type ParticipantImpl struct {
sessionStartRecorded atomic.Bool
lastActiveAt atomic.Pointer[time.Time]
// when first connected
connectedAt time.Time
connectedAt time.Time
disconnectedAt atomic.Pointer[time.Time]
// timer that's set when disconnect is detected on primary PC
disconnectTimer *time.Timer
migrationTimer *time.Timer
@@ -303,6 +308,7 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
telemetry.BytesTrackIDForParticipantID(telemetry.BytesTrackTypeData, p.ID()),
p.ID(),
params.Telemetry,
params.Reporter,
)
p.participantHelper.Store(params.ParticipantHelper)
if !params.DisableSupervisor {
@@ -323,6 +329,20 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
p.supervisor.OnPublicationError(p.onPublicationError)
}
sessionTimer := observability.NewSessionTimer(p.params.SessionStartTime)
params.Reporter.RegisterFunc(func(ts time.Time, tx roomobs.ParticipantSessionTx) bool {
if dts := p.disconnectedAt.Load(); dts != nil {
ts = *dts
tx.ReportEndTime(ts)
}
millis, mins := sessionTimer.Advance(ts)
tx.ReportDuration(uint16(millis))
tx.ReportDurationMinutes(uint8(mins))
return !p.IsDisconnected()
})
var err error
// keep last participants and when updates were sent
if p.updateCache, err = lru.New[livekit.ParticipantID, participantUpdateInfo](128); err != nil {
@@ -355,6 +375,14 @@ func (p *ParticipantImpl) GetLoggerResolver() logger.DeferredFieldResolver {
return p.params.LoggerResolver
}
func (p *ParticipantImpl) GetReporter() roomobs.ParticipantSessionReporter {
return p.params.Reporter
}
func (p *ParticipantImpl) GetReporterResolver() roomobs.ParticipantReporterResolver {
return p.params.ReporterResolver
}
func (p *ParticipantImpl) GetAdaptiveStream() bool {
return p.params.AdaptiveStream
}
@@ -1804,6 +1832,7 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
}
if state == livekit.ParticipantInfo_DISCONNECTED && oldState == livekit.ParticipantInfo_ACTIVE {
p.disconnectedAt.Store(pointer.To(time.Now()))
prometheus.RecordSessionDuration(int(p.ProtocolVersion()), time.Since(*p.lastActiveAt.Load()))
}
}
@@ -2708,6 +2737,7 @@ func (p *ParticipantImpl) addMediaTrack(signalCid string, sdpCid string, ti *liv
VideoConfig: p.params.VideoConfig,
Telemetry: p.params.Telemetry,
Logger: LoggerWithTrack(p.pubLogger, livekit.TrackID(ti.Sid), false),
Reporter: p.params.Reporter.WithTrack(ti.Sid),
SubscriberConfig: p.params.Config.Subscriber,
PLIThrottleConfig: p.params.PLIThrottleConfig,
SimTracks: p.params.SimTracks,
@@ -3345,6 +3375,7 @@ func (p *ParticipantImpl) MoveToRoom(params types.MoveToRoomParams) {
p.params.Logger.Infow("move participant to new room", "newRoomName", params.RoomName, "newID", params.ParticipantID)
p.params.LoggerResolver.Reset()
p.params.ReporterResolver.Reset()
p.participantHelper.Store(params.Helper)
p.SubscriptionManager.ClearAllSubscriptions()
p.id.Store(params.ParticipantID)
+2
View File
@@ -31,6 +31,7 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
lksdp "github.com/livekit/protocol/sdp"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid"
@@ -703,6 +704,7 @@ func newParticipantForTestWithOpts(identity livekit.ParticipantIdentity, opts *p
ClientConf: opts.clientConf,
ClientInfo: ClientInfo{ClientInfo: opts.clientInfo},
Logger: LoggerWithParticipant(logger.GetLogger(), identity, sid, false),
Reporter: roomobs.NewNoopParticipantSessionReporter(),
Telemetry: &telemetryfakes.FakeTelemetryService{},
VersionGenerator: utils.NewDefaultTimedVersionGenerator(),
ParticipantHelper: &typesfakes.FakeLocalParticipantHelper{},
+11 -1
View File
@@ -30,6 +30,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"
"github.com/livekit/protocol/utils/guid"
@@ -456,13 +457,16 @@ func (r *Room) Join(participant types.LocalParticipant, requestSource routing.Me
// subscribe participant to existing published tracks
r.subscribeToExistingTracks(p)
connectTime := time.Since(p.ConnectedAt())
meta := &livekit.AnalyticsClientMeta{
ClientConnectTime: uint32(time.Since(p.ConnectedAt()).Milliseconds()),
ClientConnectTime: uint32(connectTime.Milliseconds()),
}
infos := p.GetICEConnectionInfo()
var connectionType roomobs.ConnectionType
for _, info := range infos {
if info.Type != types.ICEConnectionTypeUnknown {
meta.ConnectionType = info.Type.String()
connectionType = info.Type.ReporterType()
break
}
}
@@ -473,6 +477,12 @@ func (r *Room) Join(participant types.LocalParticipant, requestSource routing.Me
false,
)
participant.GetReporter().Tx(func(tx roomobs.ParticipantSessionTx) {
tx.ReportClientConnectTime(uint16(connectTime.Milliseconds()))
tx.ReportConnectResult(roomobs.ConnectionResultSuccess)
tx.ReportConnectionType(connectionType)
})
fields := append(
connectionDetailsFields(infos),
"clientInfo", logger.Proto(sutils.ClientInfoWithoutAddress(p.GetClientInfo())),
+2
View File
@@ -17,6 +17,7 @@ package rtc
import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid"
@@ -75,6 +76,7 @@ func NewMockParticipant(identity livekit.ParticipantIdentity, protocol types.Pro
updateTrack()
})
p.GetLoggerReturns(logger.GetLogger())
p.GetReporterReturns(roomobs.NewNoopParticipantSessionReporter())
return p
}
+25 -4
View File
@@ -27,6 +27,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
)
type ICEConnectionType int
@@ -44,21 +45,32 @@ func (i ICEConnectionType) String() string {
switch i {
case ICEConnectionTypeUnknown:
return "unknown"
case ICEConnectionTypeUDP:
return "udp"
case ICEConnectionTypeTCP:
return "tcp"
case ICEConnectionTypeTURN:
return "turn"
default:
return "unknown"
}
}
func (i ICEConnectionType) ReporterType() roomobs.ConnectionType {
switch i {
case ICEConnectionTypeUnknown:
return roomobs.ConnectionTypeUndefined
case ICEConnectionTypeUDP:
return roomobs.ConnectionTypeUDP
case ICEConnectionTypeTCP:
return roomobs.ConnectionTypeTCP
case ICEConnectionTypeTURN:
return roomobs.ConnectionTypeTurn
default:
return roomobs.ConnectionTypeUndefined
}
}
// --------------------------------------------
type ICECandidateExtended struct {
@@ -83,6 +95,15 @@ func (i *ICEConnectionInfo) HasCandidates() bool {
return len(i.Local) > 0 || len(i.Remote) > 0
}
func ICEConnectionInfosType(infos []*ICEConnectionInfo) ICEConnectionType {
for _, info := range infos {
if info.Type != ICEConnectionTypeUnknown {
return info.Type
}
}
return ICEConnectionTypeUnknown
}
// --------------------------------------------
type ICEConnectionDetails struct {
+3
View File
@@ -24,6 +24,7 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/routing"
@@ -342,6 +343,8 @@ type LocalParticipant interface {
GetTrailer() []byte
GetLogger() logger.Logger
GetLoggerResolver() logger.DeferredFieldResolver
GetReporter() roomobs.ParticipantSessionReporter
GetReporterResolver() roomobs.ParticipantReporterResolver
GetAdaptiveStream() bool
ProtocolVersion() ProtocolVersion
SupportsSyncStreamID() bool
@@ -13,6 +13,7 @@ import (
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/observability/roomobs"
"github.com/livekit/protocol/utils"
"github.com/pion/rtcp"
webrtc "github.com/pion/webrtc/v4"
@@ -412,6 +413,26 @@ type FakeLocalParticipant struct {
result1 string
result2 error
}
GetReporterStub func() roomobs.ParticipantSessionReporter
getReporterMutex sync.RWMutex
getReporterArgsForCall []struct {
}
getReporterReturns struct {
result1 roomobs.ParticipantSessionReporter
}
getReporterReturnsOnCall map[int]struct {
result1 roomobs.ParticipantSessionReporter
}
GetReporterResolverStub func() roomobs.ParticipantReporterResolver
getReporterResolverMutex sync.RWMutex
getReporterResolverArgsForCall []struct {
}
getReporterResolverReturns struct {
result1 roomobs.ParticipantReporterResolver
}
getReporterResolverReturnsOnCall map[int]struct {
result1 roomobs.ParticipantReporterResolver
}
GetSubscribedParticipantsStub func() []livekit.ParticipantID
getSubscribedParticipantsMutex sync.RWMutex
getSubscribedParticipantsArgsForCall []struct {
@@ -3312,6 +3333,112 @@ func (fake *FakeLocalParticipant) GetPublisherICESessionUfragReturnsOnCall(i int
}{result1, result2}
}
func (fake *FakeLocalParticipant) GetReporter() roomobs.ParticipantSessionReporter {
fake.getReporterMutex.Lock()
ret, specificReturn := fake.getReporterReturnsOnCall[len(fake.getReporterArgsForCall)]
fake.getReporterArgsForCall = append(fake.getReporterArgsForCall, struct {
}{})
stub := fake.GetReporterStub
fakeReturns := fake.getReporterReturns
fake.recordInvocation("GetReporter", []interface{}{})
fake.getReporterMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) GetReporterCallCount() int {
fake.getReporterMutex.RLock()
defer fake.getReporterMutex.RUnlock()
return len(fake.getReporterArgsForCall)
}
func (fake *FakeLocalParticipant) GetReporterCalls(stub func() roomobs.ParticipantSessionReporter) {
fake.getReporterMutex.Lock()
defer fake.getReporterMutex.Unlock()
fake.GetReporterStub = stub
}
func (fake *FakeLocalParticipant) GetReporterReturns(result1 roomobs.ParticipantSessionReporter) {
fake.getReporterMutex.Lock()
defer fake.getReporterMutex.Unlock()
fake.GetReporterStub = nil
fake.getReporterReturns = struct {
result1 roomobs.ParticipantSessionReporter
}{result1}
}
func (fake *FakeLocalParticipant) GetReporterReturnsOnCall(i int, result1 roomobs.ParticipantSessionReporter) {
fake.getReporterMutex.Lock()
defer fake.getReporterMutex.Unlock()
fake.GetReporterStub = nil
if fake.getReporterReturnsOnCall == nil {
fake.getReporterReturnsOnCall = make(map[int]struct {
result1 roomobs.ParticipantSessionReporter
})
}
fake.getReporterReturnsOnCall[i] = struct {
result1 roomobs.ParticipantSessionReporter
}{result1}
}
func (fake *FakeLocalParticipant) GetReporterResolver() roomobs.ParticipantReporterResolver {
fake.getReporterResolverMutex.Lock()
ret, specificReturn := fake.getReporterResolverReturnsOnCall[len(fake.getReporterResolverArgsForCall)]
fake.getReporterResolverArgsForCall = append(fake.getReporterResolverArgsForCall, struct {
}{})
stub := fake.GetReporterResolverStub
fakeReturns := fake.getReporterResolverReturns
fake.recordInvocation("GetReporterResolver", []interface{}{})
fake.getReporterResolverMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) GetReporterResolverCallCount() int {
fake.getReporterResolverMutex.RLock()
defer fake.getReporterResolverMutex.RUnlock()
return len(fake.getReporterResolverArgsForCall)
}
func (fake *FakeLocalParticipant) GetReporterResolverCalls(stub func() roomobs.ParticipantReporterResolver) {
fake.getReporterResolverMutex.Lock()
defer fake.getReporterResolverMutex.Unlock()
fake.GetReporterResolverStub = stub
}
func (fake *FakeLocalParticipant) GetReporterResolverReturns(result1 roomobs.ParticipantReporterResolver) {
fake.getReporterResolverMutex.Lock()
defer fake.getReporterResolverMutex.Unlock()
fake.GetReporterResolverStub = nil
fake.getReporterResolverReturns = struct {
result1 roomobs.ParticipantReporterResolver
}{result1}
}
func (fake *FakeLocalParticipant) GetReporterResolverReturnsOnCall(i int, result1 roomobs.ParticipantReporterResolver) {
fake.getReporterResolverMutex.Lock()
defer fake.getReporterResolverMutex.Unlock()
fake.GetReporterResolverStub = nil
if fake.getReporterResolverReturnsOnCall == nil {
fake.getReporterResolverReturnsOnCall = make(map[int]struct {
result1 roomobs.ParticipantReporterResolver
})
}
fake.getReporterResolverReturnsOnCall[i] = struct {
result1 roomobs.ParticipantReporterResolver
}{result1}
}
func (fake *FakeLocalParticipant) GetSubscribedParticipants() []livekit.ParticipantID {
fake.getSubscribedParticipantsMutex.Lock()
ret, specificReturn := fake.getSubscribedParticipantsReturnsOnCall[len(fake.getSubscribedParticipantsArgsForCall)]
@@ -8199,6 +8326,10 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
defer fake.getPublishedTracksMutex.RUnlock()
fake.getPublisherICESessionUfragMutex.RLock()
defer fake.getPublisherICESessionUfragMutex.RUnlock()
fake.getReporterMutex.RLock()
defer fake.getReporterMutex.RUnlock()
fake.getReporterResolverMutex.RLock()
defer fake.getReporterResolverMutex.RUnlock()
fake.getSubscribedParticipantsMutex.RLock()
defer fake.getSubscribedParticipantsMutex.RUnlock()
fake.getSubscribedTracksMutex.RLock()