From b023c531c26387f9026faf19c3b47c626851cb26 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Mon, 30 Jan 2023 10:16:21 -0800 Subject: [PATCH] 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 --- pkg/rtc/subscriptionmanager.go | 34 ++++++++++++------- pkg/telemetry/events.go | 5 +++ pkg/telemetry/events_test.go | 6 ++-- .../telemetryfakes/fake_telemetry_service.go | 18 +++++----- pkg/telemetry/telemetryservice.go | 2 +- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/pkg/rtc/subscriptionmanager.go b/pkg/rtc/subscriptionmanager.go index c45f64c0..b0ad731e 100644 --- a/pkg/rtc/subscriptionmanager.go +++ b/pkg/rtc/subscriptionmanager.go @@ -482,6 +482,7 @@ func (m *SubscriptionManager) handleSubscribedTrackClose(s *trackSubscription, w s.logger.Debugw("subscribed track closed", "willBeResumed", willBeResumed, ) + wasBound := s.isBound() subTrack := s.getSubscribedTrack() if subTrack == nil { return @@ -509,15 +510,19 @@ func (m *SubscriptionManager) handleSubscribedTrackClose(s *trackSubscription, w subTrack.OnClose(nil) 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 // * it's not a migration - m.params.Telemetry.TrackUnsubscribed( - context.Background(), - m.params.Participant.ID(), - &livekit.TrackInfo{Sid: string(s.trackID), Type: subTrack.MediaTrack().Kind()}, - !willBeResumed && !m.params.Participant.IsClosed(), - ) + if wasBound { + m.params.Telemetry.TrackUnsubscribed( + context.Background(), + m.params.Participant.ID(), + &livekit.TrackInfo{Sid: string(s.trackID), Type: subTrack.MediaTrack().Kind()}, + !willBeResumed && !m.params.Participant.IsClosed(), + ) + } + if !willBeResumed { sender := subTrack.RTPSender() if sender != nil { @@ -689,6 +694,12 @@ func (s *trackSubscription) setBound() { s.bound = true } +func (s *trackSubscription) isBound() bool { + s.lock.RLock() + defer s.lock.RUnlock() + return s.bound +} + func (s *trackSubscription) startAttempt() { if s.numAttempts.Load() == 0 { t := time.Now() @@ -722,14 +733,13 @@ func (s *trackSubscription) maybeRecordSuccess(ts telemetry.TelemetryService, pI return } - if s.eventSent.Swap(true) { - return - } + eventSent := s.eventSent.Swap(true) - ts.TrackSubscribed(context.Background(), pID, mediaTrack.ToProto(), &livekit.ParticipantInfo{ + pi := &livekit.ParticipantInfo{ Identity: string(subTrack.PublisherIdentity()), Sid: string(subTrack.PublisherID()), - }) + } + ts.TrackSubscribed(context.Background(), pID, mediaTrack.ToProto(), pi, !eventSent) } func (s *trackSubscription) durationSinceStart() time.Duration { diff --git a/pkg/telemetry/events.go b/pkg/telemetry/events.go index 45d1107e..0017a146 100644 --- a/pkg/telemetry/events.go +++ b/pkg/telemetry/events.go @@ -260,10 +260,15 @@ func (t *telemetryService) TrackSubscribed( participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo, + shouldSendEvent bool, ) { t.enqueue(func() { prometheus.RecordTrackSubscribeSuccess(track.Type.String()) + if !shouldSendEvent { + return + } + room := t.getRoomDetails(participantID) ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBED, room, participantID, track) ev.Publisher = publisher diff --git a/pkg/telemetry/events_test.go b/pkg/telemetry/events_test.go index 55701ba1..85270118 100644 --- a/pkg/telemetry/events_test.go +++ b/pkg/telemetry/events_test.go @@ -207,10 +207,12 @@ func Test_OnTrackSubscribed_EventIsSent(t *testing.T) { require.Equal(t, room, event.Room) // 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) - 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) require.Equal(t, livekit.AnalyticsEventType_TRACK_SUBSCRIBED, eventTrackSubscribed.Type) require.Equal(t, partSID, eventTrackSubscribed.ParticipantId) diff --git a/pkg/telemetry/telemetryfakes/fake_telemetry_service.go b/pkg/telemetry/telemetryfakes/fake_telemetry_service.go index 2d2bc4f7..a5121bc1 100644 --- a/pkg/telemetry/telemetryfakes/fake_telemetry_service.go +++ b/pkg/telemetry/telemetryfakes/fake_telemetry_service.go @@ -152,13 +152,14 @@ type FakeTelemetryService struct { arg3 *livekit.TrackInfo 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 trackSubscribedArgsForCall []struct { arg1 context.Context arg2 livekit.ParticipantID arg3 *livekit.TrackInfo arg4 *livekit.ParticipantInfo + arg5 bool } TrackUnmutedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo) trackUnmutedMutex sync.RWMutex @@ -868,19 +869,20 @@ func (fake *FakeTelemetryService) TrackSubscribeRequestedArgsForCall(i int) (con 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.trackSubscribedArgsForCall = append(fake.trackSubscribedArgsForCall, struct { arg1 context.Context arg2 livekit.ParticipantID arg3 *livekit.TrackInfo arg4 *livekit.ParticipantInfo - }{arg1, arg2, arg3, arg4}) + arg5 bool + }{arg1, arg2, arg3, arg4, arg5}) stub := fake.TrackSubscribedStub - fake.recordInvocation("TrackSubscribed", []interface{}{arg1, arg2, arg3, arg4}) + fake.recordInvocation("TrackSubscribed", []interface{}{arg1, arg2, arg3, arg4, arg5}) fake.trackSubscribedMutex.Unlock() 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) } -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() defer fake.trackSubscribedMutex.Unlock() 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() defer fake.trackSubscribedMutex.RUnlock() 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) { diff --git a/pkg/telemetry/telemetryservice.go b/pkg/telemetry/telemetryservice.go index 3ba713e4..85b2d26d 100644 --- a/pkg/telemetry/telemetryservice.go +++ b/pkg/telemetry/telemetryservice.go @@ -38,7 +38,7 @@ type TelemetryService interface { // TrackSubscribeRequested - a participant requested to subscribe to a track TrackSubscribeRequested(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo) // 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(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, shouldSendEvent bool) // TrackSubscribeFailed - failure to subscribe to a track