Replace sync/atomic usage with uber/atomic (#471)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
@@ -54,29 +53,29 @@ func GetUpdatedNodeStats(prev *livekit.NodeStats) (*livekit.NodeStats, error) {
|
||||
updatedAt := time.Now().Unix()
|
||||
elapsed := updatedAt - prev.UpdatedAt
|
||||
|
||||
bytesIn := atomic.LoadUint64(&atomicBytesIn)
|
||||
bytesOut := atomic.LoadUint64(&atomicBytesOut)
|
||||
packetsIn := atomic.LoadUint64(&atomicPacketsIn)
|
||||
packetsOut := atomic.LoadUint64(&atomicPacketsOut)
|
||||
nackTotal := atomic.LoadUint64(&atomicNackTotal)
|
||||
bytesInNow := bytesIn.Load()
|
||||
bytesOutNow := bytesOut.Load()
|
||||
packetsInNow := packetsIn.Load()
|
||||
packetsOutNow := packetsOut.Load()
|
||||
nackTotalNow := nackTotal.Load()
|
||||
|
||||
return &livekit.NodeStats{
|
||||
StartedAt: prev.StartedAt,
|
||||
UpdatedAt: updatedAt,
|
||||
NumRooms: atomic.LoadInt32(&atomicRoomTotal),
|
||||
NumClients: atomic.LoadInt32(&atomicParticipantTotal),
|
||||
NumTracksIn: atomic.LoadInt32(&atomicTrackPublishedTotal),
|
||||
NumTracksOut: atomic.LoadInt32(&atomicTrackSubscribedTotal),
|
||||
BytesIn: bytesIn,
|
||||
BytesOut: bytesOut,
|
||||
PacketsIn: packetsIn,
|
||||
PacketsOut: packetsOut,
|
||||
NackTotal: nackTotal,
|
||||
BytesInPerSec: perSec(prev.BytesIn, bytesIn, elapsed),
|
||||
BytesOutPerSec: perSec(prev.BytesOut, bytesOut, elapsed),
|
||||
PacketsInPerSec: perSec(prev.PacketsIn, packetsIn, elapsed),
|
||||
PacketsOutPerSec: perSec(prev.PacketsOut, packetsOut, elapsed),
|
||||
NackPerSec: perSec(prev.NackTotal, nackTotal, elapsed),
|
||||
NumRooms: roomTotal.Load(),
|
||||
NumClients: participantTotal.Load(),
|
||||
NumTracksIn: trackPublishedTotal.Load(),
|
||||
NumTracksOut: trackSubscribedTotal.Load(),
|
||||
BytesIn: bytesInNow,
|
||||
BytesOut: bytesOutNow,
|
||||
PacketsIn: packetsInNow,
|
||||
PacketsOut: packetsOutNow,
|
||||
NackTotal: nackTotalNow,
|
||||
BytesInPerSec: perSec(prev.BytesIn, bytesInNow, elapsed),
|
||||
BytesOutPerSec: perSec(prev.BytesOut, bytesOutNow, elapsed),
|
||||
PacketsInPerSec: perSec(prev.PacketsIn, packetsInNow, elapsed),
|
||||
PacketsOutPerSec: perSec(prev.PacketsOut, packetsOutNow, elapsed),
|
||||
NackPerSec: perSec(prev.NackTotal, nackTotalNow, elapsed),
|
||||
NumCpus: numCPUs,
|
||||
LoadAvgLast1Min: avg1Min,
|
||||
LoadAvgLast5Min: avg5Min,
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
type Direction string
|
||||
@@ -14,11 +13,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
atomicBytesIn uint64
|
||||
atomicBytesOut uint64
|
||||
atomicPacketsIn uint64
|
||||
atomicPacketsOut uint64
|
||||
atomicNackTotal uint64
|
||||
bytesIn atomic.Uint64
|
||||
bytesOut atomic.Uint64
|
||||
packetsIn atomic.Uint64
|
||||
packetsOut atomic.Uint64
|
||||
nackTotal atomic.Uint64
|
||||
|
||||
promPacketLabels = []string{"direction"}
|
||||
|
||||
@@ -71,25 +70,25 @@ func initPacketStats(nodeID string) {
|
||||
func IncrementPackets(direction Direction, count uint64) {
|
||||
promPacketTotal.WithLabelValues(string(direction)).Add(float64(count))
|
||||
if direction == Incoming {
|
||||
atomic.AddUint64(&atomicPacketsIn, count)
|
||||
packetsIn.Add(count)
|
||||
} else {
|
||||
atomic.AddUint64(&atomicPacketsOut, count)
|
||||
packetsOut.Add(count)
|
||||
}
|
||||
}
|
||||
|
||||
func IncrementBytes(direction Direction, count uint64) {
|
||||
promPacketBytes.WithLabelValues(string(direction)).Add(float64(count))
|
||||
if direction == Incoming {
|
||||
atomic.AddUint64(&atomicBytesIn, count)
|
||||
bytesIn.Add(count)
|
||||
} else {
|
||||
atomic.AddUint64(&atomicBytesOut, count)
|
||||
bytesOut.Add(count)
|
||||
}
|
||||
}
|
||||
|
||||
func IncrementRTCP(direction Direction, nack, pli, fir uint32) {
|
||||
if nack > 0 {
|
||||
promNackTotal.WithLabelValues(string(direction)).Add(float64(nack))
|
||||
atomic.AddUint64(&atomicNackTotal, uint64(nack))
|
||||
nackTotal.Add(uint64(nack))
|
||||
}
|
||||
if pli > 0 {
|
||||
promPliTotal.WithLabelValues(string(direction)).Add(float64(pli))
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package prometheus
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
atomicRoomTotal int32
|
||||
atomicParticipantTotal int32
|
||||
atomicTrackPublishedTotal int32
|
||||
atomicTrackSubscribedTotal int32
|
||||
roomTotal atomic.Int32
|
||||
participantTotal atomic.Int32
|
||||
trackPublishedTotal atomic.Int32
|
||||
trackSubscribedTotal atomic.Int32
|
||||
|
||||
promRoomTotal prometheus.Gauge
|
||||
promRoomDuration prometheus.Histogram
|
||||
@@ -63,7 +63,7 @@ func initRoomStats(nodeID string) {
|
||||
|
||||
func RoomStarted() {
|
||||
promRoomTotal.Add(1)
|
||||
atomic.AddInt32(&atomicRoomTotal, 1)
|
||||
roomTotal.Inc()
|
||||
}
|
||||
|
||||
func RoomEnded(startedAt time.Time) {
|
||||
@@ -71,35 +71,35 @@ func RoomEnded(startedAt time.Time) {
|
||||
promRoomDuration.Observe(float64(time.Since(startedAt)) / float64(time.Second))
|
||||
}
|
||||
promRoomTotal.Sub(1)
|
||||
atomic.AddInt32(&atomicRoomTotal, -1)
|
||||
roomTotal.Dec()
|
||||
}
|
||||
|
||||
func AddParticipant() {
|
||||
promParticipantTotal.Add(1)
|
||||
atomic.AddInt32(&atomicParticipantTotal, 1)
|
||||
participantTotal.Inc()
|
||||
}
|
||||
|
||||
func SubParticipant() {
|
||||
promParticipantTotal.Sub(1)
|
||||
atomic.AddInt32(&atomicParticipantTotal, -1)
|
||||
participantTotal.Dec()
|
||||
}
|
||||
|
||||
func AddPublishedTrack(kind string) {
|
||||
promTrackPublishedTotal.WithLabelValues(kind).Add(1)
|
||||
atomic.AddInt32(&atomicTrackPublishedTotal, 1)
|
||||
trackPublishedTotal.Inc()
|
||||
}
|
||||
|
||||
func SubPublishedTrack(kind string) {
|
||||
promTrackPublishedTotal.WithLabelValues(kind).Sub(1)
|
||||
atomic.AddInt32(&atomicTrackPublishedTotal, -1)
|
||||
trackPublishedTotal.Dec()
|
||||
}
|
||||
|
||||
func AddSubscribedTrack(kind string) {
|
||||
promTrackSubscribedTotal.WithLabelValues(kind).Add(1)
|
||||
atomic.AddInt32(&atomicTrackSubscribedTotal, 1)
|
||||
trackSubscribedTotal.Inc()
|
||||
}
|
||||
|
||||
func SubSubscribedTrack(kind string) {
|
||||
promTrackSubscribedTotal.WithLabelValues(kind).Sub(1)
|
||||
atomic.AddInt32(&atomicTrackSubscribedTotal, -1)
|
||||
trackSubscribedTotal.Dec()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user