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
+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)