From 3fda00bac50a735062f059292f8c0d2a2e606605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 3 Jul 2022 23:21:25 +0200 Subject: [PATCH] add video id for webrtc metrics. --- internal/webrtc/manager.go | 3 +++ internal/webrtc/metrics.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index c6dec0c0..e3e454a1 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -138,6 +138,8 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin return nil, types.ErrWebRTCVideoNotFound } + manager.metrics.SetVideoID(session, videoID) + connection, err := manager.newPeerConnection([]codec.RTPCodec{ audioStream.Codec(), videoStream.Codec(), @@ -201,6 +203,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin return types.ErrWebRTCVideoNotFound } + manager.metrics.SetVideoID(session, videoID) return videoTrack.SetStream(videoStream) }, setPaused: func(isPaused bool) { diff --git a/internal/webrtc/metrics.go b/internal/webrtc/metrics.go index 42efa7ae..14c99def 100644 --- a/internal/webrtc/metrics.go +++ b/internal/webrtc/metrics.go @@ -18,6 +18,9 @@ type metrics struct { iceCandidatesMu *sync.Mutex iceCandidatesCount prometheus.Counter + videoIds map[string]prometheus.Gauge + videoIdsMu *sync.Mutex + iceBytesSent prometheus.Gauge iceBytesReceived prometheus.Gauge sctpBytesSent prometheus.Gauge @@ -86,6 +89,9 @@ func (m *metricsCtx) getBySession(session types.Session) metrics { }, }), + videoIds: map[string]prometheus.Gauge{}, + videoIdsMu: &sync.Mutex{}, + iceBytesSent: promauto.NewGauge(prometheus.GaugeOpts{ Name: "ice_bytes_sent", Namespace: "neko", @@ -171,6 +177,34 @@ func (m *metricsCtx) SetState(session types.Session, state webrtc.PeerConnection met.connectionStateCount.Add(1) } +func (m *metricsCtx) SetVideoID(session types.Session, videoId string) { + met := m.getBySession(session) + + met.videoIdsMu.Lock() + defer met.videoIdsMu.Unlock() + + if _, found := met.videoIds[videoId]; !found { + met.videoIds[videoId] = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "video_id", + Namespace: "neko", + Subsystem: "webrtc", + Help: "Current Video ID of a session.", + ConstLabels: map[string]string{ + "session_id": session.ID(), + "video_id": videoId, + }, + }) + } + + for id, entry := range met.videoIds { + if id == videoId { + entry.Set(1) + } else { + entry.Set(0) + } + } +} + func (m *metricsCtx) SetIceTransportStats(session types.Session, data webrtc.TransportStats) { met := m.getBySession(session)