start telemetry participant worker to collect signal stats (#2538)
* start telemetry participant worker to collect signal stats * format * resolve room * tidy
This commit is contained in:
+16
-26
@@ -247,12 +247,10 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
pi.ID = livekit.ParticipantID(initialResponse.GetJoin().GetParticipant().GetSid())
|
pi.ID = livekit.ParticipantID(initialResponse.GetJoin().GetParticipant().GetSid())
|
||||||
}
|
}
|
||||||
|
|
||||||
var signalStats *telemetry.BytesTrackStats
|
signalStats := telemetry.NewBytesSignalStats(r.Context(), s.telemetry)
|
||||||
if pi.ID != "" {
|
if join := initialResponse.GetJoin(); join != nil {
|
||||||
signalStats = telemetry.NewBytesTrackStats(
|
signalStats.ResolveRoom(join.GetRoom())
|
||||||
telemetry.BytesTrackIDForParticipantID(telemetry.BytesTrackTypeSignal, pi.ID),
|
signalStats.ResolveParticipant(join.GetParticipant())
|
||||||
pi.ID,
|
|
||||||
s.telemetry)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pLogger := rtc.LoggerWithParticipant(
|
pLogger := rtc.LoggerWithParticipant(
|
||||||
@@ -274,9 +272,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
cr.RequestSink.Close()
|
cr.RequestSink.Close()
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
if signalStats != nil {
|
signalStats.Stop()
|
||||||
signalStats.Stop()
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// upgrade only once the basics are good to go
|
// upgrade only once the basics are good to go
|
||||||
@@ -303,9 +299,8 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
pLogger.Warnw("could not write initial response", err)
|
pLogger.Warnw("could not write initial response", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if signalStats != nil {
|
signalStats.AddBytes(uint64(count), true)
|
||||||
signalStats.AddBytes(uint64(count), true)
|
|
||||||
}
|
|
||||||
pLogger.Debugw("new client WS connected",
|
pLogger.Debugw("new client WS connected",
|
||||||
"connID", cr.ConnectionID,
|
"connID", cr.ConnectionID,
|
||||||
"reconnect", pi.Reconnect,
|
"reconnect", pi.Reconnect,
|
||||||
@@ -349,20 +344,17 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
pLogger.Debugw("sending offer", "offer", m)
|
pLogger.Debugw("sending offer", "offer", m)
|
||||||
case *livekit.SignalResponse_Answer:
|
case *livekit.SignalResponse_Answer:
|
||||||
pLogger.Debugw("sending answer", "answer", m)
|
pLogger.Debugw("sending answer", "answer", m)
|
||||||
}
|
case *livekit.SignalResponse_Join:
|
||||||
|
signalStats.ResolveRoom(m.Join.GetRoom())
|
||||||
if pi.ID == "" && res.GetJoin() != nil {
|
signalStats.ResolveParticipant(m.Join.GetParticipant())
|
||||||
pi.ID = livekit.ParticipantID(res.GetJoin().GetParticipant().GetSid())
|
case *livekit.SignalResponse_RoomUpdate:
|
||||||
signalStats = telemetry.NewBytesTrackStats(
|
signalStats.ResolveRoom(m.RoomUpdate.GetRoom())
|
||||||
telemetry.BytesTrackIDForParticipantID(telemetry.BytesTrackTypeSignal, pi.ID),
|
|
||||||
pi.ID,
|
|
||||||
s.telemetry)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if count, err := sigConn.WriteResponse(res); err != nil {
|
if count, err := sigConn.WriteResponse(res); err != nil {
|
||||||
pLogger.Warnw("error writing to websocket", err)
|
pLogger.Warnw("error writing to websocket", err)
|
||||||
return
|
return
|
||||||
} else if signalStats != nil {
|
} else {
|
||||||
signalStats.AddBytes(uint64(count), true)
|
signalStats.AddBytes(uint64(count), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,9 +382,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if signalStats != nil {
|
signalStats.AddBytes(uint64(count), false)
|
||||||
signalStats.AddBytes(uint64(count), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch m := req.Message.(type) {
|
switch m := req.Message.(type) {
|
||||||
case *livekit.SignalRequest_Ping:
|
case *livekit.SignalRequest_Ping:
|
||||||
@@ -405,7 +395,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
Pong: time.Now().UnixMilli(),
|
Pong: time.Now().UnixMilli(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if perr == nil && signalStats != nil {
|
if perr == nil {
|
||||||
signalStats.AddBytes(uint64(count), true)
|
signalStats.AddBytes(uint64(count), true)
|
||||||
}
|
}
|
||||||
case *livekit.SignalRequest_PingReq:
|
case *livekit.SignalRequest_PingReq:
|
||||||
@@ -417,7 +407,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if perr == nil && signalStats != nil {
|
if perr == nil {
|
||||||
signalStats.AddBytes(uint64(count), true)
|
signalStats.AddBytes(uint64(count), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-16
@@ -80,16 +80,17 @@ func (t *telemetryService) ParticipantJoined(
|
|||||||
shouldSendEvent bool,
|
shouldSendEvent bool,
|
||||||
) {
|
) {
|
||||||
t.enqueue(func() {
|
t.enqueue(func() {
|
||||||
prometheus.IncrementParticipantRtcConnected(1)
|
_, found := t.getOrCreateWorker(
|
||||||
prometheus.AddParticipant()
|
|
||||||
|
|
||||||
t.createWorker(
|
|
||||||
ctx,
|
ctx,
|
||||||
livekit.RoomID(room.Sid),
|
livekit.RoomID(room.Sid),
|
||||||
livekit.RoomName(room.Name),
|
livekit.RoomName(room.Name),
|
||||||
livekit.ParticipantID(participant.Sid),
|
livekit.ParticipantID(participant.Sid),
|
||||||
livekit.ParticipantIdentity(participant.Identity),
|
livekit.ParticipantIdentity(participant.Identity),
|
||||||
)
|
)
|
||||||
|
if !found {
|
||||||
|
prometheus.IncrementParticipantRtcConnected(1)
|
||||||
|
prometheus.AddParticipant()
|
||||||
|
}
|
||||||
|
|
||||||
if shouldSendEvent {
|
if shouldSendEvent {
|
||||||
ev := newParticipantEvent(livekit.AnalyticsEventType_PARTICIPANT_JOINED, room, participant)
|
ev := newParticipantEvent(livekit.AnalyticsEventType_PARTICIPANT_JOINED, room, participant)
|
||||||
@@ -117,18 +118,14 @@ func (t *telemetryService) ParticipantActive(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
worker, ok := t.getWorker(livekit.ParticipantID(participant.Sid))
|
worker, found := t.getOrCreateWorker(
|
||||||
if !ok {
|
ctx,
|
||||||
// in case of session migration, we may not have seen a Join event take place.
|
livekit.RoomID(room.Sid),
|
||||||
// we'd need to create the worker here before being able to process events
|
livekit.RoomName(room.Name),
|
||||||
worker = t.createWorker(
|
livekit.ParticipantID(participant.Sid),
|
||||||
ctx,
|
livekit.ParticipantIdentity(participant.Identity),
|
||||||
livekit.RoomID(room.Sid),
|
)
|
||||||
livekit.RoomName(room.Name),
|
if !found {
|
||||||
livekit.ParticipantID(participant.Sid),
|
|
||||||
livekit.ParticipantIdentity(participant.Identity),
|
|
||||||
)
|
|
||||||
|
|
||||||
// need to also account for participant count
|
// need to also account for participant count
|
||||||
prometheus.AddParticipant()
|
prometheus.AddParticipant()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,16 +15,17 @@
|
|||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/frostbyte73/core"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/frostbyte73/core"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/utils"
|
"github.com/livekit/protocol/utils"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BytesTrackType string
|
type BytesTrackType string
|
||||||
@@ -139,6 +140,61 @@ func (s *BytesTrackStats) reporter() {
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
type BytesSignalStats struct {
|
||||||
|
BytesTrackStats
|
||||||
|
ctx context.Context
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
ri *livekit.Room
|
||||||
|
pi *livekit.ParticipantInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBytesSignalStats(ctx context.Context, telemetry TelemetryService) *BytesSignalStats {
|
||||||
|
return &BytesSignalStats{
|
||||||
|
BytesTrackStats: BytesTrackStats{
|
||||||
|
telemetry: telemetry,
|
||||||
|
},
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BytesSignalStats) ResolveRoom(ri *livekit.Room) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.ri == nil && ri.GetSid() != "" {
|
||||||
|
s.ri = ri
|
||||||
|
s.maybeStart()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BytesSignalStats) ResolveParticipant(pi *livekit.ParticipantInfo) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.pi == nil {
|
||||||
|
s.pi = pi
|
||||||
|
s.maybeStart()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BytesSignalStats) maybeStart() {
|
||||||
|
if s.ri == nil || s.pi == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.pID = livekit.ParticipantID(s.pi.Sid)
|
||||||
|
s.trackID = BytesTrackIDForParticipantID(BytesTrackTypeSignal, s.pID)
|
||||||
|
|
||||||
|
s.telemetry.ParticipantJoined(s.ctx, s.ri, s.pi, nil, nil, false)
|
||||||
|
go s.reporter()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BytesSignalStats) reporter() {
|
||||||
|
s.BytesTrackStats.reporter()
|
||||||
|
s.telemetry.ParticipantLeft(s.ctx, s.ri, s.pi, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
func BytesTrackIDForParticipantID(typ BytesTrackType, participantID livekit.ParticipantID) livekit.TrackID {
|
func BytesTrackIDForParticipantID(typ BytesTrackType, participantID livekit.ParticipantID) livekit.TrackID {
|
||||||
return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID))
|
return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,13 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/livekit-server/pkg/utils"
|
"github.com/livekit/livekit-server/pkg/utils"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/webhook"
|
"github.com/livekit/protocol/webhook"
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . TelemetryService
|
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . TelemetryService
|
||||||
@@ -153,12 +154,19 @@ func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worke
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *telemetryService) createWorker(ctx context.Context,
|
func (t *telemetryService) getOrCreateWorker(ctx context.Context,
|
||||||
roomID livekit.RoomID,
|
roomID livekit.RoomID,
|
||||||
roomName livekit.RoomName,
|
roomName livekit.RoomName,
|
||||||
participantID livekit.ParticipantID,
|
participantID livekit.ParticipantID,
|
||||||
participantIdentity livekit.ParticipantIdentity,
|
participantIdentity livekit.ParticipantIdentity,
|
||||||
) *StatsWorker {
|
) (*StatsWorker, bool) {
|
||||||
|
t.lock.Lock()
|
||||||
|
defer t.lock.Unlock()
|
||||||
|
|
||||||
|
if worker, ok := t.workers[participantID]; ok {
|
||||||
|
return worker, true
|
||||||
|
}
|
||||||
|
|
||||||
worker := newStatsWorker(
|
worker := newStatsWorker(
|
||||||
ctx,
|
ctx,
|
||||||
t,
|
t,
|
||||||
@@ -168,11 +176,10 @@ func (t *telemetryService) createWorker(ctx context.Context,
|
|||||||
participantIdentity,
|
participantIdentity,
|
||||||
)
|
)
|
||||||
|
|
||||||
t.lock.Lock()
|
|
||||||
t.workers[participantID] = worker
|
t.workers[participantID] = worker
|
||||||
t.workersShadow = maps.Values(t.workers)
|
t.workersShadow = maps.Values(t.workers)
|
||||||
t.lock.Unlock()
|
|
||||||
return worker
|
return worker, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *telemetryService) cleanupWorkers() {
|
func (t *telemetryService) cleanupWorkers() {
|
||||||
|
|||||||
Reference in New Issue
Block a user