Allow specifying extra webhooks with egress requests (#3597)
This commit is contained in:
+57
-14
@@ -28,7 +28,7 @@ import (
|
||||
"github.com/livekit/protocol/webhook"
|
||||
)
|
||||
|
||||
func (t *telemetryService) NotifyEvent(ctx context.Context, event *livekit.WebhookEvent) {
|
||||
func (t *telemetryService) NotifyEvent(ctx context.Context, event *livekit.WebhookEvent, opts ...webhook.NotifyOption) {
|
||||
if t.notifier == nil {
|
||||
return
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func (t *telemetryService) NotifyEvent(ctx context.Context, event *livekit.Webho
|
||||
event.CreatedAt = time.Now().Unix()
|
||||
event.Id = guid.New("EV_")
|
||||
|
||||
if err := t.notifier.QueueNotify(ctx, event); err != nil {
|
||||
if err := t.notifier.QueueNotify(ctx, event, opts...); err != nil {
|
||||
logger.Warnw("failed to notify webhook", err, "event", event.Event)
|
||||
}
|
||||
}
|
||||
@@ -428,12 +428,19 @@ func (t *telemetryService) TrackSubscribeRTPStats(
|
||||
})
|
||||
}
|
||||
|
||||
func (t *telemetryService) NotifyEgressEvent(ctx context.Context, event string, info *livekit.EgressInfo) {
|
||||
opts := getEgressNotifyOptions(info)
|
||||
|
||||
t.NotifyEvent(ctx, &livekit.WebhookEvent{
|
||||
Event: event,
|
||||
EgressInfo: info,
|
||||
}, opts...)
|
||||
}
|
||||
|
||||
func (t *telemetryService) EgressStarted(ctx context.Context, info *livekit.EgressInfo) {
|
||||
|
||||
t.enqueue(func() {
|
||||
t.NotifyEvent(ctx, &livekit.WebhookEvent{
|
||||
Event: webhook.EventEgressStarted,
|
||||
EgressInfo: info,
|
||||
})
|
||||
t.NotifyEgressEvent(ctx, webhook.EventEgressStarted, info)
|
||||
|
||||
t.SendEvent(ctx, newEgressEvent(livekit.AnalyticsEventType_EGRESS_STARTED, info))
|
||||
})
|
||||
@@ -441,20 +448,15 @@ func (t *telemetryService) EgressStarted(ctx context.Context, info *livekit.Egre
|
||||
|
||||
func (t *telemetryService) EgressUpdated(ctx context.Context, info *livekit.EgressInfo) {
|
||||
t.enqueue(func() {
|
||||
t.NotifyEvent(ctx, &livekit.WebhookEvent{
|
||||
Event: webhook.EventEgressUpdated,
|
||||
EgressInfo: info,
|
||||
})
|
||||
t.NotifyEgressEvent(ctx, webhook.EventEgressUpdated, info)
|
||||
|
||||
t.SendEvent(ctx, newEgressEvent(livekit.AnalyticsEventType_EGRESS_UPDATED, info))
|
||||
})
|
||||
}
|
||||
|
||||
func (t *telemetryService) EgressEnded(ctx context.Context, info *livekit.EgressInfo) {
|
||||
t.enqueue(func() {
|
||||
t.NotifyEvent(ctx, &livekit.WebhookEvent{
|
||||
Event: webhook.EventEgressEnded,
|
||||
EgressInfo: info,
|
||||
})
|
||||
t.NotifyEgressEvent(ctx, webhook.EventEgressEnded, info)
|
||||
|
||||
t.SendEvent(ctx, newEgressEvent(livekit.AnalyticsEventType_EGRESS_ENDED, info))
|
||||
})
|
||||
@@ -596,3 +598,44 @@ func newIngressEvent(event livekit.AnalyticsEventType, ingress *livekit.IngressI
|
||||
Ingress: ingress,
|
||||
}
|
||||
}
|
||||
|
||||
func getEgressNotifyOptions(egressInfo *livekit.EgressInfo) []webhook.NotifyOption {
|
||||
if egressInfo == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if egressInfo.Request == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var whs []*livekit.WebhookConfig
|
||||
|
||||
switch req := egressInfo.Request.(type) {
|
||||
case *livekit.EgressInfo_RoomComposite:
|
||||
if req.RoomComposite != nil {
|
||||
whs = req.RoomComposite.Webhooks
|
||||
}
|
||||
case *livekit.EgressInfo_Web:
|
||||
if req.Web != nil {
|
||||
whs = req.Web.Webhooks
|
||||
}
|
||||
case *livekit.EgressInfo_Participant:
|
||||
if req.Participant != nil {
|
||||
whs = req.Participant.Webhooks
|
||||
}
|
||||
case *livekit.EgressInfo_TrackComposite:
|
||||
if req.TrackComposite != nil {
|
||||
whs = req.TrackComposite.Webhooks
|
||||
}
|
||||
case *livekit.EgressInfo_Track:
|
||||
if req.Track != nil {
|
||||
whs = req.Track.Webhooks
|
||||
}
|
||||
}
|
||||
|
||||
if len(whs) > 0 {
|
||||
return []webhook.NotifyOption{webhook.WithExtraWebhooks(whs)}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -75,11 +75,12 @@ type FakeTelemetryService struct {
|
||||
arg1 context.Context
|
||||
arg2 *livekit.AnalyticsNodeRooms
|
||||
}
|
||||
NotifyEventStub func(context.Context, *livekit.WebhookEvent)
|
||||
notifyEventMutex sync.RWMutex
|
||||
notifyEventArgsForCall []struct {
|
||||
NotifyEgressEventStub func(context.Context, string, *livekit.EgressInfo)
|
||||
notifyEgressEventMutex sync.RWMutex
|
||||
notifyEgressEventArgsForCall []struct {
|
||||
arg1 context.Context
|
||||
arg2 *livekit.WebhookEvent
|
||||
arg2 string
|
||||
arg3 *livekit.EgressInfo
|
||||
}
|
||||
ParticipantActiveStub func(context.Context, *livekit.Room, *livekit.ParticipantInfo, *livekit.AnalyticsClientMeta, bool)
|
||||
participantActiveMutex sync.RWMutex
|
||||
@@ -630,37 +631,38 @@ func (fake *FakeTelemetryService) LocalRoomStateArgsForCall(i int) (context.Cont
|
||||
return argsForCall.arg1, argsForCall.arg2
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) NotifyEvent(arg1 context.Context, arg2 *livekit.WebhookEvent) {
|
||||
fake.notifyEventMutex.Lock()
|
||||
fake.notifyEventArgsForCall = append(fake.notifyEventArgsForCall, struct {
|
||||
func (fake *FakeTelemetryService) NotifyEgressEvent(arg1 context.Context, arg2 string, arg3 *livekit.EgressInfo) {
|
||||
fake.notifyEgressEventMutex.Lock()
|
||||
fake.notifyEgressEventArgsForCall = append(fake.notifyEgressEventArgsForCall, struct {
|
||||
arg1 context.Context
|
||||
arg2 *livekit.WebhookEvent
|
||||
}{arg1, arg2})
|
||||
stub := fake.NotifyEventStub
|
||||
fake.recordInvocation("NotifyEvent", []interface{}{arg1, arg2})
|
||||
fake.notifyEventMutex.Unlock()
|
||||
arg2 string
|
||||
arg3 *livekit.EgressInfo
|
||||
}{arg1, arg2, arg3})
|
||||
stub := fake.NotifyEgressEventStub
|
||||
fake.recordInvocation("NotifyEgressEvent", []interface{}{arg1, arg2, arg3})
|
||||
fake.notifyEgressEventMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.NotifyEventStub(arg1, arg2)
|
||||
fake.NotifyEgressEventStub(arg1, arg2, arg3)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) NotifyEventCallCount() int {
|
||||
fake.notifyEventMutex.RLock()
|
||||
defer fake.notifyEventMutex.RUnlock()
|
||||
return len(fake.notifyEventArgsForCall)
|
||||
func (fake *FakeTelemetryService) NotifyEgressEventCallCount() int {
|
||||
fake.notifyEgressEventMutex.RLock()
|
||||
defer fake.notifyEgressEventMutex.RUnlock()
|
||||
return len(fake.notifyEgressEventArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) NotifyEventCalls(stub func(context.Context, *livekit.WebhookEvent)) {
|
||||
fake.notifyEventMutex.Lock()
|
||||
defer fake.notifyEventMutex.Unlock()
|
||||
fake.NotifyEventStub = stub
|
||||
func (fake *FakeTelemetryService) NotifyEgressEventCalls(stub func(context.Context, string, *livekit.EgressInfo)) {
|
||||
fake.notifyEgressEventMutex.Lock()
|
||||
defer fake.notifyEgressEventMutex.Unlock()
|
||||
fake.NotifyEgressEventStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) NotifyEventArgsForCall(i int) (context.Context, *livekit.WebhookEvent) {
|
||||
fake.notifyEventMutex.RLock()
|
||||
defer fake.notifyEventMutex.RUnlock()
|
||||
argsForCall := fake.notifyEventArgsForCall[i]
|
||||
return argsForCall.arg1, argsForCall.arg2
|
||||
func (fake *FakeTelemetryService) NotifyEgressEventArgsForCall(i int) (context.Context, string, *livekit.EgressInfo) {
|
||||
fake.notifyEgressEventMutex.RLock()
|
||||
defer fake.notifyEgressEventMutex.RUnlock()
|
||||
argsForCall := fake.notifyEgressEventArgsForCall[i]
|
||||
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
|
||||
}
|
||||
|
||||
func (fake *FakeTelemetryService) ParticipantActive(arg1 context.Context, arg2 *livekit.Room, arg3 *livekit.ParticipantInfo, arg4 *livekit.AnalyticsClientMeta, arg5 bool) {
|
||||
@@ -1559,8 +1561,8 @@ func (fake *FakeTelemetryService) Invocations() map[string][][]interface{} {
|
||||
defer fake.ingressUpdatedMutex.RUnlock()
|
||||
fake.localRoomStateMutex.RLock()
|
||||
defer fake.localRoomStateMutex.RUnlock()
|
||||
fake.notifyEventMutex.RLock()
|
||||
defer fake.notifyEventMutex.RUnlock()
|
||||
fake.notifyEgressEventMutex.RLock()
|
||||
defer fake.notifyEgressEventMutex.RUnlock()
|
||||
fake.participantActiveMutex.RLock()
|
||||
defer fake.participantActiveMutex.RUnlock()
|
||||
fake.participantJoinedMutex.RLock()
|
||||
|
||||
@@ -83,7 +83,7 @@ type TelemetryService interface {
|
||||
|
||||
// helpers
|
||||
AnalyticsService
|
||||
NotifyEvent(ctx context.Context, event *livekit.WebhookEvent)
|
||||
NotifyEgressEvent(ctx context.Context, event string, info *livekit.EgressInfo)
|
||||
FlushStats()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user