mirror of
https://github.com/livekit/livekit.git
synced 2026-12-05 18:38:10 +00:00
* service: send queued webhooks before shutdown completes Start returned, and the process exited, while the room_finished webhooks of the rooms closed by the shutdown were still queued. Stop the telemetry service at the end of Start so it flushes its queue and drains the webhook notifier, waiting up to 5s. * Bump protocol to pick up the ResourceURLNotifier drain fix ResourceURLNotifier.Stop(false) now waits for the queued events to be sent (livekit/protocol#1901), which the shutdown change relies on. * telemetry: keep the analytics of webhooks sent during stop The notifier's processed hook queues a WEBHOOK analytics event for each webhook it sends, but Stop stopped the jobs queue before draining the notifier, so those events were dropped. Run the queued events with a barrier, drain the notifier, then stop the queue.
296 lines
10 KiB
Go
296 lines
10 KiB
Go
// Copyright 2023 LiveKit, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package telemetry_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/livekit/livekit-server/pkg/telemetry"
|
|
"github.com/livekit/livekit-server/pkg/telemetry/telemetryfakes"
|
|
"github.com/livekit/protocol/livekit"
|
|
"github.com/livekit/protocol/webhook"
|
|
)
|
|
|
|
func Test_OnParticipantJoin_EventIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
// prepare
|
|
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
|
|
partSID := "part1"
|
|
clientInfo := &livekit.ClientInfo{
|
|
Sdk: 2,
|
|
Version: "v1",
|
|
Os: "mac",
|
|
OsVersion: "v1",
|
|
DeviceModel: "DM1",
|
|
Browser: "chrome",
|
|
BrowserVersion: "97.0.1",
|
|
}
|
|
clientMeta := &livekit.AnalyticsClientMeta{
|
|
Region: "dark-side",
|
|
Node: "moon",
|
|
ClientAddr: "127.0.0.1",
|
|
ClientConnectTime: 420,
|
|
}
|
|
participantInfo := &livekit.ParticipantInfo{Sid: partSID}
|
|
guard := &telemetry.ReferenceGuard{}
|
|
|
|
// do
|
|
fixture.sut.ParticipantJoined(context.Background(), room, participantInfo, clientInfo, clientMeta, true, guard)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
// test
|
|
require.Equal(t, 1, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(0)
|
|
require.Equal(t, livekit.AnalyticsEventType_PARTICIPANT_JOINED, event.Type)
|
|
require.Equal(t, partSID, event.ParticipantId)
|
|
require.Equal(t, participantInfo, event.Participant)
|
|
require.Equal(t, room.Sid, event.RoomId)
|
|
require.Equal(t, room, event.Room)
|
|
|
|
require.Equal(t, clientInfo.Sdk, event.ClientInfo.Sdk)
|
|
require.Equal(t, clientInfo.Version, event.ClientInfo.Version)
|
|
require.Equal(t, clientInfo.Os, event.ClientInfo.Os)
|
|
require.Equal(t, clientInfo.OsVersion, event.ClientInfo.OsVersion)
|
|
require.Equal(t, clientInfo.DeviceModel, event.ClientInfo.DeviceModel)
|
|
require.Equal(t, clientInfo.Browser, event.ClientInfo.Browser)
|
|
require.Equal(t, clientInfo.BrowserVersion, event.ClientInfo.BrowserVersion)
|
|
|
|
require.Equal(t, clientMeta.Region, event.ClientMeta.Region)
|
|
require.Equal(t, clientMeta.Node, event.ClientMeta.Node)
|
|
require.Equal(t, clientMeta.ClientAddr, event.ClientMeta.ClientAddr)
|
|
require.Equal(t, clientMeta.ClientConnectTime, event.ClientMeta.ClientConnectTime)
|
|
}
|
|
|
|
func Test_OnParticipantLeft_EventIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
// prepare
|
|
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
|
|
partSID := "part1"
|
|
participantInfo := &livekit.ParticipantInfo{Sid: partSID}
|
|
guard := &telemetry.ReferenceGuard{}
|
|
|
|
// do
|
|
fixture.sut.ParticipantActive(context.Background(), room, participantInfo, &livekit.AnalyticsClientMeta{}, false, false, guard)
|
|
fixture.sut.ParticipantLeft(context.Background(), room, participantInfo, true, guard)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
// test
|
|
require.Equal(t, 2, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(1)
|
|
require.Equal(t, livekit.AnalyticsEventType_PARTICIPANT_LEFT, event.Type)
|
|
require.Equal(t, partSID, event.ParticipantId)
|
|
require.Equal(t, room.Sid, event.RoomId)
|
|
require.Equal(t, room, event.Room)
|
|
}
|
|
|
|
func Test_OnTrackUpdate_EventIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
// prepare
|
|
roomID := "room1"
|
|
roomName := "RoomName"
|
|
partID := "part1"
|
|
trackID := "track1"
|
|
layer := &livekit.VideoLayer{
|
|
Quality: livekit.VideoQuality_HIGH,
|
|
Width: uint32(360),
|
|
Height: uint32(720),
|
|
Bitrate: 2048,
|
|
}
|
|
|
|
trackInfo := &livekit.TrackInfo{
|
|
Sid: trackID,
|
|
Type: livekit.TrackType_VIDEO,
|
|
Muted: false,
|
|
Simulcast: false,
|
|
DisableDtx: false,
|
|
Layers: []*livekit.VideoLayer{layer},
|
|
}
|
|
|
|
// do
|
|
fixture.sut.TrackPublishedUpdate(context.Background(), &livekit.Room{Sid: roomID, Name: roomName}, livekit.ParticipantID(partID), trackInfo)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
// test
|
|
require.Equal(t, 1, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(0)
|
|
require.Equal(t, livekit.AnalyticsEventType_TRACK_PUBLISHED_UPDATE, event.Type)
|
|
require.Equal(t, partID, event.ParticipantId)
|
|
|
|
require.Equal(t, trackID, event.Track.Sid)
|
|
require.NotNil(t, event.Track.Layers)
|
|
require.Equal(t, layer.Width, event.Track.Layers[0].Width)
|
|
require.Equal(t, layer.Height, event.Track.Layers[0].Height)
|
|
require.Equal(t, layer.Quality, event.Track.Layers[0].Quality)
|
|
|
|
}
|
|
|
|
func Test_OnParticipantActive_EventIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
// prepare participant to change status
|
|
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
|
|
partSID := "part1"
|
|
|
|
clientInfo := &livekit.ClientInfo{
|
|
Sdk: 2,
|
|
Version: "v1",
|
|
Os: "mac",
|
|
OsVersion: "v1",
|
|
DeviceModel: "DM1",
|
|
Browser: "chrome",
|
|
BrowserVersion: "97.0.1",
|
|
}
|
|
clientMeta := &livekit.AnalyticsClientMeta{
|
|
Region: "dark-side",
|
|
Node: "moon",
|
|
ClientAddr: "127.0.0.1",
|
|
}
|
|
participantInfo := &livekit.ParticipantInfo{Sid: partSID}
|
|
guard := &telemetry.ReferenceGuard{}
|
|
|
|
// do
|
|
fixture.sut.ParticipantJoined(context.Background(), room, participantInfo, clientInfo, clientMeta, true, guard)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
// test
|
|
require.Equal(t, 1, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(0)
|
|
|
|
// test
|
|
// do
|
|
clientMetaConnect := &livekit.AnalyticsClientMeta{
|
|
ClientConnectTime: 420,
|
|
}
|
|
|
|
fixture.sut.ParticipantActive(context.Background(), room, participantInfo, clientMetaConnect, false, false, guard)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
require.Equal(t, 2, fixture.analytics.SendEventCallCount())
|
|
_, eventActive := fixture.analytics.SendEventArgsForCall(1)
|
|
require.Equal(t, livekit.AnalyticsEventType_PARTICIPANT_ACTIVE, eventActive.Type)
|
|
require.Equal(t, partSID, eventActive.ParticipantId)
|
|
require.Equal(t, room.Sid, eventActive.RoomId)
|
|
require.Equal(t, room, event.Room)
|
|
|
|
require.Equal(t, clientMetaConnect.ClientConnectTime, eventActive.ClientMeta.ClientConnectTime)
|
|
}
|
|
|
|
func Test_OnTrackSubscribed_EventIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
// prepare participant to change status
|
|
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
|
|
partSID := "part1"
|
|
publisherInfo := &livekit.ParticipantInfo{Sid: "pub1", Identity: "publisher"}
|
|
trackInfo := &livekit.TrackInfo{Sid: "tr1", Type: livekit.TrackType_VIDEO}
|
|
|
|
clientInfo := &livekit.ClientInfo{
|
|
Sdk: 2,
|
|
Version: "v1",
|
|
Os: "mac",
|
|
OsVersion: "v1",
|
|
DeviceModel: "DM1",
|
|
Browser: "chrome",
|
|
BrowserVersion: "97.0.1",
|
|
}
|
|
clientMeta := &livekit.AnalyticsClientMeta{
|
|
Region: "dark-side",
|
|
Node: "moon",
|
|
ClientAddr: "127.0.0.1",
|
|
}
|
|
participantInfo := &livekit.ParticipantInfo{Sid: partSID}
|
|
guard := &telemetry.ReferenceGuard{}
|
|
|
|
// do
|
|
fixture.sut.ParticipantJoined(context.Background(), room, participantInfo, clientInfo, clientMeta, true, guard)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
// test
|
|
require.Equal(t, 1, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(0)
|
|
require.Equal(t, room, event.Room)
|
|
|
|
// do
|
|
fixture.sut.TrackSubscribed(context.Background(), room, livekit.ParticipantID(partSID), trackInfo, publisherInfo, true)
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
require.Eventually(t, func() bool {
|
|
return fixture.analytics.SendEventCallCount() == 2
|
|
}, time.Second, time.Millisecond*50, "expected send event to be called twice")
|
|
_, eventTrackSubscribed := fixture.analytics.SendEventArgsForCall(1)
|
|
require.Equal(t, livekit.AnalyticsEventType_TRACK_SUBSCRIBED, eventTrackSubscribed.Type)
|
|
require.Equal(t, partSID, eventTrackSubscribed.ParticipantId)
|
|
require.Equal(t, trackInfo.Sid, eventTrackSubscribed.Track.Sid)
|
|
require.Equal(t, trackInfo.Type, eventTrackSubscribed.Track.Type)
|
|
require.Equal(t, publisherInfo.Sid, eventTrackSubscribed.Publisher.Sid)
|
|
require.Equal(t, publisherInfo.Identity, eventTrackSubscribed.Publisher.Identity)
|
|
|
|
}
|
|
|
|
func Test_OnRoomEnded_ReasonIsSent(t *testing.T) {
|
|
fixture := createFixture()
|
|
|
|
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
|
|
fixture.sut.RoomEnded(context.Background(), room, livekit.RoomEndReason_ROOM_END_API_DELETE)
|
|
|
|
fixture.flush()
|
|
|
|
require.Equal(t, 1, fixture.analytics.SendEventCallCount())
|
|
_, event := fixture.analytics.SendEventArgsForCall(0)
|
|
require.Equal(t, livekit.AnalyticsEventType_ROOM_ENDED, event.Type)
|
|
require.Equal(t, room.Sid, event.RoomId)
|
|
require.Equal(t, livekit.RoomEndReason_ROOM_END_API_DELETE, event.RoomEndReason)
|
|
}
|
|
|
|
// drainingNotifier sends the queued webhook on Stop(false) and reports it to the processed hook
|
|
type drainingNotifier struct {
|
|
hook func(ctx context.Context, whi *livekit.WebhookInfo)
|
|
}
|
|
|
|
func (n *drainingNotifier) RegisterProcessedHook(f func(ctx context.Context, whi *livekit.WebhookInfo)) {
|
|
n.hook = f
|
|
}
|
|
func (n *drainingNotifier) SetKeys(apiKey, apiSecret string) {}
|
|
func (n *drainingNotifier) SetFilter(params webhook.FilterParams) {}
|
|
func (n *drainingNotifier) QueueNotify(ctx context.Context, event *livekit.WebhookEvent, opts ...webhook.NotifyOption) error {
|
|
return nil
|
|
}
|
|
func (n *drainingNotifier) Stop(force bool) {
|
|
if !force {
|
|
n.hook(context.Background(), &livekit.WebhookInfo{Event: "room_finished"})
|
|
}
|
|
}
|
|
|
|
func Test_Stop_SendsWebhookEventsOfDrainedWebhooks(t *testing.T) {
|
|
analytics := &telemetryfakes.FakeAnalyticsService{}
|
|
notifier := &drainingNotifier{}
|
|
sut := telemetry.NewTelemetryService(notifier, analytics)
|
|
notifier.RegisterProcessedHook(sut.Webhook)
|
|
|
|
sut.Stop()
|
|
|
|
require.Equal(t, 1, analytics.SendEventCallCount())
|
|
_, event := analytics.SendEventArgsForCall(0)
|
|
require.Equal(t, livekit.AnalyticsEventType_WEBHOOK, event.Type)
|
|
require.Equal(t, "room_finished", event.Webhook.Event)
|
|
}
|