Make TelemetryService testable (#276)
* Make TelemetryService testable Timer is extracted for better testability of telemetryservice. Divided TelemetryService to internal part that: - contains business logic - does not contain timer - and therefore testable and external part that: - does not contain business logic - contains timer to send analytics every 10 seconds for all participants. - does not need tests * Add Test_AnalyticsSentWhenParticipantLeaves * Fix test
This commit is contained in:
@@ -2,24 +2,23 @@ package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/workerpool"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/webhook"
|
||||
"github.com/pion/rtcp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
)
|
||||
|
||||
const updateFrequency = time.Second * 10
|
||||
|
||||
type TelemetryService interface {
|
||||
// stats
|
||||
NewStatsInterceptorFactory(participantID, identity string) *StatsInterceptorFactory
|
||||
AddUpTrack(participantID string, buff *buffer.Buffer)
|
||||
OnDownstreamPacket(participantID string, bytes int)
|
||||
HandleRTCP(streamType livekit.StreamType, participantID string, pkts []rtcp.Packet)
|
||||
Report(ctx context.Context, stats []*livekit.AnalyticsStat)
|
||||
|
||||
// events
|
||||
RoomStarted(ctx context.Context, room *livekit.Room)
|
||||
@@ -35,91 +34,80 @@ type TelemetryService interface {
|
||||
}
|
||||
|
||||
type telemetryService struct {
|
||||
notifier webhook.Notifier
|
||||
webhookPool *workerpool.WorkerPool
|
||||
|
||||
sync.RWMutex
|
||||
// one worker per participant
|
||||
workers map[string]*StatsWorker
|
||||
|
||||
analytics AnalyticsService
|
||||
internalService TelemetryServiceInternal
|
||||
}
|
||||
|
||||
func NewTelemetryService(notifier webhook.Notifier, analytics AnalyticsService) TelemetryService {
|
||||
return &telemetryService{
|
||||
notifier: notifier,
|
||||
webhookPool: workerpool.New(1),
|
||||
workers: make(map[string]*StatsWorker),
|
||||
analytics: analytics,
|
||||
t := &telemetryService{
|
||||
internalService: NewTelemetryServiceInternal(notifier, analytics),
|
||||
}
|
||||
|
||||
go t.run()
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *telemetryService) run() {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(updateFrequency):
|
||||
t.internalService.SendAnalytics()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *telemetryService) AddUpTrack(participantID string, buff *buffer.Buffer) {
|
||||
t.RLock()
|
||||
w := t.workers[participantID]
|
||||
t.RUnlock()
|
||||
if w != nil {
|
||||
w.AddBuffer(buff)
|
||||
}
|
||||
t.internalService.AddUpTrack(participantID, buff)
|
||||
}
|
||||
|
||||
func (t *telemetryService) OnDownstreamPacket(participantID string, bytes int) {
|
||||
t.RLock()
|
||||
w := t.workers[participantID]
|
||||
t.RUnlock()
|
||||
if w != nil {
|
||||
w.OnDownstreamPacket(bytes)
|
||||
}
|
||||
t.internalService.OnDownstreamPacket(participantID, bytes)
|
||||
}
|
||||
|
||||
func (t *telemetryService) HandleRTCP(streamType livekit.StreamType, participantID string, pkts []rtcp.Packet) {
|
||||
stats := &livekit.AnalyticsStat{}
|
||||
for _, pkt := range pkts {
|
||||
switch pkt := pkt.(type) {
|
||||
case *rtcp.TransportLayerNack:
|
||||
stats.NackCount++
|
||||
case *rtcp.PictureLossIndication:
|
||||
stats.PliCount++
|
||||
case *rtcp.FullIntraRequest:
|
||||
stats.FirCount++
|
||||
case *rtcp.ReceiverReport:
|
||||
for _, rr := range pkt.Reports {
|
||||
if delay := uint64(rr.Delay); delay > stats.Delay {
|
||||
stats.Delay = delay
|
||||
}
|
||||
if jitter := float64(rr.Jitter); jitter > stats.Jitter {
|
||||
stats.Jitter = jitter
|
||||
}
|
||||
stats.PacketLost += uint64(rr.TotalLost)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
direction := prometheus.Incoming
|
||||
if streamType == livekit.StreamType_DOWNSTREAM {
|
||||
direction = prometheus.Outgoing
|
||||
}
|
||||
|
||||
prometheus.IncrementRTCP(direction, stats.NackCount, stats.PliCount, stats.FirCount)
|
||||
|
||||
t.RLock()
|
||||
w := t.workers[participantID]
|
||||
t.RUnlock()
|
||||
if w != nil {
|
||||
w.OnRTCP(streamType, stats)
|
||||
}
|
||||
t.internalService.HandleRTCP(streamType, participantID, pkts)
|
||||
}
|
||||
|
||||
func (t *telemetryService) Report(ctx context.Context, stats []*livekit.AnalyticsStat) {
|
||||
for _, stat := range stats {
|
||||
direction := prometheus.Incoming
|
||||
if stat.Kind == livekit.StreamType_DOWNSTREAM {
|
||||
direction = prometheus.Outgoing
|
||||
}
|
||||
|
||||
prometheus.IncrementPackets(direction, stat.TotalPackets)
|
||||
prometheus.IncrementBytes(direction, stat.TotalBytes)
|
||||
}
|
||||
|
||||
t.analytics.SendStats(ctx, stats)
|
||||
func (t *telemetryService) RoomStarted(ctx context.Context, room *livekit.Room) {
|
||||
t.internalService.RoomStarted(ctx, room)
|
||||
}
|
||||
|
||||
func (t *telemetryService) RoomEnded(ctx context.Context, room *livekit.Room) {
|
||||
t.internalService.RoomEnded(ctx, room)
|
||||
}
|
||||
|
||||
func (t *telemetryService) ParticipantJoined(ctx context.Context, room *livekit.Room, participant *livekit.ParticipantInfo, clientInfo *livekit.ClientInfo) {
|
||||
t.internalService.ParticipantJoined(ctx, room, participant, clientInfo)
|
||||
}
|
||||
|
||||
func (t *telemetryService) ParticipantLeft(ctx context.Context, room *livekit.Room, participant *livekit.ParticipantInfo) {
|
||||
t.internalService.ParticipantLeft(ctx, room, participant)
|
||||
}
|
||||
|
||||
func (t *telemetryService) TrackPublished(ctx context.Context, participantID string, track *livekit.TrackInfo) {
|
||||
t.internalService.TrackPublished(ctx, participantID, track)
|
||||
}
|
||||
|
||||
func (t *telemetryService) TrackUnpublished(ctx context.Context, participantID string, track *livekit.TrackInfo, ssrc uint32) {
|
||||
t.internalService.TrackUnpublished(ctx, participantID, track, ssrc)
|
||||
}
|
||||
|
||||
func (t *telemetryService) TrackSubscribed(ctx context.Context, participantID string, track *livekit.TrackInfo) {
|
||||
t.internalService.TrackSubscribed(ctx, participantID, track)
|
||||
}
|
||||
|
||||
func (t *telemetryService) TrackUnsubscribed(ctx context.Context, participantID string, track *livekit.TrackInfo) {
|
||||
t.internalService.TrackUnsubscribed(ctx, participantID, track)
|
||||
}
|
||||
|
||||
func (t *telemetryService) RecordingStarted(ctx context.Context, ri *livekit.RecordingInfo) {
|
||||
t.internalService.RecordingStarted(ctx, ri)
|
||||
}
|
||||
|
||||
func (t *telemetryService) RecordingEnded(ctx context.Context, ri *livekit.RecordingInfo) {
|
||||
t.internalService.RecordingEnded(ctx, ri)
|
||||
}
|
||||
|
||||
func (t *telemetryService) NewStatsInterceptorFactory(participantID, identity string) *StatsInterceptorFactory {
|
||||
return t.internalService.NewStatsInterceptorFactory(participantID, identity)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user