Record join/publish/subscribe cancellations. (#4102)

To get better picture of success/failure rate.
This commit is contained in:
Raja Subramanian
2025-11-25 14:06:02 +05:30
committed by GitHub
parent 402936324c
commit 7f10e18bac
9 changed files with 90 additions and 30 deletions
+10 -2
View File
@@ -1525,6 +1525,12 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
}
p.pendingTracksLock.Lock()
for _, pti := range p.pendingTracks {
if len(pti.trackInfos) == 0 {
continue
}
prometheus.RecordTrackPublishCancels(pti.trackInfos[0].Type.String(), int32(len(pti.trackInfos)))
}
p.pendingTracks = make(map[string]*pendingTrackInfo)
p.pendingPublishingTracks = make(map[livekit.TrackID]*pendingTrackInfo)
p.pendingTracksLock.Unlock()
@@ -3006,7 +3012,8 @@ func (p *ParticipantImpl) addPendingTrackLocked(req *livekit.AddTrackRequest) *l
mimeType := codec.Codec
videoLayerMode := codec.VideoLayerMode
if req.Type == livekit.TrackType_VIDEO {
switch req.Type {
case livekit.TrackType_VIDEO:
if !mime.IsMimeTypeStringVideo(mimeType) {
mimeType = mime.MimeTypePrefixVideo + mimeType
}
@@ -3032,7 +3039,8 @@ func (p *ParticipantImpl) addPendingTrackLocked(req *livekit.AddTrackRequest) *l
}
}
}
} else if req.Type == livekit.TrackType_AUDIO {
case livekit.TrackType_AUDIO:
if !mime.IsMimeTypeStringAudio(mimeType) {
mimeType = mime.MimeTypePrefixAudio + mimeType
}
+19
View File
@@ -120,6 +120,8 @@ func (m *SubscriptionManager) Close(isExpectedToResume bool) {
<-m.doneCh
done.Store(true)
prometheus.RecordTrackSubscribeCancels(int32(m.getNumCancellations()))
subTracks := m.GetSubscribedTracks()
downTracksToClose := make([]*sfu.DownTrack, 0, len(subTracks))
for _, st := range subTracks {
@@ -891,6 +893,19 @@ func (m *SubscriptionManager) handleSubscribedTrackClose(s *trackSubscription, i
}
}
func (m *SubscriptionManager) getNumCancellations() int {
m.lock.RLock()
defer m.lock.RUnlock()
numCancellations := 0
for _, sub := range m.subscriptions {
if sub.isCanceled() {
numCancellations++
}
}
return numCancellations
}
// --------------------------------------------------------------------------------------
type trackSubscription struct {
@@ -1173,6 +1188,10 @@ func (s *trackSubscription) maybeRecordSuccess(ts telemetry.TelemetryService, pI
ts.TrackSubscribed(context.Background(), pID, mediaTrack.ToProto(), pi, !eventSent)
}
func (s *trackSubscription) isCanceled() bool {
return !s.eventSent.Load() && s.isDesired()
}
func (s *trackSubscription) durationSinceStart() time.Duration {
t := s.subStartedAt.Load()
if t == nil {
+2
View File
@@ -338,6 +338,7 @@ func (r *RoomManager) StartSession(
Leave: leave,
},
})
prometheus.IncrementParticipantRtcCanceled(1)
return errors.New("could not restart closed participant")
}
@@ -395,6 +396,7 @@ func (r *RoomManager) StartSession(
Leave: leave,
},
})
prometheus.IncrementParticipantRtcCanceled(1)
return errors.New("could not restart participant")
}
+2 -2
View File
@@ -221,7 +221,7 @@ func (t *telemetryService) TrackPublishRequested(
track *livekit.TrackInfo,
) {
t.enqueue(func() {
prometheus.AddPublishAttempt(track.Type.String())
prometheus.RecordTrackPublishAttempt(track.Type.String())
room := t.getRoomDetails(participantID)
ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_PUBLISH_REQUESTED, room, participantID, track)
if ev.Participant != nil {
@@ -239,7 +239,7 @@ func (t *telemetryService) TrackPublished(
) {
t.enqueue(func() {
prometheus.AddPublishedTrack(track.Type.String())
prometheus.AddPublishSuccess(track.Type.String())
prometheus.RecordTrackPublishSuccess(track.Type.String())
room := t.getRoomDetails(participantID)
participant := &livekit.ParticipantInfo{
+6
View File
@@ -143,8 +143,10 @@ func GetNodeStats(nodeStartedAt int64, prevStats []*livekit.NodeStats, rateInter
NumTracksOut: trackSubscribedCurrent.Load(),
NumTrackPublishAttempts: trackPublishAttempts.Load(),
NumTrackPublishSuccess: trackPublishSuccess.Load(),
NumTrackPublishCancels: trackPublishCancels.Load(),
NumTrackSubscribeAttempts: trackSubscribeAttempts.Load(),
NumTrackSubscribeSuccess: trackSubscribeSuccess.Load(),
NumTrackSubscribeCancels: trackSubscribeCancels.Load(),
BytesIn: bytesIn.Load(),
BytesOut: bytesOut.Load(),
PacketsIn: packetsIn.Load(),
@@ -155,6 +157,7 @@ func GetNodeStats(nodeStartedAt int64, prevStats []*livekit.NodeStats, rateInter
ParticipantSignalConnected: participantSignalConnected.Load(),
ParticipantRtcInit: participantRTCInit.Load(),
ParticipantRtcConnected: participantRTCConnected.Load(),
ParticipantRtcCanceled: participantRTCCanceled.Load(),
ForwardLatency: forwardLatency.Load(),
ForwardJitter: forwardJitter.Load(),
NumCpus: uint32(cpuStats.NumCPU()), // this will round down to the nearest integer
@@ -235,12 +238,15 @@ func getNodeStatsRate(statsHistory []*livekit.NodeStats) *livekit.NodeStatsRate
ParticipantSignalConnected: perSec(earlier.ParticipantSignalConnected, later.ParticipantSignalConnected, elapsed),
ParticipantRtcInit: perSec(earlier.ParticipantRtcInit, later.ParticipantRtcInit, elapsed),
ParticipantRtcConnected: perSec(earlier.ParticipantRtcConnected, later.ParticipantRtcConnected, elapsed),
ParticipantRtcCanceled: perSec(earlier.ParticipantRtcCanceled, later.ParticipantRtcCanceled, elapsed),
SysPacketsOut: perSec(uint64(earlier.SysPacketsOut), uint64(later.SysPacketsOut), elapsed),
SysPacketsDropped: perSec(uint64(earlier.SysPacketsDropped), uint64(later.SysPacketsDropped), elapsed),
TrackPublishAttempts: perSec(uint64(earlier.NumTrackPublishAttempts), uint64(later.NumTrackPublishAttempts), elapsed),
TrackPublishSuccess: perSec(uint64(earlier.NumTrackPublishSuccess), uint64(later.NumTrackPublishSuccess), elapsed),
TrackPublishCancels: perSec(uint64(earlier.NumTrackPublishCancels), uint64(later.NumTrackPublishCancels), elapsed),
TrackSubscribeAttempts: perSec(uint64(earlier.NumTrackSubscribeAttempts), uint64(later.NumTrackSubscribeAttempts), elapsed),
TrackSubscribeSuccess: perSec(uint64(earlier.NumTrackSubscribeSuccess), uint64(later.NumTrackSubscribeSuccess), elapsed),
TrackSubscribeCancels: perSec(uint64(earlier.NumTrackSubscribeCancels), uint64(later.NumTrackSubscribeCancels), elapsed),
CpuLoad: cpuLoad / float32(elapsed),
MemoryLoad: memoryLoad / float32(elapsed),
MemoryUsed: memoryUsed / float32(elapsed),
+8
View File
@@ -46,6 +46,7 @@ var (
participantSignalConnected atomic.Uint64
participantRTCConnected atomic.Uint64
participantRTCInit atomic.Uint64
participantRTCCanceled atomic.Uint64
forwardLatency atomic.Uint32
forwardJitter atomic.Uint32
@@ -328,6 +329,13 @@ func IncrementParticipantRtcConnected(join uint32) {
}
}
func IncrementParticipantRtcCanceled(numCancels uint64) {
if numCancels > 0 {
participantRTCConnected.Add(numCancels)
promParticipantJoin.WithLabelValues("rtc_canceled").Add(float64(numCancels))
}
}
func AddConnection(direction Direction) {
promConnections.WithLabelValues(string(direction)).Add(1)
}
+15 -2
View File
@@ -31,8 +31,10 @@ var (
trackSubscribedCurrent atomic.Int32
trackPublishAttempts atomic.Int32
trackPublishSuccess atomic.Int32
trackPublishCancels atomic.Int32
trackSubscribeAttempts atomic.Int32
trackSubscribeSuccess atomic.Int32
trackSubscribeCancels atomic.Int32
// count the number of failures that are due to user error (permissions, track doesn't exist), so we could compute
// success rate by subtracting this from total attempts
trackSubscribeUserError atomic.Int32
@@ -162,16 +164,21 @@ func SubPublishedTrack(kind string) {
trackPublishedCurrent.Dec()
}
func AddPublishAttempt(kind string) {
func RecordTrackPublishAttempt(kind string) {
trackPublishAttempts.Inc()
promTrackPublishCounter.WithLabelValues(kind, "attempt").Inc()
}
func AddPublishSuccess(kind string) {
func RecordTrackPublishSuccess(kind string) {
trackPublishSuccess.Inc()
promTrackPublishCounter.WithLabelValues(kind, "success").Inc()
}
func RecordTrackPublishCancels(kind string, numCancels int32) {
trackPublishCancels.Add(numCancels)
promTrackPublishCounter.WithLabelValues(kind, "cancel").Add(float64(numCancels))
}
func RecordPublishTime(
country string,
source livekit.TrackSource,
@@ -246,9 +253,15 @@ func RecordTrackSubscribeFailure(err error, isUserError bool) {
if isUserError {
trackSubscribeUserError.Inc()
trackSubscribeCancels.Inc()
}
}
func RecordTrackSubscribeCancels(numCancels int32) {
trackSubscribeCancels.Add(numCancels)
promTrackSubscribeCounter.WithLabelValues("cancel", "").Add(float64(numCancels))
}
func RecordSessionStartTime(protocolVersion int, d time.Duration) {
promSessionStartTime.WithLabelValues(strconv.Itoa(protocolVersion)).Observe(float64(d.Milliseconds()))
}