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,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