2020-08-27 10:37:32 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2022-03-07 04:26:52 +03:00
|
|
|
|
|
|
|
"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"
|
2020-08-27 10:37:32 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// How often we poll for updates.
|
2020-10-02 22:18:08 +03:00
|
|
|
const metricsPollingInterval = 1 * time.Minute
|
2020-08-27 10:37:32 +03:00
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// CollectedMetrics stores different collected + timestamped values.
|
2020-10-02 22:18:08 +03:00
|
|
|
type CollectedMetrics struct {
|
|
|
|
CPUUtilizations []timestampedValue `json:"cpu"`
|
|
|
|
RAMUtilizations []timestampedValue `json:"memory"`
|
|
|
|
DiskUtilizations []timestampedValue `json:"disk"`
|
2020-08-27 10:37:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Metrics is the shared Metrics instance.
|
2020-10-02 22:18:08 +03:00
|
|
|
var Metrics *CollectedMetrics
|
2020-08-27 10:37:32 +03:00
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Start will begin the metrics collection and alerting.
|
2022-03-07 04:26:52 +03:00
|
|
|
func Start(getStatus func() models.Status) {
|
|
|
|
host := data.GetServerURL()
|
|
|
|
if host == "" {
|
|
|
|
host = "unknown"
|
|
|
|
}
|
|
|
|
labels = map[string]string{
|
|
|
|
"version": config.VersionNumber,
|
|
|
|
"host": host,
|
|
|
|
}
|
|
|
|
|
|
|
|
activeViewerCount = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-10 04:48:53 +03:00
|
|
|
Name: "owncast_instance_active_viewer_count",
|
2022-03-07 04:26:52 +03:00
|
|
|
Help: "The number of viewers.",
|
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
|
|
|
|
|
|
|
activeChatClientCount = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-10 04:48:53 +03:00
|
|
|
Name: "owncast_instance_active_chat_client_count",
|
2022-03-07 04:26:52 +03:00
|
|
|
Help: "The number of connected chat clients.",
|
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
|
|
|
|
|
|
|
chatUserCount = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-10 04:48:53 +03:00
|
|
|
Name: "owncast_instance_total_chat_users",
|
2022-03-07 04:26:52 +03:00
|
|
|
Help: "The total number of chat users on this Owncast instance.",
|
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
|
|
|
|
|
|
|
currentChatMessageCount = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-10 04:48:53 +03:00
|
|
|
Name: "owncast_instance_current_chat_message_count",
|
2022-03-07 04:26:52 +03:00
|
|
|
Help: "The number of chat messages currently saved before cleanup.",
|
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
|
|
|
|
|
|
|
cpuUsage = promauto.NewGauge(prometheus.GaugeOpts{
|
2022-03-10 04:48:53 +03:00
|
|
|
Name: "owncast_instance_cpu_use_pct",
|
2022-03-07 04:26:52 +03:00
|
|
|
Help: "CPU percentage used as seen within Owncast",
|
|
|
|
ConstLabels: labels,
|
|
|
|
})
|
|
|
|
|
2020-10-02 22:18:08 +03:00
|
|
|
Metrics = new(CollectedMetrics)
|
|
|
|
go startViewerCollectionMetrics()
|
2020-08-27 10:37:32 +03:00
|
|
|
|
|
|
|
for range time.Tick(metricsPollingInterval) {
|
|
|
|
handlePolling()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePolling() {
|
|
|
|
// Collect hardware stats
|
|
|
|
collectCPUUtilization()
|
|
|
|
collectRAMUtilization()
|
2020-10-02 22:18:08 +03:00
|
|
|
collectDiskUtilization()
|
2020-08-27 10:37:32 +03:00
|
|
|
|
|
|
|
// Alerting
|
|
|
|
handleAlerting()
|
|
|
|
}
|