Server side metrics (#3198)
* mbb WIP * deps * WIP * WIP * remove unused file * Switch to enabled * misc * deps * mediatransportutil update * Typo * Set ParticipantIdentity in metrics data packets * use uint32 as JSON decoder does not unmarshal time.Duration
This commit is contained in:
@@ -18,10 +18,14 @@ package metric
|
||||
|
||||
type MetricConfig struct {
|
||||
Timestamper MetricTimestamperConfig `yaml:"timestamper_config,omitempty"`
|
||||
Collector MetricsCollectorConfig `yaml:"collector,omitempty"`
|
||||
Reporter MetricsReporterConfig `yaml:"reporter,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultMetricConfig = MetricConfig{
|
||||
Timestamper: DefaultMetricTimestamperConfig,
|
||||
Collector: DefaultMetricsCollectorConfig,
|
||||
Reporter: DefaultMetricsReporterConfig,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -63,6 +63,10 @@ func NewMetricTimestamper(params MetricTimestamperParams) *MetricTimestamper {
|
||||
}
|
||||
|
||||
func (m *MetricTimestamper) Process(batch *livekit.MetricsBatch) {
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// run OWD estimation periodically
|
||||
estimatedOWDNanos := m.maybeRunOWDEstimator(batch)
|
||||
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
// 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 metric
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/frostbyte73/core"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/mono"
|
||||
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type MetricsCollectorProvider interface {
|
||||
MetricsCollectorTimeToCollectMetrics()
|
||||
MetricsCollectorBatchReady(mb *livekit.MetricsBatch)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
type MetricsCollectorConfig struct {
|
||||
SamplingIntervalMs uint32 `yaml:"sampling_interval_ms,omitempty" json:"sampling_interval_ms,omitempty"`
|
||||
BatchIntervalMs uint32 `yaml:"batch_interval_ms,omitempty" json:"batch_interval_ms,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultMetricsCollectorConfig = MetricsCollectorConfig{
|
||||
SamplingIntervalMs: 3 * 1000,
|
||||
BatchIntervalMs: 10 * 1000,
|
||||
}
|
||||
)
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
type MetricsCollectorParams struct {
|
||||
ParticipantIdentity livekit.ParticipantIdentity
|
||||
Config MetricsCollectorConfig
|
||||
Provider MetricsCollectorProvider
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type MetricsCollector struct {
|
||||
params MetricsCollectorParams
|
||||
|
||||
lock sync.RWMutex
|
||||
mbb *utils.MetricsBatchBuilder
|
||||
publisherRTTMetricId map[livekit.ParticipantIdentity]int
|
||||
subscriberRTTMetricId int
|
||||
relayRTTMetricId map[livekit.ParticipantIdentity]int
|
||||
|
||||
stop core.Fuse
|
||||
}
|
||||
|
||||
func NewMetricsCollector(params MetricsCollectorParams) *MetricsCollector {
|
||||
mc := &MetricsCollector{
|
||||
params: params,
|
||||
}
|
||||
mc.reset()
|
||||
|
||||
go mc.worker()
|
||||
return mc
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) Stop() {
|
||||
if mc != nil {
|
||||
mc.stop.Break()
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) AddPublisherRTT(participantIdentity livekit.ParticipantIdentity, rtt float32) {
|
||||
mc.lock.Lock()
|
||||
defer mc.lock.Unlock()
|
||||
|
||||
metricId, ok := mc.publisherRTTMetricId[participantIdentity]
|
||||
if !ok {
|
||||
var err error
|
||||
metricId, err = mc.createTimeSeriesMetric(livekit.MetricLabel_PUBLISHER_RTT, participantIdentity)
|
||||
if err != nil {
|
||||
mc.params.Logger.Warnw("could not add time series metric for publisher RTT", err)
|
||||
return
|
||||
}
|
||||
|
||||
mc.publisherRTTMetricId[participantIdentity] = metricId
|
||||
}
|
||||
|
||||
mc.addTimeSeriesMetricSample(metricId, rtt)
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) AddSubscriberRTT(rtt float32) {
|
||||
mc.lock.Lock()
|
||||
defer mc.lock.Unlock()
|
||||
|
||||
if mc.subscriberRTTMetricId == utils.MetricsBatchBuilderInvalidTimeSeriesMetricId {
|
||||
var err error
|
||||
mc.subscriberRTTMetricId, err = mc.createTimeSeriesMetric(livekit.MetricLabel_SUBSCRIBER_RTT, mc.params.ParticipantIdentity)
|
||||
if err != nil {
|
||||
mc.params.Logger.Warnw("could not add time series metric for publisher RTT", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
mc.addTimeSeriesMetricSample(mc.subscriberRTTMetricId, rtt)
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) AddRelayRTT(participantIdentity livekit.ParticipantIdentity, rtt float32) {
|
||||
mc.lock.Lock()
|
||||
defer mc.lock.Unlock()
|
||||
|
||||
metricId, ok := mc.relayRTTMetricId[participantIdentity]
|
||||
if !ok {
|
||||
var err error
|
||||
metricId, err = mc.createTimeSeriesMetric(livekit.MetricLabel_SERVER_MESH_RTT, participantIdentity)
|
||||
if err != nil {
|
||||
mc.params.Logger.Warnw("could not add time series metric for server mesh RTT", err)
|
||||
return
|
||||
}
|
||||
|
||||
mc.relayRTTMetricId[participantIdentity] = metricId
|
||||
}
|
||||
|
||||
mc.addTimeSeriesMetricSample(metricId, rtt)
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) getMetricsBatchAndReset() *livekit.MetricsBatch {
|
||||
mc.lock.Lock()
|
||||
mbb := mc.mbb
|
||||
|
||||
mc.reset()
|
||||
mc.lock.Unlock()
|
||||
|
||||
if mbb.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
now := mono.Now()
|
||||
mbb.SetTime(now, now)
|
||||
return mbb.ToProto()
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) reset() {
|
||||
mc.mbb = utils.NewMetricsBatchBuilder()
|
||||
mc.mbb.SetRestrictedLabels(utils.MetricRestrictedLabels{
|
||||
LabelRanges: []utils.MetricLabelRange{
|
||||
{
|
||||
StartInclusive: livekit.MetricLabel_CLIENT_VIDEO_SUBSCRIBER_FREEZE_COUNT,
|
||||
EndInclusive: livekit.MetricLabel_CLIENT_VIDEO_PUBLISHER_QUALITY_LIMITATION_DURATION_OTHER,
|
||||
},
|
||||
},
|
||||
ParticipantIdentity: mc.params.ParticipantIdentity,
|
||||
})
|
||||
|
||||
mc.publisherRTTMetricId = make(map[livekit.ParticipantIdentity]int)
|
||||
mc.subscriberRTTMetricId = utils.MetricsBatchBuilderInvalidTimeSeriesMetricId
|
||||
mc.relayRTTMetricId = make(map[livekit.ParticipantIdentity]int)
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) createTimeSeriesMetric(
|
||||
label livekit.MetricLabel,
|
||||
participantIdentity livekit.ParticipantIdentity,
|
||||
) (int, error) {
|
||||
return mc.mbb.AddTimeSeriesMetric(utils.TimeSeriesMetric{
|
||||
MetricLabel: label,
|
||||
ParticipantIdentity: participantIdentity,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) addTimeSeriesMetricSample(metricId int, value float32) {
|
||||
now := mono.Now()
|
||||
if err := mc.mbb.AddMetricSamplesToTimeSeriesMetric(metricId, []utils.MetricSample{
|
||||
{
|
||||
At: now,
|
||||
NormalizedAt: now,
|
||||
Value: value,
|
||||
},
|
||||
}); err != nil {
|
||||
mc.params.Logger.Warnw("could not add metric sample", err, "metricId", metricId)
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *MetricsCollector) worker() {
|
||||
samplingIntervalMs := mc.params.Config.SamplingIntervalMs
|
||||
if samplingIntervalMs == 0 {
|
||||
samplingIntervalMs = DefaultMetricsCollectorConfig.SamplingIntervalMs
|
||||
}
|
||||
samplingTicker := time.NewTicker(time.Duration(samplingIntervalMs) * time.Millisecond)
|
||||
defer samplingTicker.Stop()
|
||||
|
||||
batchIntervalMs := mc.params.Config.BatchIntervalMs
|
||||
if batchIntervalMs < samplingIntervalMs {
|
||||
batchIntervalMs = samplingIntervalMs
|
||||
}
|
||||
batchTicker := time.NewTicker(time.Duration(batchIntervalMs) * time.Millisecond)
|
||||
defer batchTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-samplingTicker.C:
|
||||
mc.params.Provider.MetricsCollectorTimeToCollectMetrics()
|
||||
|
||||
case <-batchTicker.C:
|
||||
if mb := mc.getMetricsBatchAndReset(); mb != nil {
|
||||
mc.params.Provider.MetricsCollectorBatchReady(mb)
|
||||
}
|
||||
|
||||
case <-mc.stop.Watch():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
// 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 metric
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/frostbyte73/core"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/mono"
|
||||
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type MetricsReporterConsumer interface {
|
||||
MetricsReporterBatchReady(mb *livekit.MetricsBatch)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
type MetricsReporterConfig struct {
|
||||
ReportingIntervalMs uint32 `yaml:"reporting_interval_ms,omitempty" json:"reporting_interval_ms,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultMetricsReporterConfig = MetricsReporterConfig{
|
||||
ReportingIntervalMs: 10 * 1000,
|
||||
}
|
||||
)
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
||||
type MetricsReporterParams struct {
|
||||
ParticipantIdentity livekit.ParticipantIdentity
|
||||
Config MetricsReporterConfig
|
||||
Consumer MetricsReporterConsumer
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type MetricsReporter struct {
|
||||
params MetricsReporterParams
|
||||
|
||||
lock sync.RWMutex
|
||||
mbb *utils.MetricsBatchBuilder
|
||||
|
||||
stop core.Fuse
|
||||
}
|
||||
|
||||
func NewMetricsReporter(params MetricsReporterParams) *MetricsReporter {
|
||||
mr := &MetricsReporter{
|
||||
params: params,
|
||||
}
|
||||
mr.reset()
|
||||
|
||||
go mr.worker()
|
||||
return mr
|
||||
}
|
||||
|
||||
func (mr *MetricsReporter) Stop() {
|
||||
if mr != nil {
|
||||
mr.stop.Break()
|
||||
}
|
||||
}
|
||||
|
||||
func (mr *MetricsReporter) Merge(other *livekit.MetricsBatch) {
|
||||
if mr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
mr.lock.Lock()
|
||||
defer mr.lock.Unlock()
|
||||
|
||||
mr.mbb.Merge(other)
|
||||
}
|
||||
|
||||
func (mr *MetricsReporter) getMetricsBatchAndReset() *livekit.MetricsBatch {
|
||||
mr.lock.Lock()
|
||||
mbb := mr.mbb
|
||||
|
||||
mr.reset()
|
||||
mr.lock.Unlock()
|
||||
|
||||
if mbb.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
now := mono.Now()
|
||||
mbb.SetTime(now, now)
|
||||
return mbb.ToProto()
|
||||
}
|
||||
|
||||
func (mr *MetricsReporter) reset() {
|
||||
mr.mbb = utils.NewMetricsBatchBuilder()
|
||||
mr.mbb.SetRestrictedLabels(utils.MetricRestrictedLabels{
|
||||
LabelRanges: []utils.MetricLabelRange{
|
||||
{
|
||||
StartInclusive: livekit.MetricLabel_CLIENT_VIDEO_SUBSCRIBER_FREEZE_COUNT,
|
||||
EndInclusive: livekit.MetricLabel_CLIENT_VIDEO_PUBLISHER_QUALITY_LIMITATION_DURATION_OTHER,
|
||||
},
|
||||
},
|
||||
ParticipantIdentity: mr.params.ParticipantIdentity,
|
||||
})
|
||||
}
|
||||
|
||||
func (mr *MetricsReporter) worker() {
|
||||
reportingIntervalMs := mr.params.Config.ReportingIntervalMs
|
||||
if reportingIntervalMs == 0 {
|
||||
reportingIntervalMs = DefaultMetricsReporterConfig.ReportingIntervalMs
|
||||
}
|
||||
reportingTicker := time.NewTicker(time.Duration(reportingIntervalMs) * time.Millisecond)
|
||||
defer reportingTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-reportingTicker.C:
|
||||
if mb := mr.getMetricsBatchAndReset(); mb != nil {
|
||||
mr.params.Consumer.MetricsReporterBatchReady(mb)
|
||||
}
|
||||
|
||||
case <-mr.stop.Watch():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type FakeMessageSink struct {
|
||||
@@ -34,10 +34,10 @@ type FakeMessageSink struct {
|
||||
isClosedReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
WriteMessageStub func(protoreflect.ProtoMessage) error
|
||||
WriteMessageStub func(proto.Message) error
|
||||
writeMessageMutex sync.RWMutex
|
||||
writeMessageArgsForCall []struct {
|
||||
arg1 protoreflect.ProtoMessage
|
||||
arg1 proto.Message
|
||||
}
|
||||
writeMessageReturns struct {
|
||||
result1 error
|
||||
@@ -179,11 +179,11 @@ func (fake *FakeMessageSink) IsClosedReturnsOnCall(i int, result1 bool) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSink) WriteMessage(arg1 protoreflect.ProtoMessage) error {
|
||||
func (fake *FakeMessageSink) WriteMessage(arg1 proto.Message) error {
|
||||
fake.writeMessageMutex.Lock()
|
||||
ret, specificReturn := fake.writeMessageReturnsOnCall[len(fake.writeMessageArgsForCall)]
|
||||
fake.writeMessageArgsForCall = append(fake.writeMessageArgsForCall, struct {
|
||||
arg1 protoreflect.ProtoMessage
|
||||
arg1 proto.Message
|
||||
}{arg1})
|
||||
stub := fake.WriteMessageStub
|
||||
fakeReturns := fake.writeMessageReturns
|
||||
@@ -204,13 +204,13 @@ func (fake *FakeMessageSink) WriteMessageCallCount() int {
|
||||
return len(fake.writeMessageArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSink) WriteMessageCalls(stub func(protoreflect.ProtoMessage) error) {
|
||||
func (fake *FakeMessageSink) WriteMessageCalls(stub func(proto.Message) error) {
|
||||
fake.writeMessageMutex.Lock()
|
||||
defer fake.writeMessageMutex.Unlock()
|
||||
fake.WriteMessageStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSink) WriteMessageArgsForCall(i int) protoreflect.ProtoMessage {
|
||||
func (fake *FakeMessageSink) WriteMessageArgsForCall(i int) proto.Message {
|
||||
fake.writeMessageMutex.RLock()
|
||||
defer fake.writeMessageMutex.RUnlock()
|
||||
argsForCall := fake.writeMessageArgsForCall[i]
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type FakeMessageSource struct {
|
||||
@@ -34,15 +34,15 @@ type FakeMessageSource struct {
|
||||
isClosedReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
ReadChanStub func() <-chan protoreflect.ProtoMessage
|
||||
ReadChanStub func() <-chan proto.Message
|
||||
readChanMutex sync.RWMutex
|
||||
readChanArgsForCall []struct {
|
||||
}
|
||||
readChanReturns struct {
|
||||
result1 <-chan protoreflect.ProtoMessage
|
||||
result1 <-chan proto.Message
|
||||
}
|
||||
readChanReturnsOnCall map[int]struct {
|
||||
result1 <-chan protoreflect.ProtoMessage
|
||||
result1 <-chan proto.Message
|
||||
}
|
||||
invocations map[string][][]interface{}
|
||||
invocationsMutex sync.RWMutex
|
||||
@@ -178,7 +178,7 @@ func (fake *FakeMessageSource) IsClosedReturnsOnCall(i int, result1 bool) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSource) ReadChan() <-chan protoreflect.ProtoMessage {
|
||||
func (fake *FakeMessageSource) ReadChan() <-chan proto.Message {
|
||||
fake.readChanMutex.Lock()
|
||||
ret, specificReturn := fake.readChanReturnsOnCall[len(fake.readChanArgsForCall)]
|
||||
fake.readChanArgsForCall = append(fake.readChanArgsForCall, struct {
|
||||
@@ -202,32 +202,32 @@ func (fake *FakeMessageSource) ReadChanCallCount() int {
|
||||
return len(fake.readChanArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSource) ReadChanCalls(stub func() <-chan protoreflect.ProtoMessage) {
|
||||
func (fake *FakeMessageSource) ReadChanCalls(stub func() <-chan proto.Message) {
|
||||
fake.readChanMutex.Lock()
|
||||
defer fake.readChanMutex.Unlock()
|
||||
fake.ReadChanStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSource) ReadChanReturns(result1 <-chan protoreflect.ProtoMessage) {
|
||||
func (fake *FakeMessageSource) ReadChanReturns(result1 <-chan proto.Message) {
|
||||
fake.readChanMutex.Lock()
|
||||
defer fake.readChanMutex.Unlock()
|
||||
fake.ReadChanStub = nil
|
||||
fake.readChanReturns = struct {
|
||||
result1 <-chan protoreflect.ProtoMessage
|
||||
result1 <-chan proto.Message
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeMessageSource) ReadChanReturnsOnCall(i int, result1 <-chan protoreflect.ProtoMessage) {
|
||||
func (fake *FakeMessageSource) ReadChanReturnsOnCall(i int, result1 <-chan proto.Message) {
|
||||
fake.readChanMutex.Lock()
|
||||
defer fake.readChanMutex.Unlock()
|
||||
fake.ReadChanStub = nil
|
||||
if fake.readChanReturnsOnCall == nil {
|
||||
fake.readChanReturnsOnCall = make(map[int]struct {
|
||||
result1 <-chan protoreflect.ProtoMessage
|
||||
result1 <-chan proto.Message
|
||||
})
|
||||
}
|
||||
fake.readChanReturnsOnCall[i] = struct {
|
||||
result1 <-chan protoreflect.ProtoMessage
|
||||
result1 <-chan proto.Message
|
||||
}{result1}
|
||||
}
|
||||
|
||||
|
||||
+76
-26
@@ -161,6 +161,7 @@ type ParticipantParams struct {
|
||||
UseSendSideBWEInterceptor bool
|
||||
UseSendSideBWE bool
|
||||
UseOneShotSignallingMode bool
|
||||
EnableMetrics bool
|
||||
}
|
||||
|
||||
type ParticipantImpl struct {
|
||||
@@ -238,7 +239,7 @@ type ParticipantImpl struct {
|
||||
onMigrateStateChange func(p types.LocalParticipant, migrateState types.MigrateState)
|
||||
onParticipantUpdate func(types.LocalParticipant)
|
||||
onDataPacket func(types.LocalParticipant, livekit.DataPacket_Kind, *livekit.DataPacket)
|
||||
onMetrics func(types.LocalParticipant, *livekit.DataPacket)
|
||||
onMetrics func(types.Participant, *livekit.DataPacket)
|
||||
|
||||
migrateState atomic.Value // types.MigrateState
|
||||
|
||||
@@ -254,6 +255,8 @@ type ParticipantImpl struct {
|
||||
tracksQuality map[livekit.TrackID]livekit.ConnectionQuality
|
||||
|
||||
metricTimestamper *metric.MetricTimestamper
|
||||
metricsCollector *metric.MetricsCollector
|
||||
metricsReporter *metric.MetricsReporter
|
||||
|
||||
// loggers for publisher and subscriber
|
||||
pubLogger logger.Logger
|
||||
@@ -289,12 +292,8 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
|
||||
params.Telemetry,
|
||||
),
|
||||
tracksQuality: make(map[livekit.TrackID]livekit.ConnectionQuality),
|
||||
metricTimestamper: metric.NewMetricTimestamper(metric.MetricTimestamperParams{
|
||||
Config: params.MetricConfig.Timestamper,
|
||||
Logger: params.Logger,
|
||||
}),
|
||||
pubLogger: params.Logger.WithComponent(sutils.ComponentPub),
|
||||
subLogger: params.Logger.WithComponent(sutils.ComponentSub),
|
||||
pubLogger: params.Logger.WithComponent(sutils.ComponentPub),
|
||||
subLogger: params.Logger.WithComponent(sutils.ComponentSub),
|
||||
}
|
||||
if !params.DisableSupervisor {
|
||||
p.supervisor = supervisor.NewParticipantSupervisor(supervisor.ParticipantSupervisorParams{Logger: params.Logger})
|
||||
@@ -325,6 +324,7 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
|
||||
|
||||
p.setupUpTrackManager()
|
||||
p.setupSubscriptionManager()
|
||||
p.setupMetrics()
|
||||
|
||||
return p, nil
|
||||
}
|
||||
@@ -784,13 +784,13 @@ func (p *ParticipantImpl) getOnDataPacket() func(types.LocalParticipant, livekit
|
||||
return p.onDataPacket
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) OnMetrics(callback func(types.LocalParticipant, *livekit.DataPacket)) {
|
||||
func (p *ParticipantImpl) OnMetrics(callback func(types.Participant, *livekit.DataPacket)) {
|
||||
p.lock.Lock()
|
||||
p.onMetrics = callback
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) getOnMetrics() func(types.LocalParticipant, *livekit.DataPacket) {
|
||||
func (p *ParticipantImpl) getOnMetrics() func(types.Participant, *livekit.DataPacket) {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
return p.onMetrics
|
||||
@@ -1076,6 +1076,9 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
|
||||
go func() {
|
||||
p.SubscriptionManager.Close(isExpectedToResume)
|
||||
p.TransportManager.Close()
|
||||
|
||||
p.metricsCollector.Stop()
|
||||
p.metricsReporter.Stop()
|
||||
}()
|
||||
|
||||
p.dataChannelStats.Stop()
|
||||
@@ -1607,7 +1610,6 @@ func (p *ParticipantImpl) setupTransportManager() error {
|
||||
|
||||
func (p *ParticipantImpl) setupUpTrackManager() {
|
||||
p.UpTrackManager = NewUpTrackManager(UpTrackManagerParams{
|
||||
SID: p.params.SID,
|
||||
Logger: p.pubLogger,
|
||||
VersionGenerator: p.params.VersionGenerator,
|
||||
})
|
||||
@@ -1637,6 +1639,67 @@ func (p *ParticipantImpl) setupSubscriptionManager() {
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) MetricsCollectorTimeToCollectMetrics() {
|
||||
publisherRTT, ok := p.TransportManager.GetPublisherRTT()
|
||||
if ok {
|
||||
p.metricsCollector.AddPublisherRTT(p.Identity(), float32(publisherRTT))
|
||||
}
|
||||
|
||||
subscriberRTT, ok := p.TransportManager.GetSubscriberRTT()
|
||||
if ok {
|
||||
p.metricsCollector.AddSubscriberRTT(float32(subscriberRTT))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) MetricsCollectorBatchReady(mb *livekit.MetricsBatch) {
|
||||
if onMetrics := p.getOnMetrics(); onMetrics != nil {
|
||||
onMetrics(p, &livekit.DataPacket{
|
||||
ParticipantIdentity: string(p.Identity()),
|
||||
Value: &livekit.DataPacket_Metrics{
|
||||
Metrics: mb,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) MetricsReporterBatchReady(mb *livekit.MetricsBatch) {
|
||||
dpData, err := proto.Marshal(&livekit.DataPacket{
|
||||
ParticipantIdentity: string(p.Identity()),
|
||||
Value: &livekit.DataPacket_Metrics{
|
||||
Metrics: mb,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
p.params.Logger.Errorw("failed to marshal data packet", err)
|
||||
return
|
||||
}
|
||||
|
||||
p.TransportManager.SendDataPacket(livekit.DataPacket_RELIABLE, dpData)
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) setupMetrics() {
|
||||
if !p.params.EnableMetrics {
|
||||
return
|
||||
}
|
||||
|
||||
p.metricTimestamper = metric.NewMetricTimestamper(metric.MetricTimestamperParams{
|
||||
Config: p.params.MetricConfig.Timestamper,
|
||||
Logger: p.params.Logger,
|
||||
})
|
||||
p.metricsCollector = metric.NewMetricsCollector(metric.MetricsCollectorParams{
|
||||
ParticipantIdentity: p.Identity(),
|
||||
Config: p.params.MetricConfig.Collector,
|
||||
Provider: p,
|
||||
Logger: p.params.Logger,
|
||||
})
|
||||
p.metricsReporter = metric.NewMetricsReporter(metric.MetricsReporterParams{
|
||||
ParticipantIdentity: p.Identity(),
|
||||
Config: p.params.MetricConfig.Reporter,
|
||||
Consumer: p,
|
||||
Logger: p.params.Logger,
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
|
||||
if state == livekit.ParticipantInfo_ACTIVE {
|
||||
t := time.Now()
|
||||
@@ -3030,25 +3093,12 @@ func (p *ParticipantImpl) HandleMetrics(senderParticipantID livekit.ParticipantI
|
||||
return ErrNoSubscribeMetricsPermission
|
||||
}
|
||||
|
||||
if !p.SubscriptionManager.IsSubscribedTo(senderParticipantID) {
|
||||
if senderParticipantID != p.ID() && !p.SubscriptionManager.IsSubscribedTo(senderParticipantID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// METRICS-TODO: This is just forwarding. Will have to do more, including but not limited to
|
||||
// 1. Filtering: subscriber metrics from self only should be sent to that participant.
|
||||
// 2. Batching: could include re-mapping labels to consolidate multiple batches.
|
||||
// 3. (Maybe) Time stamps: this is done on receive, TBD if required here also
|
||||
dpData, err := proto.Marshal(&livekit.DataPacket{
|
||||
Value: &livekit.DataPacket_Metrics{
|
||||
Metrics: metrics,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
p.params.Logger.Errorw("failed to marshal data packet", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return p.TransportManager.SendDataPacket(livekit.DataPacket_RELIABLE, dpData)
|
||||
p.metricsReporter.Merge(metrics)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ----------------------------------------------
|
||||
|
||||
+1
-1
@@ -1270,7 +1270,7 @@ func (r *Room) onDataPacket(source types.LocalParticipant, kind livekit.DataPack
|
||||
BroadcastDataPacketForRoom(r, source, kind, dp, r.Logger)
|
||||
}
|
||||
|
||||
func (r *Room) onMetrics(source types.LocalParticipant, dp *livekit.DataPacket) {
|
||||
func (r *Room) onMetrics(source types.Participant, dp *livekit.DataPacket) {
|
||||
BroadcastMetricsForRoom(r, source, dp, r.Logger)
|
||||
}
|
||||
|
||||
|
||||
+19
-4
@@ -81,6 +81,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoICETransport = errors.New("no ICE transport")
|
||||
ErrIceRestartWithoutLocalSDP = errors.New("ICE restart without local SDP settled")
|
||||
ErrIceRestartOnClosedPeerConnection = errors.New("ICE restart on closed peer connection")
|
||||
ErrNoTransceiver = errors.New("no transceiver")
|
||||
@@ -178,9 +179,10 @@ type trackDescription struct {
|
||||
|
||||
// PCTransport is a wrapper around PeerConnection, with some helper methods
|
||||
type PCTransport struct {
|
||||
params TransportParams
|
||||
pc *webrtc.PeerConnection
|
||||
me *webrtc.MediaEngine
|
||||
params TransportParams
|
||||
pc *webrtc.PeerConnection
|
||||
iceTransport *webrtc.ICETransport
|
||||
me *webrtc.MediaEngine
|
||||
|
||||
lock sync.RWMutex
|
||||
|
||||
@@ -506,7 +508,11 @@ func (t *PCTransport) createPeerConnection() error {
|
||||
t.pc.OnDataChannel(t.onDataChannel)
|
||||
t.pc.OnTrack(t.params.Handler.OnTrack)
|
||||
|
||||
t.pc.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange(func(pair *webrtc.ICECandidatePair) {
|
||||
t.iceTransport = t.pc.SCTP().Transport().ICETransport()
|
||||
if t.iceTransport == nil {
|
||||
return ErrNoICETransport
|
||||
}
|
||||
t.iceTransport.OnSelectedCandidatePairChange(func(pair *webrtc.ICECandidatePair) {
|
||||
t.params.Logger.Debugw("selected ICE candidate pair changed", "pair", wrappedICECandidatePairLogger{pair})
|
||||
t.connectionDetails.SetSelectedPair(pair)
|
||||
existingPair := t.selectedPair.Load()
|
||||
@@ -942,6 +948,15 @@ func (t *PCTransport) CreateDataChannelIfEmpty(dcLabel string, dci *webrtc.DataC
|
||||
return dc.Label(), *dc.ID(), false, nil
|
||||
}
|
||||
|
||||
func (t *PCTransport) GetRTT() (float64, bool) {
|
||||
scps, ok := t.iceTransport.GetSelectedCandidatePairStats()
|
||||
if !ok {
|
||||
return 0.0, false
|
||||
}
|
||||
|
||||
return scps.CurrentRoundTripTime, true
|
||||
}
|
||||
|
||||
// IsEstablished returns true if the PeerConnection has been established
|
||||
func (t *PCTransport) IsEstablished() bool {
|
||||
return t.pc.ConnectionState() != webrtc.PeerConnectionStateNew
|
||||
|
||||
@@ -218,6 +218,10 @@ func (t *TransportManager) IsPublisherEstablished() bool {
|
||||
return t.publisher.IsEstablished()
|
||||
}
|
||||
|
||||
func (t *TransportManager) GetPublisherRTT() (float64, bool) {
|
||||
return t.publisher.GetRTT()
|
||||
}
|
||||
|
||||
func (t *TransportManager) GetPublisherMid(rtpReceiver *webrtc.RTPReceiver) string {
|
||||
return t.publisher.GetMid(rtpReceiver)
|
||||
}
|
||||
@@ -230,6 +234,10 @@ func (t *TransportManager) WritePublisherRTCP(pkts []rtcp.Packet) error {
|
||||
return t.publisher.WriteRTCP(pkts)
|
||||
}
|
||||
|
||||
func (t *TransportManager) GetSubscriberRTT() (float64, bool) {
|
||||
return t.subscriber.GetRTT()
|
||||
}
|
||||
|
||||
func (t *TransportManager) HasSubscriberEverConnected() bool {
|
||||
return t.subscriber.HasEverConnected()
|
||||
}
|
||||
|
||||
@@ -300,6 +300,8 @@ type Participant interface {
|
||||
) error
|
||||
|
||||
DebugInfo() map[string]interface{}
|
||||
|
||||
OnMetrics(callback func(Participant, *livekit.DataPacket))
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
@@ -420,7 +422,6 @@ type LocalParticipant interface {
|
||||
// OnParticipantUpdate - metadata or permission is updated
|
||||
OnParticipantUpdate(callback func(LocalParticipant))
|
||||
OnDataPacket(callback func(LocalParticipant, livekit.DataPacket_Kind, *livekit.DataPacket))
|
||||
OnMetrics(callback func(LocalParticipant, *livekit.DataPacket))
|
||||
OnSubscribeStatusChanged(fn func(publisherID livekit.ParticipantID, subscribed bool))
|
||||
OnClose(callback func(LocalParticipant))
|
||||
OnClaimsChanged(callback func(LocalParticipant))
|
||||
|
||||
@@ -706,10 +706,10 @@ type FakeLocalParticipant struct {
|
||||
onICEConfigChangedArgsForCall []struct {
|
||||
arg1 func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)
|
||||
}
|
||||
OnMetricsStub func(func(types.LocalParticipant, *livekit.DataPacket))
|
||||
OnMetricsStub func(func(types.Participant, *livekit.DataPacket))
|
||||
onMetricsMutex sync.RWMutex
|
||||
onMetricsArgsForCall []struct {
|
||||
arg1 func(types.LocalParticipant, *livekit.DataPacket)
|
||||
arg1 func(types.Participant, *livekit.DataPacket)
|
||||
}
|
||||
OnMigrateStateChangeStub func(func(p types.LocalParticipant, migrateState types.MigrateState))
|
||||
onMigrateStateChangeMutex sync.RWMutex
|
||||
@@ -4821,10 +4821,10 @@ func (fake *FakeLocalParticipant) OnICEConfigChangedArgsForCall(i int) func(part
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) OnMetrics(arg1 func(types.LocalParticipant, *livekit.DataPacket)) {
|
||||
func (fake *FakeLocalParticipant) OnMetrics(arg1 func(types.Participant, *livekit.DataPacket)) {
|
||||
fake.onMetricsMutex.Lock()
|
||||
fake.onMetricsArgsForCall = append(fake.onMetricsArgsForCall, struct {
|
||||
arg1 func(types.LocalParticipant, *livekit.DataPacket)
|
||||
arg1 func(types.Participant, *livekit.DataPacket)
|
||||
}{arg1})
|
||||
stub := fake.OnMetricsStub
|
||||
fake.recordInvocation("OnMetrics", []interface{}{arg1})
|
||||
@@ -4840,13 +4840,13 @@ func (fake *FakeLocalParticipant) OnMetricsCallCount() int {
|
||||
return len(fake.onMetricsArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) OnMetricsCalls(stub func(func(types.LocalParticipant, *livekit.DataPacket))) {
|
||||
func (fake *FakeLocalParticipant) OnMetricsCalls(stub func(func(types.Participant, *livekit.DataPacket))) {
|
||||
fake.onMetricsMutex.Lock()
|
||||
defer fake.onMetricsMutex.Unlock()
|
||||
fake.OnMetricsStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) OnMetricsArgsForCall(i int) func(types.LocalParticipant, *livekit.DataPacket) {
|
||||
func (fake *FakeLocalParticipant) OnMetricsArgsForCall(i int) func(types.Participant, *livekit.DataPacket) {
|
||||
fake.onMetricsMutex.RLock()
|
||||
defer fake.onMetricsMutex.RUnlock()
|
||||
argsForCall := fake.onMetricsArgsForCall[i]
|
||||
|
||||
@@ -178,6 +178,11 @@ type FakeParticipant struct {
|
||||
kindReturnsOnCall map[int]struct {
|
||||
result1 livekit.ParticipantInfo_Kind
|
||||
}
|
||||
OnMetricsStub func(func(types.Participant, *livekit.DataPacket))
|
||||
onMetricsMutex sync.RWMutex
|
||||
onMetricsArgsForCall []struct {
|
||||
arg1 func(types.Participant, *livekit.DataPacket)
|
||||
}
|
||||
RemovePublishedTrackStub func(types.MediaTrack, bool, bool)
|
||||
removePublishedTrackMutex sync.RWMutex
|
||||
removePublishedTrackArgsForCall []struct {
|
||||
@@ -1122,6 +1127,38 @@ func (fake *FakeParticipant) KindReturnsOnCall(i int, result1 livekit.Participan
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) OnMetrics(arg1 func(types.Participant, *livekit.DataPacket)) {
|
||||
fake.onMetricsMutex.Lock()
|
||||
fake.onMetricsArgsForCall = append(fake.onMetricsArgsForCall, struct {
|
||||
arg1 func(types.Participant, *livekit.DataPacket)
|
||||
}{arg1})
|
||||
stub := fake.OnMetricsStub
|
||||
fake.recordInvocation("OnMetrics", []interface{}{arg1})
|
||||
fake.onMetricsMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.OnMetricsStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) OnMetricsCallCount() int {
|
||||
fake.onMetricsMutex.RLock()
|
||||
defer fake.onMetricsMutex.RUnlock()
|
||||
return len(fake.onMetricsArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) OnMetricsCalls(stub func(func(types.Participant, *livekit.DataPacket))) {
|
||||
fake.onMetricsMutex.Lock()
|
||||
defer fake.onMetricsMutex.Unlock()
|
||||
fake.OnMetricsStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) OnMetricsArgsForCall(i int) func(types.Participant, *livekit.DataPacket) {
|
||||
fake.onMetricsMutex.RLock()
|
||||
defer fake.onMetricsMutex.RUnlock()
|
||||
argsForCall := fake.onMetricsArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RemovePublishedTrack(arg1 types.MediaTrack, arg2 bool, arg3 bool) {
|
||||
fake.removePublishedTrackMutex.Lock()
|
||||
fake.removePublishedTrackArgsForCall = append(fake.removePublishedTrackArgsForCall, struct {
|
||||
@@ -1469,6 +1506,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.isRecorderMutex.RUnlock()
|
||||
fake.kindMutex.RLock()
|
||||
defer fake.kindMutex.RUnlock()
|
||||
fake.onMetricsMutex.RLock()
|
||||
defer fake.onMetricsMutex.RUnlock()
|
||||
fake.removePublishedTrackMutex.RLock()
|
||||
defer fake.removePublishedTrackMutex.RUnlock()
|
||||
fake.stateMutex.RLock()
|
||||
|
||||
@@ -31,7 +31,6 @@ var (
|
||||
)
|
||||
|
||||
type UpTrackManagerParams struct {
|
||||
SID livekit.ParticipantID
|
||||
Logger logger.Logger
|
||||
VersionGenerator utils.TimedVersionGenerator
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user