Record subscribe stream start time in prometheus. (#4704)

* Record subscribe stream start time in prometheus.

Adjust for mutes, i. e. take the last unmute time as the start point and
calculate time till the first byte is sent.

* close the tiny window of race

* Prevent long tail sample when publisher glitches.

Thanks to @milos-lk for this.

Publisher restarting would have reset the layer and would have caused a
sample with very high stream start time. We only need to capture when we
do a dummy start or when the state is seeded to a different node upon
migration.

* reduce a diff

* test

* changed the wrong thing, thank you Devin
This commit is contained in:
Raja Subramanian
2026-07-27 20:37:44 +05:30
committed by GitHub
parent f1e2eee2fe
commit 0aa296e126
11 changed files with 152 additions and 34 deletions
+16 -6
View File
@@ -3,6 +3,7 @@ package sfufakes
import (
"sync"
"time"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/protocol/livekit"
@@ -39,9 +40,10 @@ type FakeDownTrackListener struct {
onStatsUpdateArgsForCall []struct {
arg1 *livekit.AnalyticsStat
}
OnStreamStartedStub func()
OnStreamStartedStub func(time.Duration)
onStreamStartedMutex sync.RWMutex
onStreamStartedArgsForCall []struct {
arg1 time.Duration
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
@@ -231,15 +233,16 @@ func (fake *FakeDownTrackListener) OnStatsUpdateArgsForCall(i int) *livekit.Anal
return argsForCall.arg1
}
func (fake *FakeDownTrackListener) OnStreamStarted() {
func (fake *FakeDownTrackListener) OnStreamStarted(arg1 time.Duration) {
fake.onStreamStartedMutex.Lock()
fake.onStreamStartedArgsForCall = append(fake.onStreamStartedArgsForCall, struct {
}{})
arg1 time.Duration
}{arg1})
stub := fake.OnStreamStartedStub
fake.recordInvocation("OnStreamStarted", []interface{}{})
fake.recordInvocation("OnStreamStarted", []interface{}{arg1})
fake.onStreamStartedMutex.Unlock()
if stub != nil {
fake.OnStreamStartedStub()
fake.OnStreamStartedStub(arg1)
}
}
@@ -249,12 +252,19 @@ func (fake *FakeDownTrackListener) OnStreamStartedCallCount() int {
return len(fake.onStreamStartedArgsForCall)
}
func (fake *FakeDownTrackListener) OnStreamStartedCalls(stub func()) {
func (fake *FakeDownTrackListener) OnStreamStartedCalls(stub func(time.Duration)) {
fake.onStreamStartedMutex.Lock()
defer fake.onStreamStartedMutex.Unlock()
fake.OnStreamStartedStub = stub
}
func (fake *FakeDownTrackListener) OnStreamStartedArgsForCall(i int) time.Duration {
fake.onStreamStartedMutex.RLock()
defer fake.onStreamStartedMutex.RUnlock()
argsForCall := fake.onStreamStartedArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeDownTrackListener) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()