neko/internal/webrtc/metrics.go

221 lines
5.5 KiB
Go
Raw Normal View History

2022-06-26 06:12:42 +12:00
package webrtc
import (
"sync"
"github.com/pion/webrtc/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gitlab.com/demodesk/neko/server/pkg/types"
)
type metrics struct {
2022-07-01 09:54:06 +12:00
connectionState prometheus.Gauge
connectionStateCount prometheus.Counter
connectionCount prometheus.Counter
2022-06-26 06:12:42 +12:00
2022-07-01 09:50:47 +12:00
iceCandidates map[string]struct{}
2022-07-04 09:06:56 +12:00
iceCandidatesMu *sync.Mutex
2022-07-01 09:50:47 +12:00
iceCandidatesCount prometheus.Counter
2022-07-04 09:21:25 +12:00
videoIds map[string]prometheus.Gauge
videoIdsMu *sync.Mutex
2022-07-01 10:16:39 +12:00
iceBytesSent prometheus.Gauge
iceBytesReceived prometheus.Gauge
sctpBytesSent prometheus.Gauge
sctpBytesReceived prometheus.Gauge
2022-06-26 06:12:42 +12:00
}
type metricsCtx struct {
mu sync.Mutex
sessions map[string]metrics
}
func newMetrics() *metricsCtx {
return &metricsCtx{
sessions: map[string]metrics{},
}
}
func (m *metricsCtx) getBySession(session types.Session) metrics {
m.mu.Lock()
defer m.mu.Unlock()
met, ok := m.sessions[session.ID()]
if ok {
return met
}
met = metrics{
connectionState: promauto.NewGauge(prometheus.GaugeOpts{
Name: "connection_state",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Connection state of session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-07-01 09:54:06 +12:00
connectionStateCount: promauto.NewCounter(prometheus.CounterOpts{
Name: "connection_state_count",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Count of connection state changes for a session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-06-26 06:12:42 +12:00
connectionCount: promauto.NewCounter(prometheus.CounterOpts{
Name: "connection_count",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Connection count of a session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-07-01 09:50:47 +12:00
2022-07-04 09:06:56 +12:00
iceCandidates: map[string]struct{}{},
iceCandidatesMu: &sync.Mutex{},
2022-07-01 09:50:47 +12:00
iceCandidatesCount: 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(),
},
}),
2022-07-04 09:21:25 +12:00
videoIds: map[string]prometheus.Gauge{},
videoIdsMu: &sync.Mutex{},
2022-07-01 10:16:39 +12:00
iceBytesSent: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ice_bytes_sent",
2022-06-26 06:12:42 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-01 10:16:39 +12:00
Help: "Sent bytes using ICE transport to a session.",
2022-06-26 06:12:42 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-07-01 10:16:39 +12:00
iceBytesReceived: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ice_bytes_received",
2022-06-26 06:12:42 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-01 10:16:39 +12:00
Help: "Received bytes using ICE transport from a session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
sctpBytesSent: promauto.NewGauge(prometheus.GaugeOpts{
Name: "sctp_bytes_sent",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Sent bytes using SCTP transport to a session.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
sctpBytesReceived: promauto.NewGauge(prometheus.GaugeOpts{
Name: "sctp_bytes_received",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Received bytes using SCTP transport from a session.",
2022-06-26 06:12:42 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
}
m.sessions[session.ID()] = met
return met
}
func (m *metricsCtx) NewConnection(session types.Session) {
met := m.getBySession(session)
met.connectionCount.Add(1)
}
2022-07-01 09:50:47 +12:00
func (m *metricsCtx) NewICECandidate(session types.Session, id string) {
met := m.getBySession(session)
2022-07-04 09:06:56 +12:00
met.iceCandidatesMu.Lock()
defer met.iceCandidatesMu.Unlock()
2022-07-01 09:50:47 +12:00
if _, found := met.iceCandidates[id]; found {
return
}
met.iceCandidates[id] = struct{}{}
met.iceCandidatesCount.Add(1)
}
2022-06-26 06:12:42 +12:00
func (m *metricsCtx) SetState(session types.Session, state webrtc.PeerConnectionState) {
met := m.getBySession(session)
switch state {
case webrtc.PeerConnectionStateNew:
met.connectionState.Set(0)
case webrtc.PeerConnectionStateConnecting:
met.connectionState.Set(4)
case webrtc.PeerConnectionStateConnected:
met.connectionState.Set(5)
case webrtc.PeerConnectionStateDisconnected:
met.connectionState.Set(3)
case webrtc.PeerConnectionStateFailed:
met.connectionState.Set(2)
case webrtc.PeerConnectionStateClosed:
met.connectionState.Set(1)
default:
met.connectionState.Set(-1)
}
2022-07-01 09:54:06 +12:00
met.connectionStateCount.Add(1)
2022-06-26 06:12:42 +12:00
}
2022-07-04 09:21:25 +12:00
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)
}
}
}
2022-07-01 10:16:39 +12:00
func (m *metricsCtx) SetIceTransportStats(session types.Session, data webrtc.TransportStats) {
met := m.getBySession(session)
met.iceBytesSent.Set(float64(data.BytesSent))
met.iceBytesReceived.Set(float64(data.BytesReceived))
}
func (m *metricsCtx) SetSctpTransportStats(session types.Session, data webrtc.TransportStats) {
2022-06-26 06:12:42 +12:00
met := m.getBySession(session)
2022-07-01 10:16:39 +12:00
met.sctpBytesSent.Set(float64(data.BytesSent))
met.sctpBytesReceived.Set(float64(data.BytesReceived))
2022-06-26 06:12:42 +12:00
}