Include node ID with Prometheus metrics (#251)

* include node id in prometheus metrics

* static prom init and nodeID

* update protocol dep
This commit is contained in:
Mathew Kamkar
2021-12-10 15:49:14 -08:00
committed by GitHub
parent d342335d09
commit bd42a39117
6 changed files with 88 additions and 71 deletions
+5 -16
View File
@@ -1,13 +1,9 @@
package routing
import (
"crypto/sha1"
"fmt"
"os"
"runtime"
"time"
"github.com/jxskiss/base62"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
@@ -17,15 +13,15 @@ import (
type LocalNode *livekit.Node
func NewLocalNode(conf *config.Config) (LocalNode, error) {
hostname, err := os.Hostname()
nodeID, err := utils.LocalNodeID()
if err != nil {
return nil, err
}
if conf.RTC.NodeIP == "" {
return nil, ErrIPNotSet
}
return &livekit.Node{
Id: fmt.Sprintf("%s%s", utils.NodePrefix, HashedID(hostname)[:8]),
node := &livekit.Node{
Id: nodeID,
Ip: conf.RTC.NodeIP,
NumCpus: uint32(runtime.NumCPU()),
Region: conf.Region,
@@ -34,14 +30,7 @@ func NewLocalNode(conf *config.Config) (LocalNode, error) {
StartedAt: time.Now().Unix(),
UpdatedAt: time.Now().Unix(),
},
}, nil
}
}
// Creates a hashed ID from a unique string
func HashedID(id string) string {
h := sha1.New()
h.Write([]byte(id))
val := h.Sum(nil)
return base62.EncodeToString(val)
return node, nil
}
+18 -11
View File
@@ -5,37 +5,44 @@ import (
"time"
livekit "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
"github.com/prometheus/client_golang/prometheus"
)
const livekitNamespace = "livekit"
const livekitNamespace string = "livekit"
var (
MessageCounter *prometheus.CounterVec
ServiceOperationCounter *prometheus.CounterVec
)
func init() {
nodeID, _ := utils.LocalNodeID()
MessageCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "node",
Name: "messages",
Namespace: livekitNamespace,
Subsystem: "node",
Name: "messages",
ConstLabels: prometheus.Labels{"node_id": nodeID},
},
[]string{"type", "status"},
)
ServiceOperationCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "node",
Name: "service_operation",
Namespace: livekitNamespace,
Subsystem: "node",
Name: "service_operation",
ConstLabels: prometheus.Labels{"node_id": nodeID},
},
[]string{"type", "status", "error_type"},
)
)
func init() {
prometheus.MustRegister(MessageCounter)
prometheus.MustRegister(ServiceOperationCounter)
initPacketStats()
initRoomStats()
initPacketStats(nodeID)
initRoomStats(nodeID)
}
func UpdateCurrentNodeStats(nodeStats *livekit.NodeStats) error {
+37 -26
View File
@@ -22,34 +22,45 @@ var (
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)
promPacketTotal *prometheus.CounterVec
promPacketBytes *prometheus.CounterVec
promNackTotal *prometheus.CounterVec
promPliTotal *prometheus.CounterVec
promFirTotal *prometheus.CounterVec
)
func initPacketStats() {
func initPacketStats(nodeID string) {
promPacketTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "packet",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, promPacketLabels)
promPacketBytes = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "packet",
Name: "bytes",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, promPacketLabels)
promNackTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "nack",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, promPacketLabels)
promPliTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "pli",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, promPacketLabels)
promFirTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: livekitNamespace,
Subsystem: "fir",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, promPacketLabels)
prometheus.MustRegister(promPacketTotal)
prometheus.MustRegister(promPacketBytes)
prometheus.MustRegister(promNackTotal)
+24 -14
View File
@@ -13,37 +13,47 @@ var (
atomicTrackPublishedTotal int32
atomicTrackSubscribedTotal int32
promRoomTotal prometheus.Gauge
promRoomDuration prometheus.Histogram
promParticipantTotal prometheus.Gauge
promTrackPublishedTotal *prometheus.GaugeVec
promTrackSubscribedTotal *prometheus.GaugeVec
)
func initRoomStats(nodeID string) {
promRoomTotal = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "room",
Name: "total",
})
promRoomDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: livekitNamespace,
Subsystem: "room",
Name: "duration_seconds",
Namespace: livekitNamespace,
Subsystem: "room",
Name: "duration_seconds",
ConstLabels: prometheus.Labels{"node_id": nodeID},
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",
Namespace: livekitNamespace,
Subsystem: "participant",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
})
promTrackPublishedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "track",
Name: "published_total",
Namespace: livekitNamespace,
Subsystem: "track",
Name: "published_total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, []string{"kind"})
promTrackSubscribedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "track",
Name: "subscribed_total",
Namespace: livekitNamespace,
Subsystem: "track",
Name: "subscribed_total",
ConstLabels: prometheus.Labels{"node_id": nodeID},
}, []string{"kind"})
)
func initRoomStats() {
prometheus.MustRegister(promRoomTotal)
prometheus.MustRegister(promRoomDuration)
prometheus.MustRegister(promParticipantTotal)