Add WebRTC candidate metrics (#18)

* stats: add protocol to iceCandidates.

* add ice candidates used metric.

* NewICECandidate use whole struct.
This commit is contained in:
Miroslav Šedivý 2022-12-16 13:49:51 +01:00 committed by GitHub
parent b49f545094
commit fb8462b56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 9 deletions

View File

@ -545,13 +545,31 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int)
manager.metrics.SetSctpTransportStats(session, data) manager.metrics.SetSctpTransportStats(session, data)
} }
remoteCandidates := map[string]webrtc.ICECandidateStats{}
nominatedRemoteCandidates := map[string]struct{}{}
for _, entry := range stats { for _, entry := range stats {
// only remote ice candidate stats // only remote ice candidate stats
candidate, ok := entry.(webrtc.ICECandidateStats) candidate, ok := entry.(webrtc.ICECandidateStats)
if ok && candidate.Type == webrtc.StatsTypeRemoteCandidate { if ok && candidate.Type == webrtc.StatsTypeRemoteCandidate {
manager.metrics.NewICECandidate(session, candidate.ID) manager.metrics.NewICECandidate(session, candidate)
remoteCandidates[candidate.ID] = candidate
}
// only nominated ice candidate pair stats
pair, ok := entry.(webrtc.ICECandidatePairStats)
if ok && pair.Nominated {
nominatedRemoteCandidates[pair.RemoteCandidateID] = struct{}{}
} }
} }
iceCandidatesUsed := []webrtc.ICECandidateStats{}
for id := range nominatedRemoteCandidates {
if candidate, ok := remoteCandidates[id]; ok {
iceCandidatesUsed = append(iceCandidatesUsed, candidate)
}
}
manager.metrics.SetICECandidatesUsed(session, iceCandidatesUsed)
} }
}() }()

View File

@ -17,7 +17,11 @@ type metrics struct {
iceCandidates map[string]struct{} iceCandidates map[string]struct{}
iceCandidatesMu *sync.Mutex iceCandidatesMu *sync.Mutex
iceCandidatesCount prometheus.Counter iceCandidatesUdpCount prometheus.Counter
iceCandidatesTcpCount prometheus.Counter
iceCandidatesUsedUdp prometheus.Gauge
iceCandidatesUsedTcp prometheus.Gauge
videoIds map[string]prometheus.Gauge videoIds map[string]prometheus.Gauge
videoIdsMu *sync.Mutex videoIdsMu *sync.Mutex
@ -86,13 +90,45 @@ func (m *metricsCtx) getBySession(session types.Session) metrics {
iceCandidates: map[string]struct{}{}, iceCandidates: map[string]struct{}{},
iceCandidatesMu: &sync.Mutex{}, iceCandidatesMu: &sync.Mutex{},
iceCandidatesCount: promauto.NewCounter(prometheus.CounterOpts{ iceCandidatesUdpCount: promauto.NewCounter(prometheus.CounterOpts{
Name: "ice_candidates_count", Name: "ice_candidates_count",
Namespace: "neko", Namespace: "neko",
Subsystem: "webrtc", Subsystem: "webrtc",
Help: "Count of ICE candidates sent by a remote client.", Help: "Count of ICE candidates sent by a remote client.",
ConstLabels: map[string]string{ ConstLabels: map[string]string{
"session_id": session.ID(), "session_id": session.ID(),
"protocol": "udp",
},
}),
iceCandidatesTcpCount: promauto.NewCounter(prometheus.CounterOpts{
Name: "ice_candidates_count",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Count of ICE candidates sent by a remote client.",
ConstLabels: map[string]string{
"session_id": session.ID(),
"protocol": "tcp",
},
}),
iceCandidatesUsedUdp: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ice_candidates_used",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Used ICE candidates that are currently in use.",
ConstLabels: map[string]string{
"session_id": session.ID(),
"protocol": "udp",
},
}),
iceCandidatesUsedTcp: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ice_candidates_used",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Used ICE candidates that are currently in use.",
ConstLabels: map[string]string{
"session_id": session.ID(),
"protocol": "tcp",
}, },
}), }),
@ -191,6 +227,9 @@ func (m *metricsCtx) reset(met metrics) {
} }
met.videoIdsMu.Unlock() met.videoIdsMu.Unlock()
met.iceCandidatesUsedUdp.Set(float64(0))
met.iceCandidatesUsedTcp.Set(float64(0))
met.receiverEstimatedMaximumBitrate.Set(0) met.receiverEstimatedMaximumBitrate.Set(0)
met.receiverReportDelay.Set(0) met.receiverReportDelay.Set(0)
@ -202,18 +241,38 @@ func (m *metricsCtx) NewConnection(session types.Session) {
met.connectionCount.Add(1) met.connectionCount.Add(1)
} }
func (m *metricsCtx) NewICECandidate(session types.Session, id string) { func (m *metricsCtx) NewICECandidate(session types.Session, candidate webrtc.ICECandidateStats) {
met := m.getBySession(session) met := m.getBySession(session)
met.iceCandidatesMu.Lock() met.iceCandidatesMu.Lock()
defer met.iceCandidatesMu.Unlock() defer met.iceCandidatesMu.Unlock()
if _, found := met.iceCandidates[id]; found { if _, found := met.iceCandidates[candidate.ID]; found {
return return
} }
met.iceCandidates[id] = struct{}{} met.iceCandidates[candidate.ID] = struct{}{}
met.iceCandidatesCount.Add(1) if candidate.Protocol == "udp" {
met.iceCandidatesUdpCount.Add(1)
} else if candidate.Protocol == "tcp" {
met.iceCandidatesTcpCount.Add(1)
}
}
func (m *metricsCtx) SetICECandidatesUsed(session types.Session, candidates []webrtc.ICECandidateStats) {
met := m.getBySession(session)
udp, tcp := 0, 0
for _, candidate := range candidates {
if candidate.Protocol == "udp" {
udp++
} else if candidate.Protocol == "tcp" {
tcp++
}
}
met.iceCandidatesUsedUdp.Set(float64(udp))
met.iceCandidatesUsedTcp.Set(float64(tcp))
} }
func (m *metricsCtx) SetState(session types.Session, state webrtc.PeerConnectionState) { func (m *metricsCtx) SetState(session types.Session, state webrtc.PeerConnectionState) {