Telemetry refactor (#172)

* telemetry refactor

* fix imports

* update protocol
This commit is contained in:
David Colburn
2021-11-08 20:00:34 -06:00
committed by GitHub
parent 8344466629
commit 289ebd32ff
25 changed files with 597 additions and 511 deletions
+86
View File
@@ -0,0 +1,86 @@
package prometheus
import (
"sync/atomic"
"time"
livekit "github.com/livekit/protocol/proto"
"github.com/prometheus/client_golang/prometheus"
)
const livekitNamespace = "livekit"
var (
MessageCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "node",
Name: "messages",
},
[]string{"type", "status"},
)
ServiceOperationCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "node",
Name: "service_operation",
},
[]string{"type", "status", "error_type"},
)
)
func init() {
prometheus.MustRegister(MessageCounter)
prometheus.MustRegister(ServiceOperationCounter)
initPacketStats()
initRoomStats()
}
func UpdateCurrentNodeStats(nodeStats *livekit.NodeStats) error {
updatedAtPrevious := nodeStats.UpdatedAt
nodeStats.UpdatedAt = time.Now().Unix()
secondsSinceLastUpdate := nodeStats.UpdatedAt - updatedAtPrevious
err := updateCurrentNodeSystemStats(nodeStats)
updateCurrentNodeRoomStats(nodeStats)
updateCurrentNodePacketStats(nodeStats, secondsSinceLastUpdate)
return err
}
func updateCurrentNodeRoomStats(nodeStats *livekit.NodeStats) {
nodeStats.NumClients = atomic.LoadInt32(&atomicParticipantTotal)
nodeStats.NumRooms = atomic.LoadInt32(&atomicRoomTotal)
nodeStats.NumTracksIn = atomic.LoadInt32(&atomicTrackPublishedTotal)
nodeStats.NumTracksOut = atomic.LoadInt32(&atomicTrackSubscribedTotal)
}
func updateCurrentNodePacketStats(nodeStats *livekit.NodeStats, secondsSinceLastUpdate int64) {
if secondsSinceLastUpdate == 0 {
return
}
bytesInPrevious := nodeStats.BytesIn
bytesOutPrevious := nodeStats.BytesOut
packetsInPrevious := nodeStats.PacketsIn
packetsOutPrevious := nodeStats.PacketsOut
nackTotalPrevious := nodeStats.NackTotal
nodeStats.BytesIn = atomic.LoadUint64(&atomicBytesIn)
nodeStats.BytesOut = atomic.LoadUint64(&atomicBytesOut)
nodeStats.PacketsIn = atomic.LoadUint64(&atomicPacketsIn)
nodeStats.PacketsOut = atomic.LoadUint64(&atomicPacketsOut)
nodeStats.NackTotal = atomic.LoadUint64(&atomicNackTotal)
nodeStats.BytesInPerSec = perSec(bytesInPrevious, nodeStats.BytesIn, secondsSinceLastUpdate)
nodeStats.BytesOutPerSec = perSec(bytesOutPrevious, nodeStats.BytesOut, secondsSinceLastUpdate)
nodeStats.PacketsInPerSec = perSec(packetsInPrevious, nodeStats.PacketsIn, secondsSinceLastUpdate)
nodeStats.PacketsOutPerSec = perSec(packetsOutPrevious, nodeStats.PacketsOut, secondsSinceLastUpdate)
nodeStats.NackPerSec = perSec(nackTotalPrevious, nodeStats.NackTotal, secondsSinceLastUpdate)
}
func perSec(prev, curr uint64, secs int64) float32 {
return float32(curr-prev) / float32(secs)
}
+28
View File
@@ -0,0 +1,28 @@
//go:build linux
// +build linux
package prometheus
import (
linuxproc "github.com/c9s/goprocinfo/linux"
livekit "github.com/livekit/protocol/proto"
)
func updateCurrentNodeSystemStats(nodeStats *livekit.NodeStats) error {
cpuInfo, err := linuxproc.ReadCPUInfo("/proc/cpuinfo")
if err != nil {
return err
}
loadAvg, err := linuxproc.ReadLoadAvg("/proc/loadavg")
if err != nil {
return err
}
nodeStats.NumCpus = uint32(cpuInfo.NumCPU())
nodeStats.LoadAvgLast1Min = float32(loadAvg.Last1Min)
nodeStats.LoadAvgLast5Min = float32(loadAvg.Last5Min)
nodeStats.LoadAvgLast15Min = float32(loadAvg.Last15Min)
return nil
}
+10
View File
@@ -0,0 +1,10 @@
//go:build !linux
// +build !linux
package prometheus
import livekit "github.com/livekit/protocol/proto"
func updateCurrentNodeSystemStats(nodeStats *livekit.NodeStats) error {
return nil
}
+83
View File
@@ -0,0 +1,83 @@
package prometheus
import (
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
)
type Direction string
const (
Incoming Direction = "incoming"
Outgoing Direction = "outgoing"
)
var (
atomicBytesIn uint64
atomicBytesOut uint64
atomicPacketsIn uint64
atomicPacketsOut uint64
atomicNackTotal uint64
promPacketLabels = []string{"direction"}
promPacketTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "packet",
Name: "total",
}, promPacketLabels)
promPacketBytes = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "packet",
Name: "bytes",
}, promPacketLabels)
promNackTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "nack",
Name: "total",
}, promPacketLabels)
promPliTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "pli",
Name: "total",
}, promPacketLabels)
promFirTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "fir",
Name: "total",
}, promPacketLabels)
)
func initPacketStats() {
prometheus.MustRegister(promPacketTotal)
prometheus.MustRegister(promPacketBytes)
prometheus.MustRegister(promNackTotal)
prometheus.MustRegister(promPliTotal)
prometheus.MustRegister(promFirTotal)
}
func IncrementPackets(direction Direction, pktLen uint64) {
promPacketTotal.WithLabelValues(string(direction)).Add(1)
promPacketBytes.WithLabelValues(string(direction)).Add(float64(pktLen))
if direction == Incoming {
atomic.AddUint64(&atomicPacketsIn, 1)
atomic.AddUint64(&atomicBytesIn, pktLen)
} else {
atomic.AddUint64(&atomicPacketsOut, 1)
atomic.AddUint64(&atomicBytesOut, pktLen)
}
}
func IncrementNack(direction Direction) {
promNackTotal.WithLabelValues(string(direction)).Add(1)
atomic.AddUint64(&atomicNackTotal, 1)
}
func IncrementPLI(direction Direction) {
promPliTotal.WithLabelValues(string(direction)).Add(1)
}
func IncrementFIR(direction Direction) {
promFirTotal.WithLabelValues(string(direction)).Add(1)
}
+95
View File
@@ -0,0 +1,95 @@
package prometheus
import (
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
atomicRoomTotal int32
atomicParticipantTotal int32
atomicTrackPublishedTotal int32
atomicTrackSubscribedTotal 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 initRoomStats() {
prometheus.MustRegister(promRoomTotal)
prometheus.MustRegister(promRoomDuration)
prometheus.MustRegister(promParticipantTotal)
prometheus.MustRegister(promTrackPublishedTotal)
prometheus.MustRegister(promTrackSubscribedTotal)
}
func RoomStarted() {
promRoomTotal.Add(1)
atomic.AddInt32(&atomicRoomTotal, 1)
}
func RoomEnded(startedAt time.Time) {
if !startedAt.IsZero() {
promRoomDuration.Observe(float64(time.Now().Sub(startedAt)) / float64(time.Second))
}
promRoomTotal.Sub(1)
atomic.AddInt32(&atomicRoomTotal, -1)
}
func AddParticipant() {
promParticipantTotal.Add(1)
atomic.AddInt32(&atomicParticipantTotal, 1)
}
func SubParticipant() {
promParticipantTotal.Sub(1)
atomic.AddInt32(&atomicParticipantTotal, -1)
}
func AddPublishedTrack(kind string) {
promTrackPublishedTotal.WithLabelValues(kind).Add(1)
atomic.AddInt32(&atomicTrackPublishedTotal, 1)
}
func SubPublishedTrack(kind string) {
promTrackPublishedTotal.WithLabelValues(kind).Sub(1)
atomic.AddInt32(&atomicTrackPublishedTotal, -1)
}
func AddSubscribedTrack(kind string) {
promTrackSubscribedTotal.WithLabelValues(kind).Add(1)
atomic.AddInt32(&atomicTrackSubscribedTotal, 1)
}
func SubSubscribedTrack(kind string) {
promTrackSubscribedTotal.WithLabelValues(kind).Sub(1)
atomic.AddInt32(&atomicTrackSubscribedTotal, -1)
}