neko/internal/webrtc/metrics.go

373 lines
9.9 KiB
Go
Raw Normal View History

2022-06-26 06:12:42 +12:00
package webrtc
import (
"sync"
"github.com/demodesk/neko/pkg/types"
2022-07-04 11:01:03 +12:00
"github.com/pion/rtcp"
2022-06-26 06:12:42 +12:00
"github.com/pion/webrtc/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
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
iceCandidates map[string]struct{}
iceCandidatesMu *sync.Mutex
iceCandidatesUdpCount prometheus.Counter
iceCandidatesTcpCount prometheus.Counter
iceCandidatesUsedUdp prometheus.Gauge
iceCandidatesUsedTcp prometheus.Gauge
2022-07-01 09:50:47 +12:00
2022-07-04 09:21:25 +12:00
videoIds map[string]prometheus.Gauge
videoIdsMu *sync.Mutex
2022-07-04 10:38:46 +12:00
receiverEstimatedMaximumBitrate prometheus.Gauge
receiverEstimatedTargetBitrate prometheus.Gauge
2022-07-04 10:38:46 +12:00
2022-07-04 11:01:03 +12:00
receiverReportDelay prometheus.Gauge
receiverReportJitter prometheus.Gauge
receiverReportTotalLost prometheus.Gauge
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{},
iceCandidatesUdpCount: promauto.NewCounter(prometheus.CounterOpts{
2022-07-01 09:50:47 +12:00
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": "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",
2022-07-01 09:50:47 +12:00
},
}),
2022-07-04 09:21:25 +12:00
videoIds: map[string]prometheus.Gauge{},
videoIdsMu: &sync.Mutex{},
2022-07-04 10:38:46 +12:00
receiverEstimatedMaximumBitrate: promauto.NewGauge(prometheus.GaugeOpts{
Name: "receiver_estimated_maximum_bitrate",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Receiver Estimated Maximum Bitrate from SCTP.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
receiverEstimatedTargetBitrate: promauto.NewGauge(prometheus.GaugeOpts{
Name: "receiver_estimated_target_bitrate",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Receiver Estimated Target Bitrate using Google's congestion control.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-07-04 10:38:46 +12:00
2022-07-04 11:01:03 +12:00
receiverReportDelay: promauto.NewGauge(prometheus.GaugeOpts{
Name: "receiver_report_delay",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Receiver Report Delay from SCTP, expressed in units of 1/65536 seconds.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
receiverReportJitter: promauto.NewGauge(prometheus.GaugeOpts{
Name: "receiver_report_jitter",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Receiver Report Jitter from SCTP.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
receiverReportTotalLost: promauto.NewGauge(prometheus.GaugeOpts{
Name: "receiver_report_total_lost",
Namespace: "neko",
Subsystem: "webrtc",
Help: "Receiver Report Total Lost from SCTP.",
ConstLabels: map[string]string{
"session_id": session.ID(),
},
}),
2022-07-01 10:16:39 +12:00
iceBytesSent: promauto.NewGauge(prometheus.GaugeOpts{
2022-07-04 10:48:29 +12:00
Name: "bytes_sent",
2022-06-26 06:12:42 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-04 10:48:29 +12:00
Help: "Sent bytes to a session.",
2022-06-26 06:12:42 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
2022-07-04 10:48:29 +12:00
"transport": "ice",
2022-06-26 06:12:42 +12:00
},
}),
2022-07-01 10:16:39 +12:00
iceBytesReceived: promauto.NewGauge(prometheus.GaugeOpts{
2022-07-04 10:48:29 +12:00
Name: "bytes_received",
2022-06-26 06:12:42 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-04 10:48:29 +12:00
Help: "Received bytes from a session.",
2022-07-01 10:16:39 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
2022-07-04 10:48:29 +12:00
"transport": "ice",
2022-07-01 10:16:39 +12:00
},
}),
sctpBytesSent: promauto.NewGauge(prometheus.GaugeOpts{
2022-07-04 10:48:29 +12:00
Name: "bytes_sent",
2022-07-01 10:16:39 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-04 10:48:29 +12:00
Help: "Sent bytes to a session.",
2022-07-01 10:16:39 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
2022-07-04 10:48:29 +12:00
"transport": "sctp",
2022-07-01 10:16:39 +12:00
},
}),
sctpBytesReceived: promauto.NewGauge(prometheus.GaugeOpts{
2022-07-04 10:48:29 +12:00
Name: "bytes_received",
2022-07-01 10:16:39 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-04 10:48:29 +12:00
Help: "Received bytes from a session.",
2022-06-26 06:12:42 +12:00
ConstLabels: map[string]string{
"session_id": session.ID(),
2022-07-04 10:48:29 +12:00
"transport": "sctp",
2022-06-26 06:12:42 +12:00
},
}),
}
m.sessions[session.ID()] = met
return met
}
2022-07-05 03:47:04 +12:00
func (m *metricsCtx) reset(met metrics) {
met.videoIdsMu.Lock()
for _, entry := range met.videoIds {
entry.Set(0)
}
met.videoIdsMu.Unlock()
met.iceCandidatesUsedUdp.Set(float64(0))
met.iceCandidatesUsedTcp.Set(float64(0))
2022-07-05 03:47:04 +12:00
met.receiverEstimatedMaximumBitrate.Set(0)
met.receiverReportDelay.Set(0)
met.receiverReportJitter.Set(0)
}
2022-06-26 06:12:42 +12:00
func (m *metricsCtx) NewConnection(session types.Session) {
met := m.getBySession(session)
met.connectionCount.Add(1)
}
func (m *metricsCtx) NewICECandidate(session types.Session, candidate webrtc.ICECandidateStats) {
2022-07-01 09:50:47 +12:00
met := m.getBySession(session)
2022-07-04 09:06:56 +12:00
met.iceCandidatesMu.Lock()
defer met.iceCandidatesMu.Unlock()
if _, found := met.iceCandidates[candidate.ID]; found {
2022-07-01 09:50:47 +12:00
return
}
met.iceCandidates[candidate.ID] = struct{}{}
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))
2022-07-01 09:50:47 +12:00
}
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)
2022-07-05 03:47:04 +12:00
m.reset(met)
2022-06-26 06:12:42 +12:00
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{
2022-07-05 04:23:35 +12:00
Name: "video_listeners",
2022-07-04 09:21:25 +12:00
Namespace: "neko",
Subsystem: "webrtc",
2022-07-05 04:23:35 +12:00
Help: "Listeners for Video pipelines by a session.",
2022-07-04 09:21:25 +12:00
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) SetReceiverEstimatedMaximumBitrate(session types.Session, bitrate float32) {
met := m.getBySession(session)
met.receiverEstimatedMaximumBitrate.Set(float64(bitrate))
}
func (m *metricsCtx) SetReceiverEstimatedTargetBitrate(session types.Session, bitrate float64) {
2022-07-04 10:38:46 +12:00
met := m.getBySession(session)
met.receiverEstimatedTargetBitrate.Set(bitrate)
2022-07-04 10:38:46 +12:00
}
2022-07-04 11:01:03 +12:00
func (m *metricsCtx) SetReceiverReport(session types.Session, report rtcp.ReceptionReport) {
met := m.getBySession(session)
met.receiverReportDelay.Set(float64(report.Delay))
met.receiverReportJitter.Set(float64(report.Jitter))
met.receiverReportTotalLost.Set(float64(report.TotalLost))
}
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
}