list-nodes command action (#87)
* list-nodes command action * refactor stats into a package * store existing stats in redis, more refactoring * dz review: atomic load
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
promPacketTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "packet",
|
||||
Name: "total",
|
||||
}, promLabels)
|
||||
promPacketBytes = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "packet",
|
||||
Name: "bytes",
|
||||
}, promLabels)
|
||||
promNackTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "nack",
|
||||
Name: "total",
|
||||
}, promLabels)
|
||||
promPliTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "pli",
|
||||
Name: "total",
|
||||
}, promLabels)
|
||||
promFirTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "fir",
|
||||
Name: "total",
|
||||
}, promLabels)
|
||||
)
|
||||
|
||||
func initPacketStats() {
|
||||
prometheus.MustRegister(promPacketTotal)
|
||||
prometheus.MustRegister(promPacketBytes)
|
||||
prometheus.MustRegister(promNackTotal)
|
||||
prometheus.MustRegister(promPliTotal)
|
||||
prometheus.MustRegister(promFirTotal)
|
||||
}
|
||||
|
||||
type PacketStats struct {
|
||||
roomName string
|
||||
direction string // incoming or outgoing
|
||||
|
||||
PacketBytes uint64 `json:"packetBytes"`
|
||||
PacketTotal uint64 `json:"packetTotal"`
|
||||
NackTotal uint64 `json:"nackTotal"`
|
||||
PLITotal uint64 `json:"pliTotal"`
|
||||
FIRTotal uint64 `json:"firTotal"`
|
||||
}
|
||||
|
||||
func newPacketStats(room, direction string) *PacketStats {
|
||||
return &PacketStats{
|
||||
roomName: room,
|
||||
direction: direction,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PacketStats) IncrementBytes(bytes uint64) {
|
||||
promPacketBytes.WithLabelValues(s.direction).Add(float64(bytes))
|
||||
atomic.AddUint64(&s.PacketBytes, bytes)
|
||||
}
|
||||
|
||||
func (s *PacketStats) IncrementPackets(count uint64) {
|
||||
promPacketTotal.WithLabelValues(s.direction).Add(float64(count))
|
||||
atomic.AddUint64(&s.PacketTotal, count)
|
||||
}
|
||||
|
||||
func (s *PacketStats) IncrementNack(count uint64) {
|
||||
promNackTotal.WithLabelValues(s.direction).Add(float64(count))
|
||||
atomic.AddUint64(&s.NackTotal, count)
|
||||
}
|
||||
|
||||
func (s *PacketStats) IncrementPLI(count uint64) {
|
||||
promPliTotal.WithLabelValues(s.direction).Add(float64(count))
|
||||
atomic.AddUint64(&s.PLITotal, count)
|
||||
}
|
||||
|
||||
func (s *PacketStats) IncrementFIR(count uint64) {
|
||||
promFirTotal.WithLabelValues(s.direction).Add(float64(count))
|
||||
atomic.AddUint64(&s.FIRTotal, count)
|
||||
}
|
||||
|
||||
func (s *PacketStats) HandleRTCP(pkts []rtcp.Packet) {
|
||||
for _, rtcpPacket := range pkts {
|
||||
switch rtcpPacket.(type) {
|
||||
case *rtcp.TransportLayerNack:
|
||||
s.IncrementNack(1)
|
||||
case *rtcp.PictureLossIndication:
|
||||
s.IncrementPLI(1)
|
||||
case *rtcp.FullIntraRequest:
|
||||
s.IncrementFIR(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s PacketStats) Copy() *PacketStats {
|
||||
return &PacketStats{
|
||||
roomName: s.roomName,
|
||||
direction: s.direction,
|
||||
PacketBytes: atomic.LoadUint64(&s.PacketBytes),
|
||||
PacketTotal: atomic.LoadUint64(&s.PacketTotal),
|
||||
NackTotal: atomic.LoadUint64(&s.NackTotal),
|
||||
PLITotal: atomic.LoadUint64(&s.PLITotal),
|
||||
FIRTotal: atomic.LoadUint64(&s.FIRTotal),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
roomTotal int32
|
||||
participantTotal int32
|
||||
trackPublishedTotal int32
|
||||
trackSubscribedTotal int32
|
||||
|
||||
promRoomTotal = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "room",
|
||||
Name: "total",
|
||||
})
|
||||
promRoomDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "room",
|
||||
Name: "duration_seconds",
|
||||
Buckets: []float64{
|
||||
5, 10, 60, 5 * 60, 10 * 60, 30 * 60, 60 * 60, 2 * 60 * 60, 5 * 60 * 60, 10 * 60 * 60,
|
||||
},
|
||||
})
|
||||
promParticipantTotal = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "participant",
|
||||
Name: "total",
|
||||
})
|
||||
promTrackPublishedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "track",
|
||||
Name: "published_total",
|
||||
}, []string{"kind"})
|
||||
promTrackSubscribedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: livekitNamespace,
|
||||
Subsystem: "track",
|
||||
Name: "subscribed_total",
|
||||
}, []string{"kind"})
|
||||
)
|
||||
|
||||
func initRoomStatsReporter() {
|
||||
prometheus.MustRegister(promRoomTotal)
|
||||
prometheus.MustRegister(promRoomDuration)
|
||||
prometheus.MustRegister(promParticipantTotal)
|
||||
prometheus.MustRegister(promTrackPublishedTotal)
|
||||
prometheus.MustRegister(promTrackSubscribedTotal)
|
||||
}
|
||||
|
||||
// RoomStatsReporter is created for each room
|
||||
type RoomStatsReporter struct {
|
||||
roomName string
|
||||
startedAt time.Time
|
||||
Incoming *PacketStats
|
||||
Outgoing *PacketStats
|
||||
}
|
||||
|
||||
func NewRoomStatsReporter(roomName string) *RoomStatsReporter {
|
||||
return &RoomStatsReporter{
|
||||
roomName: roomName,
|
||||
Incoming: newPacketStats(roomName, "incoming"),
|
||||
Outgoing: newPacketStats(roomName, "outgoing"),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) RoomStarted() {
|
||||
r.startedAt = time.Now()
|
||||
promRoomTotal.Add(1)
|
||||
atomic.AddInt32(&roomTotal, 1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) RoomEnded() {
|
||||
if !r.startedAt.IsZero() {
|
||||
promRoomDuration.Observe(float64(time.Now().Sub(r.startedAt)) / float64(time.Second))
|
||||
}
|
||||
promRoomTotal.Sub(1)
|
||||
atomic.AddInt32(&roomTotal, -1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) AddParticipant() {
|
||||
promParticipantTotal.Add(1)
|
||||
atomic.AddInt32(&participantTotal, 1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) SubParticipant() {
|
||||
promParticipantTotal.Sub(1)
|
||||
atomic.AddInt32(&participantTotal, -1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) AddPublishedTrack(kind string) {
|
||||
promTrackPublishedTotal.WithLabelValues(kind).Add(1)
|
||||
atomic.AddInt32(&trackPublishedTotal, 1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) SubPublishedTrack(kind string) {
|
||||
promTrackPublishedTotal.WithLabelValues(kind).Sub(1)
|
||||
atomic.AddInt32(&trackPublishedTotal, -1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) AddSubscribedTrack(kind string) {
|
||||
promTrackSubscribedTotal.WithLabelValues(kind).Add(1)
|
||||
atomic.AddInt32(&trackSubscribedTotal, 1)
|
||||
}
|
||||
|
||||
func (r *RoomStatsReporter) SubSubscribedTrack(kind string) {
|
||||
promTrackSubscribedTotal.WithLabelValues(kind).Sub(1)
|
||||
atomic.AddInt32(&trackSubscribedTotal, -1)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
"github.com/pion/interceptor"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/transport/packetio"
|
||||
)
|
||||
|
||||
const livekitNamespace = "livekit"
|
||||
|
||||
var (
|
||||
promLabels = []string{"direction"}
|
||||
)
|
||||
|
||||
func init() {
|
||||
initPacketStats()
|
||||
initRoomStatsReporter()
|
||||
}
|
||||
|
||||
// StatsBufferWrapper wraps a buffer factory so we could get information on
|
||||
// incoming packets
|
||||
type StatsBufferWrapper struct {
|
||||
CreateBufferFunc func(packetType packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser
|
||||
Stats *PacketStats
|
||||
}
|
||||
|
||||
func (w *StatsBufferWrapper) CreateBuffer(packetType packetio.BufferPacketType, ssrc uint32) io.ReadWriteCloser {
|
||||
writer := w.CreateBufferFunc(packetType, ssrc)
|
||||
if packetType == packetio.RTPBufferPacket {
|
||||
// wrap this in a counter class
|
||||
return &rtpReporterWriter{
|
||||
ReadWriteCloser: writer,
|
||||
stats: w.Stats,
|
||||
}
|
||||
}
|
||||
return writer
|
||||
}
|
||||
|
||||
type rtpReporterWriter struct {
|
||||
io.ReadWriteCloser
|
||||
stats *PacketStats
|
||||
}
|
||||
|
||||
func (w *rtpReporterWriter) Write(p []byte) (n int, err error) {
|
||||
w.stats.IncrementPackets(1)
|
||||
w.stats.IncrementBytes(uint64(len(p)))
|
||||
return w.ReadWriteCloser.Write(p)
|
||||
}
|
||||
|
||||
// StatsInterceptor is created for each participant to keep of track of outgoing stats
|
||||
// it adheres to Pion interceptor interface
|
||||
type StatsInterceptor struct {
|
||||
interceptor.NoOp
|
||||
reporter *RoomStatsReporter
|
||||
}
|
||||
|
||||
func NewStatsInterceptor(reporter *RoomStatsReporter) *StatsInterceptor {
|
||||
return &StatsInterceptor{
|
||||
reporter: reporter,
|
||||
}
|
||||
}
|
||||
|
||||
// BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method
|
||||
// will be called once per packet batch.
|
||||
func (s *StatsInterceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter {
|
||||
return interceptor.RTCPWriterFunc(func(pkts []rtcp.Packet, attributes interceptor.Attributes) (int, error) {
|
||||
s.reporter.Outgoing.HandleRTCP(pkts)
|
||||
return writer.Write(pkts, attributes)
|
||||
})
|
||||
}
|
||||
|
||||
// BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method
|
||||
// will be called once per rtp packet.
|
||||
func (s *StatsInterceptor) BindLocalStream(_ *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter {
|
||||
return interceptor.RTPWriterFunc(func(header *rtp.Header, payload []byte, attributes interceptor.Attributes) (int, error) {
|
||||
s.reporter.Outgoing.IncrementPackets(1)
|
||||
s.reporter.Outgoing.IncrementBytes(uint64(len(payload)))
|
||||
return writer.Write(header, payload, attributes)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateCurrentNodeStats(nodeStats *livekit.NodeStats) {
|
||||
nodeStats.NumClients = uint32(atomic.LoadInt32(&participantTotal))
|
||||
nodeStats.NumRooms = uint32(atomic.LoadInt32(&roomTotal))
|
||||
nodeStats.NumTracksIn = uint32(atomic.LoadInt32(&trackPublishedTotal))
|
||||
nodeStats.NumTracksOut = uint32(atomic.LoadInt32(&trackSubscribedTotal))
|
||||
|
||||
nodeStats.UpdatedAt = time.Now().Unix()
|
||||
}
|
||||
Reference in New Issue
Block a user