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
+24 -4
View File
@@ -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() {