Add Prometheus for some Owncast metrics (#1744)
* Add Prometheus for some Owncast metrics. Closes #1303 * Wrap prometheus metrics endpoint in admin middleware
This commit is contained in:
@@ -26,6 +26,7 @@ func collectCPUUtilization() {
|
||||
|
||||
metricValue := timestampedValue{time.Now(), int(v[0])}
|
||||
Metrics.CPUUtilizations = append(Metrics.CPUUtilizations, metricValue)
|
||||
cpuUsage.Set(float64(metricValue.Value))
|
||||
}
|
||||
|
||||
func collectRAMUtilization() {
|
||||
|
||||
@@ -2,11 +2,19 @@ package metrics
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
// How often we poll for updates.
|
||||
const metricsPollingInterval = 1 * time.Minute
|
||||
|
||||
var _getStatus func() models.Status
|
||||
|
||||
// CollectedMetrics stores different collected + timestamped values.
|
||||
type CollectedMetrics struct {
|
||||
CPUUtilizations []timestampedValue `json:"cpu"`
|
||||
@@ -20,7 +28,48 @@ type CollectedMetrics struct {
|
||||
var Metrics *CollectedMetrics
|
||||
|
||||
// Start will begin the metrics collection and alerting.
|
||||
func Start() {
|
||||
func Start(getStatus func() models.Status) {
|
||||
_getStatus = getStatus
|
||||
|
||||
host := data.GetServerURL()
|
||||
if host == "" {
|
||||
host = "unknown"
|
||||
}
|
||||
labels = map[string]string{
|
||||
"version": config.VersionNumber,
|
||||
"host": host,
|
||||
}
|
||||
|
||||
activeViewerCount = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "active_viewer_count",
|
||||
Help: "The number of viewers.",
|
||||
ConstLabels: labels,
|
||||
})
|
||||
|
||||
activeChatClientCount = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "active_chat_client_count",
|
||||
Help: "The number of connected chat clients.",
|
||||
ConstLabels: labels,
|
||||
})
|
||||
|
||||
chatUserCount = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "total_chat_users",
|
||||
Help: "The total number of chat users on this Owncast instance.",
|
||||
ConstLabels: labels,
|
||||
})
|
||||
|
||||
currentChatMessageCount = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "current_chat_message_count",
|
||||
Help: "The number of chat messages currently saved before cleanup.",
|
||||
ConstLabels: labels,
|
||||
})
|
||||
|
||||
cpuUsage = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "cpu_use_pct",
|
||||
Help: "CPU percentage used as seen within Owncast",
|
||||
ConstLabels: labels,
|
||||
})
|
||||
|
||||
Metrics = new(CollectedMetrics)
|
||||
go startViewerCollectionMetrics()
|
||||
handlePolling()
|
||||
@@ -35,6 +84,7 @@ func handlePolling() {
|
||||
collectCPUUtilization()
|
||||
collectRAMUtilization()
|
||||
collectDiskUtilization()
|
||||
collectChatClientCount()
|
||||
|
||||
// Alerting
|
||||
handleAlerting()
|
||||
|
||||
14
metrics/prometheus.go
Normal file
14
metrics/prometheus.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
labels map[string]string
|
||||
activeViewerCount prometheus.Gauge
|
||||
activeChatClientCount prometheus.Gauge
|
||||
cpuUsage prometheus.Gauge
|
||||
chatUserCount prometheus.Gauge
|
||||
currentChatMessageCount prometheus.Gauge
|
||||
)
|
||||
@@ -3,7 +3,8 @@ package metrics
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/owncast/owncast/core"
|
||||
"github.com/owncast/owncast/core/chat"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
)
|
||||
|
||||
// How often we poll for updates.
|
||||
@@ -22,10 +23,26 @@ func collectViewerCount() {
|
||||
Metrics.Viewers = Metrics.Viewers[1:]
|
||||
}
|
||||
|
||||
count := core.GetStatus().ViewerCount
|
||||
count := _getStatus().ViewerCount
|
||||
value := timestampedValue{
|
||||
Value: count,
|
||||
Time: time.Now(),
|
||||
}
|
||||
Metrics.Viewers = append(Metrics.Viewers, value)
|
||||
|
||||
// Save to our Prometheus collector.
|
||||
activeViewerCount.Set(float64(count))
|
||||
|
||||
// Total message count
|
||||
cmc := data.GetMessagesCount()
|
||||
currentChatMessageCount.Set(float64(cmc))
|
||||
|
||||
// Total user count
|
||||
uc := data.GetUsersCount()
|
||||
chatUserCount.Set(float64(uc))
|
||||
}
|
||||
|
||||
func collectChatClientCount() {
|
||||
count := len(chat.GetClients())
|
||||
activeChatClientCount.Set(float64(count))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user