Fix incorrect unsubscribed track telemetry (#1350)

- only log unsubscribe on close if Track was actually bound
- update subscribed counters even if a failure had been logged before
This commit is contained in:
David Zhao
2023-01-30 10:16:21 -08:00
committed by GitHub
parent 2671493870
commit b023c531c2
5 changed files with 42 additions and 23 deletions
+22 -12
View File
@@ -482,6 +482,7 @@ func (m *SubscriptionManager) handleSubscribedTrackClose(s *trackSubscription, w
s.logger.Debugw("subscribed track closed", s.logger.Debugw("subscribed track closed",
"willBeResumed", willBeResumed, "willBeResumed", willBeResumed,
) )
wasBound := s.isBound()
subTrack := s.getSubscribedTrack() subTrack := s.getSubscribedTrack()
if subTrack == nil { if subTrack == nil {
return return
@@ -509,15 +510,19 @@ func (m *SubscriptionManager) handleSubscribedTrackClose(s *trackSubscription, w
subTrack.OnClose(nil) subTrack.OnClose(nil)
go m.params.OnTrackUnsubscribed(subTrack) go m.params.OnTrackUnsubscribed(subTrack)
// always trigger to decrement unsubscribed counter. However, only log an analytics event when // trigger to decrement unsubscribed counter as long as track has been bound
// Only log an analytics event when
// * the participant isn't closing // * the participant isn't closing
// * it's not a migration // * it's not a migration
m.params.Telemetry.TrackUnsubscribed( if wasBound {
context.Background(), m.params.Telemetry.TrackUnsubscribed(
m.params.Participant.ID(), context.Background(),
&livekit.TrackInfo{Sid: string(s.trackID), Type: subTrack.MediaTrack().Kind()}, m.params.Participant.ID(),
!willBeResumed && !m.params.Participant.IsClosed(), &livekit.TrackInfo{Sid: string(s.trackID), Type: subTrack.MediaTrack().Kind()},
) !willBeResumed && !m.params.Participant.IsClosed(),
)
}
if !willBeResumed { if !willBeResumed {
sender := subTrack.RTPSender() sender := subTrack.RTPSender()
if sender != nil { if sender != nil {
@@ -689,6 +694,12 @@ func (s *trackSubscription) setBound() {
s.bound = true s.bound = true
} }
func (s *trackSubscription) isBound() bool {
s.lock.RLock()
defer s.lock.RUnlock()
return s.bound
}
func (s *trackSubscription) startAttempt() { func (s *trackSubscription) startAttempt() {
if s.numAttempts.Load() == 0 { if s.numAttempts.Load() == 0 {
t := time.Now() t := time.Now()
@@ -722,14 +733,13 @@ func (s *trackSubscription) maybeRecordSuccess(ts telemetry.TelemetryService, pI
return return
} }
if s.eventSent.Swap(true) { eventSent := s.eventSent.Swap(true)
return
}
ts.TrackSubscribed(context.Background(), pID, mediaTrack.ToProto(), &livekit.ParticipantInfo{ pi := &livekit.ParticipantInfo{
Identity: string(subTrack.PublisherIdentity()), Identity: string(subTrack.PublisherIdentity()),
Sid: string(subTrack.PublisherID()), Sid: string(subTrack.PublisherID()),
}) }
ts.TrackSubscribed(context.Background(), pID, mediaTrack.ToProto(), pi, !eventSent)
} }
func (s *trackSubscription) durationSinceStart() time.Duration { func (s *trackSubscription) durationSinceStart() time.Duration {
+5
View File
@@ -260,10 +260,15 @@ func (t *telemetryService) TrackSubscribed(
participantID livekit.ParticipantID, participantID livekit.ParticipantID,
track *livekit.TrackInfo, track *livekit.TrackInfo,
publisher *livekit.ParticipantInfo, publisher *livekit.ParticipantInfo,
shouldSendEvent bool,
) { ) {
t.enqueue(func() { t.enqueue(func() {
prometheus.RecordTrackSubscribeSuccess(track.Type.String()) prometheus.RecordTrackSubscribeSuccess(track.Type.String())
if !shouldSendEvent {
return
}
room := t.getRoomDetails(participantID) room := t.getRoomDetails(participantID)
ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBED, room, participantID, track) ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBED, room, participantID, track)
ev.Publisher = publisher ev.Publisher = publisher
+4 -2
View File
@@ -207,10 +207,12 @@ func Test_OnTrackSubscribed_EventIsSent(t *testing.T) {
require.Equal(t, room, event.Room) require.Equal(t, room, event.Room)
// do // do
fixture.sut.TrackSubscribed(context.Background(), livekit.ParticipantID(partSID), trackInfo, publisherInfo) fixture.sut.TrackSubscribed(context.Background(), livekit.ParticipantID(partSID), trackInfo, publisherInfo, true)
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)
require.Equal(t, 2, fixture.analytics.SendEventCallCount()) require.Eventually(t, func() bool {
return fixture.analytics.SendEventCallCount() == 2
}, time.Second, time.Millisecond*50, "expected send event to be called twice")
_, eventTrackSubscribed := fixture.analytics.SendEventArgsForCall(1) _, eventTrackSubscribed := fixture.analytics.SendEventArgsForCall(1)
require.Equal(t, livekit.AnalyticsEventType_TRACK_SUBSCRIBED, eventTrackSubscribed.Type) require.Equal(t, livekit.AnalyticsEventType_TRACK_SUBSCRIBED, eventTrackSubscribed.Type)
require.Equal(t, partSID, eventTrackSubscribed.ParticipantId) require.Equal(t, partSID, eventTrackSubscribed.ParticipantId)
@@ -152,13 +152,14 @@ type FakeTelemetryService struct {
arg3 *livekit.TrackInfo arg3 *livekit.TrackInfo
arg4 *livekit.ParticipantInfo arg4 *livekit.ParticipantInfo
} }
TrackSubscribedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo) TrackSubscribedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo, bool)
trackSubscribedMutex sync.RWMutex trackSubscribedMutex sync.RWMutex
trackSubscribedArgsForCall []struct { trackSubscribedArgsForCall []struct {
arg1 context.Context arg1 context.Context
arg2 livekit.ParticipantID arg2 livekit.ParticipantID
arg3 *livekit.TrackInfo arg3 *livekit.TrackInfo
arg4 *livekit.ParticipantInfo arg4 *livekit.ParticipantInfo
arg5 bool
} }
TrackUnmutedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo) TrackUnmutedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo)
trackUnmutedMutex sync.RWMutex trackUnmutedMutex sync.RWMutex
@@ -868,19 +869,20 @@ func (fake *FakeTelemetryService) TrackSubscribeRequestedArgsForCall(i int) (con
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
} }
func (fake *FakeTelemetryService) TrackSubscribed(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo, arg4 *livekit.ParticipantInfo) { func (fake *FakeTelemetryService) TrackSubscribed(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo, arg4 *livekit.ParticipantInfo, arg5 bool) {
fake.trackSubscribedMutex.Lock() fake.trackSubscribedMutex.Lock()
fake.trackSubscribedArgsForCall = append(fake.trackSubscribedArgsForCall, struct { fake.trackSubscribedArgsForCall = append(fake.trackSubscribedArgsForCall, struct {
arg1 context.Context arg1 context.Context
arg2 livekit.ParticipantID arg2 livekit.ParticipantID
arg3 *livekit.TrackInfo arg3 *livekit.TrackInfo
arg4 *livekit.ParticipantInfo arg4 *livekit.ParticipantInfo
}{arg1, arg2, arg3, arg4}) arg5 bool
}{arg1, arg2, arg3, arg4, arg5})
stub := fake.TrackSubscribedStub stub := fake.TrackSubscribedStub
fake.recordInvocation("TrackSubscribed", []interface{}{arg1, arg2, arg3, arg4}) fake.recordInvocation("TrackSubscribed", []interface{}{arg1, arg2, arg3, arg4, arg5})
fake.trackSubscribedMutex.Unlock() fake.trackSubscribedMutex.Unlock()
if stub != nil { if stub != nil {
fake.TrackSubscribedStub(arg1, arg2, arg3, arg4) fake.TrackSubscribedStub(arg1, arg2, arg3, arg4, arg5)
} }
} }
@@ -890,17 +892,17 @@ func (fake *FakeTelemetryService) TrackSubscribedCallCount() int {
return len(fake.trackSubscribedArgsForCall) return len(fake.trackSubscribedArgsForCall)
} }
func (fake *FakeTelemetryService) TrackSubscribedCalls(stub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo)) { func (fake *FakeTelemetryService) TrackSubscribedCalls(stub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo, bool)) {
fake.trackSubscribedMutex.Lock() fake.trackSubscribedMutex.Lock()
defer fake.trackSubscribedMutex.Unlock() defer fake.trackSubscribedMutex.Unlock()
fake.TrackSubscribedStub = stub fake.TrackSubscribedStub = stub
} }
func (fake *FakeTelemetryService) TrackSubscribedArgsForCall(i int) (context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo) { func (fake *FakeTelemetryService) TrackSubscribedArgsForCall(i int) (context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo, bool) {
fake.trackSubscribedMutex.RLock() fake.trackSubscribedMutex.RLock()
defer fake.trackSubscribedMutex.RUnlock() defer fake.trackSubscribedMutex.RUnlock()
argsForCall := fake.trackSubscribedArgsForCall[i] argsForCall := fake.trackSubscribedArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5
} }
func (fake *FakeTelemetryService) TrackUnmuted(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo) { func (fake *FakeTelemetryService) TrackUnmuted(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo) {
+1 -1
View File
@@ -38,7 +38,7 @@ type TelemetryService interface {
// TrackSubscribeRequested - a participant requested to subscribe to a track // TrackSubscribeRequested - a participant requested to subscribe to a track
TrackSubscribeRequested(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo) TrackSubscribeRequested(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo)
// TrackSubscribed - a participant subscribed to a track successfully // TrackSubscribed - a participant subscribed to a track successfully
TrackSubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo) TrackSubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo, shouldSendEvent bool)
// TrackUnsubscribed - a participant unsubscribed from a track successfully // TrackUnsubscribed - a participant unsubscribed from a track successfully
TrackUnsubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, shouldSendEvent bool) TrackUnsubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, shouldSendEvent bool)
// TrackSubscribeFailed - failure to subscribe to a track // TrackSubscribeFailed - failure to subscribe to a track