Telemetry refactor (#172)

* telemetry refactor

* fix imports

* update protocol
This commit is contained in:
David Colburn
2021-11-08 20:00:34 -06:00
committed by GitHub
parent 8344466629
commit 289ebd32ff
25 changed files with 597 additions and 511 deletions
+11 -35
View File
@@ -3,28 +3,28 @@ package service
import (
"context"
"errors"
"time"
"github.com/livekit/protocol/logger"
livekit "github.com/livekit/protocol/proto"
"github.com/livekit/protocol/recording"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/webhook"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/livekit/livekit-server/pkg/telemetry"
)
type RecordingService struct {
bus utils.MessageBus
notifier webhook.Notifier
shutdown chan struct{}
bus utils.MessageBus
telemetry *telemetry.TelemetryService
shutdown chan struct{}
}
func NewRecordingService(mb utils.MessageBus, notifier webhook.Notifier) *RecordingService {
func NewRecordingService(mb utils.MessageBus, telemetry *telemetry.TelemetryService) *RecordingService {
return &RecordingService{
bus: mb,
notifier: notifier,
shutdown: make(chan struct{}, 1),
bus: mb,
telemetry: telemetry,
shutdown: make(chan struct{}, 1),
}
}
@@ -62,6 +62,7 @@ func (s *RecordingService) StartRecording(ctx context.Context, req *livekit.Star
return nil, err
}
s.telemetry.RecordingStarted(ctx, recordingId, req)
return &livekit.StartRecordingResponse{RecordingId: recordingId}, nil
}
@@ -141,35 +142,10 @@ func (s *RecordingService) resultsWorker() {
logger.Errorw("failed to read results", err)
continue
}
s.notify(res)
s.telemetry.RecordingEnded(res)
case <-s.shutdown:
_ = sub.Close()
return
}
}
}
func (s *RecordingService) notify(res *livekit.RecordingResult) {
// log results
values := []interface{}{"id", res.Id}
if res.Error != "" {
values = append(values, "error", res.Error)
} else {
values = append(values, "duration", time.Duration(res.Duration*1e9))
if res.DownloadUrl != "" {
values = append(values, "url", res.DownloadUrl)
}
}
logger.Debugw("received recording result", values...)
// webhook
if s.notifier != nil {
event := webhook.EventRecordingFinished
if err := s.notifier.Notify(&livekit.WebhookEvent{
Event: event,
RecordingResult: res,
}); err != nil {
logger.Warnw("could not notify webhook", err, "event", event)
}
}
}
+15 -47
View File
@@ -6,15 +6,14 @@ import (
"sync"
"time"
"github.com/gammazero/workerpool"
"github.com/livekit/protocol/logger"
livekit "github.com/livekit/protocol/proto"
"github.com/livekit/protocol/webhook"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/telemetry"
)
const (
@@ -29,15 +28,14 @@ type LocalRoomManager struct {
lock sync.RWMutex
router routing.Router
currentNode routing.LocalNode
notifier webhook.Notifier
rtcConfig *rtc.WebRTCConfig
config *config.Config
webhookPool *workerpool.WorkerPool
telemetry *telemetry.TelemetryService
rooms map[string]*rtc.Room
}
func NewLocalRoomManager(conf *config.Config, rs RoomStore, router routing.Router, currentNode routing.LocalNode,
notifier webhook.Notifier) (*LocalRoomManager, error) {
telemetry *telemetry.TelemetryService) (*LocalRoomManager, error) {
rtcConf, err := rtc.NewWebRTCConfig(conf, currentNode.Ip)
if err != nil {
@@ -50,9 +48,8 @@ func NewLocalRoomManager(conf *config.Config, rs RoomStore, router routing.Route
rtcConfig: rtcConf,
config: conf,
router: router,
notifier: notifier,
currentNode: currentNode,
webhookPool: workerpool.New(1),
telemetry: telemetry,
rooms: make(map[string]*rtc.Room),
}
@@ -228,7 +225,7 @@ func (r *LocalRoomManager) StartSession(ctx context.Context, roomName string, pi
Sink: responseSink,
AudioConfig: r.config.Audio,
ProtocolVersion: pv,
Stats: room.GetStatsReporter(),
Telemetry: r.telemetry,
ThrottleConfig: r.config.RTC.PLIThrottle,
EnabledCodecs: room.Room.EnabledCodecs,
Hidden: pi.Hidden,
@@ -255,6 +252,11 @@ func (r *LocalRoomManager) StartSession(ctx context.Context, roomName string, pi
return
}
r.telemetry.ParticipantJoined(ctx, room.Room, participant.ToProto())
participant.OnClose(func(p types.Participant) {
r.telemetry.ParticipantLeft(ctx, room.Room, p.ToProto())
})
go r.rtcSessionWorker(room, participant, requestSource)
}
@@ -275,22 +277,16 @@ func (r *LocalRoomManager) getOrCreateRoom(ctx context.Context, roomName string)
}
// construct ice servers
room = rtc.NewRoom(ri, *r.rtcConfig, &r.config.Audio)
room = rtc.NewRoom(ri, *r.rtcConfig, &r.config.Audio, r.telemetry)
r.telemetry.RoomStarted(ctx, room.Room)
room.OnClose(func() {
r.telemetry.RoomEnded(ctx, room.Room)
if err := r.DeleteRoom(ctx, roomName); err != nil {
logger.Errorw("could not delete room", err)
}
r.notifyEvent(&livekit.WebhookEvent{
Event: webhook.EventRoomFinished,
Room: room.Room,
})
// print stats
logger.Infow("room closed",
"incomingStats", room.GetIncomingStats().Copy(),
"outgoingStats", room.GetOutgoingStats().Copy(),
)
logger.Infow("room closed")
})
room.OnMetadataUpdate(func(metadata string) {
err := r.StoreRoom(ctx, room.Room)
@@ -313,11 +309,6 @@ func (r *LocalRoomManager) getOrCreateRoom(ctx context.Context, roomName string)
r.rooms[roomName] = room
r.lock.Unlock()
r.notifyEvent(&livekit.WebhookEvent{
Event: webhook.EventRoomStarted,
Room: room.Room,
})
return room, nil
}
@@ -331,20 +322,9 @@ func (r *LocalRoomManager) rtcSessionWorker(room *rtc.Room, participant types.Pa
"roomID", room.Room.Sid,
)
_ = participant.Close()
r.notifyEvent(&livekit.WebhookEvent{
Event: webhook.EventParticipantLeft,
Room: room.Room,
Participant: participant.ToProto(),
})
}()
defer rtc.Recover()
r.notifyEvent(&livekit.WebhookEvent{
Event: webhook.EventParticipantJoined,
Room: room.Room,
Participant: participant.ToProto(),
})
for {
select {
case <-time.After(time.Millisecond * 50):
@@ -569,18 +549,6 @@ func (r *LocalRoomManager) iceServersForRoom(ri *livekit.Room) []*livekit.ICESer
return iceServers
}
func (r *LocalRoomManager) notifyEvent(event *livekit.WebhookEvent) {
if r.notifier == nil {
return
}
r.webhookPool.Submit(func() {
if err := r.notifier.Notify(event); err != nil {
logger.Warnw("could not notify webhook", err, "event", event.Event)
}
})
}
func iceServerForStunServers(servers []string) *livekit.ICEServer {
iceServer := &livekit.ICEServer{}
for _, stunServer := range servers {
+6 -6
View File
@@ -16,7 +16,7 @@ import (
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/utils/stats"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
)
type RTCService struct {
@@ -93,7 +93,7 @@ func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit,
func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// reject non websocket requests
if !websocket.IsWebSocketUpgrade(r) {
stats.PromServiceOperationCounter.WithLabelValues("signal_ws", "error", "reject").Add(1)
prometheus.ServiceOperationCounter.WithLabelValues("signal_ws", "error", "reject").Add(1)
w.WriteHeader(404)
return
}
@@ -107,7 +107,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// create room if it doesn't exist, also assigns an RTC node for the room
rm, err := s.roomAllocator.CreateRoom(r.Context(), &livekit.CreateRoomRequest{Name: roomName})
if err != nil {
stats.PromServiceOperationCounter.WithLabelValues("signal_ws", "error", "create_room").Add(1)
prometheus.ServiceOperationCounter.WithLabelValues("signal_ws", "error", "create_room").Add(1)
handleError(w, http.StatusInternalServerError, err.Error())
return
}
@@ -115,7 +115,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// this needs to be started first *before* using router functions on this node
connId, reqSink, resSource, err := s.router.StartParticipantSignal(r.Context(), roomName, pi)
if err != nil {
stats.PromServiceOperationCounter.WithLabelValues("signal_ws", "error", "start_signal").Add(1)
prometheus.ServiceOperationCounter.WithLabelValues("signal_ws", "error", "start_signal").Add(1)
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
return
}
@@ -131,7 +131,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// upgrade only once the basics are good to go
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
stats.PromServiceOperationCounter.WithLabelValues("signal_ws", "error", "upgrade").Add(1)
prometheus.ServiceOperationCounter.WithLabelValues("signal_ws", "error", "upgrade").Add(1)
logger.Warnw("could not upgrade to WS", err)
handleError(w, http.StatusInternalServerError, err.Error())
return
@@ -141,7 +141,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sigConn.useJSON = false
}
stats.PromServiceOperationCounter.WithLabelValues("signal_ws", "success", "").Add(1)
prometheus.ServiceOperationCounter.WithLabelValues("signal_ws", "success", "").Add(1)
logger.Infow("new client WS connected",
"connID", connId,
"roomID", rm.Sid,
+2
View File
@@ -20,6 +20,7 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/telemetry"
)
func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*LivekitServer, error) {
@@ -30,6 +31,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
createKeyProvider,
createWebhookNotifier,
routing.CreateRouter,
telemetry.NewTelemetryService,
NewRecordingService,
NewRoomAllocator,
NewRoomService,
+5 -4
View File
@@ -1,8 +1,7 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
//+build !wireinject
package service
@@ -12,6 +11,7 @@ import (
"github.com/go-redis/redis/v8"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
@@ -46,9 +46,10 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
recordingService := NewRecordingService(messageBus, notifier)
telemetryService := telemetry.NewTelemetryService(notifier)
recordingService := NewRecordingService(messageBus, telemetryService)
rtcService := NewRTCService(conf, roomAllocator, router, currentNode)
localRoomManager, err := NewLocalRoomManager(conf, roomStore, router, currentNode, notifier)
localRoomManager, err := NewLocalRoomManager(conf, roomStore, router, currentNode, telemetryService)
if err != nil {
return nil, err
}