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:
+24
-4
@@ -299,6 +299,7 @@ type DownTrack struct {
|
||||
connected atomic.Bool
|
||||
bindAndConnectedOnce atomic.Bool
|
||||
writable atomic.Bool
|
||||
writableAt atomic.Time
|
||||
writeStopped atomic.Bool
|
||||
isReceiverReady bool
|
||||
|
||||
@@ -335,7 +336,8 @@ type DownTrack struct {
|
||||
keyFrameRequesterCh chan struct{}
|
||||
keyFrameRequesterChClosed bool
|
||||
|
||||
createdAt int64
|
||||
createdAt int64
|
||||
lastUnmutedAt atomic.Time
|
||||
}
|
||||
|
||||
// NewDownTrack returns a DownTrack.
|
||||
@@ -364,6 +366,8 @@ func NewDownTrack(params DownTrackParams) (*DownTrack, error) {
|
||||
createdAt: time.Now().UnixNano(),
|
||||
receiver: params.Receiver,
|
||||
}
|
||||
d.lastUnmutedAt.Store(time.Now())
|
||||
|
||||
d.codec.Store(codec)
|
||||
d.bindState.Store(bindStateUnbound)
|
||||
d.params.Logger = params.Logger.WithValues(
|
||||
@@ -1157,7 +1161,14 @@ func (d *DownTrack) WriteRTP(extPkt *buffer.ExtPacket, layer int32) int32 {
|
||||
}
|
||||
|
||||
if tp.isStarting {
|
||||
d.params.Listener.OnStreamStarted()
|
||||
writableAt := d.writableAt.Load()
|
||||
lastUnmutedAt := d.lastUnmutedAt.Load()
|
||||
if !writableAt.IsZero() && !lastUnmutedAt.IsZero() {
|
||||
if writableAt.After(lastUnmutedAt) {
|
||||
lastUnmutedAt = writableAt
|
||||
}
|
||||
d.params.Listener.OnStreamStarted(time.Since(lastUnmutedAt))
|
||||
}
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -1302,6 +1313,11 @@ func (d *DownTrack) handleMute(muted bool, changed bool) {
|
||||
}
|
||||
|
||||
d.connectionStats.UpdateMute(d.forwarder.IsAnyMuted())
|
||||
if muted {
|
||||
d.lastUnmutedAt.Store(time.Time{})
|
||||
} else {
|
||||
d.lastUnmutedAt.Store(time.Now())
|
||||
}
|
||||
|
||||
//
|
||||
// Subscriber mute changes trigger a max layer notification.
|
||||
@@ -2483,8 +2499,12 @@ func (d *DownTrack) onBindAndConnectedChange() {
|
||||
if d.writeStopped.Load() {
|
||||
return
|
||||
}
|
||||
d.writable.Store(d.connected.Load() && d.bindState.Load() == bindStateBound)
|
||||
if d.connected.Load() && d.bindState.Load() == bindStateBound && !d.bindAndConnectedOnce.Swap(true) {
|
||||
writable := d.connected.Load() && d.bindState.Load() == bindStateBound
|
||||
if writable {
|
||||
d.writableAt.Store(time.Now())
|
||||
}
|
||||
d.writable.Store(writable)
|
||||
if writable && !d.bindAndConnectedOnce.Swap(true) {
|
||||
go d.params.Listener.OnBindAndConnected()
|
||||
|
||||
if d.activePaddingOnMuteUpTrack.Load() {
|
||||
|
||||
@@ -240,6 +240,7 @@ type Forwarder struct {
|
||||
|
||||
started bool
|
||||
preStartTime time.Time
|
||||
startPending bool
|
||||
acquireDeadline int64 // mono nanos; initial-acquisition grace deadline, 0 = inactive
|
||||
extFirstTS uint64
|
||||
lastSSRC uint32
|
||||
@@ -508,6 +509,7 @@ func (f *Forwarder) SeedState(state *livekit.RTPForwarderState) {
|
||||
f.referenceLayerSpatial = state.ReferenceLayerSpatial
|
||||
if state.PreStartTime != 0 {
|
||||
f.preStartTime = time.Unix(0, state.PreStartTime)
|
||||
f.startPending = true
|
||||
}
|
||||
f.extFirstTS = state.ExtFirstTimestamp
|
||||
f.dummyStartTSOffset = state.DummyStartTimestampOffset
|
||||
@@ -1858,7 +1860,10 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) (
|
||||
"referenceLayerSpatial", f.referenceLayerSpatial,
|
||||
)
|
||||
|
||||
starting = true
|
||||
if f.startPending {
|
||||
f.startPending = false
|
||||
starting = true
|
||||
}
|
||||
}
|
||||
|
||||
logTransition := func(message string, extExpectedTS, extRefTS, extLastTS uint64, diffSeconds float64) {
|
||||
@@ -2230,6 +2235,7 @@ func (f *Forwarder) maybeStart() {
|
||||
|
||||
f.started = true
|
||||
f.preStartTime = time.Now()
|
||||
f.startPending = true
|
||||
|
||||
sequenceNumber := uint16(rand.Intn(1<<14)) + uint16(1<<15) // a random number in third quartile of sequence number space
|
||||
timestamp := uint32(rand.Intn(1<<30)) + uint32(1<<31) // a random number in third quartile of timestamp space
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
package sfu
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/webrtc/v4"
|
||||
|
||||
@@ -181,7 +183,7 @@ type DownTrackListener interface {
|
||||
OnRttUpdate(rtt uint32)
|
||||
OnCodecNegotiated(webrtc.RTPCodecCapability)
|
||||
OnDownTrackClose(isExpectedToResume bool)
|
||||
OnStreamStarted()
|
||||
OnStreamStarted(elapsed time.Duration)
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user