forked from owncast/owncast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
103 lines (80 loc) · 2.36 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package metrics
import (
"sync"
"time"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/core/data"
"github.com/owncast/owncast/models"
)
// How often we poll for updates.
const (
hardwareMetricsPollingInterval = 2 * time.Minute
playbackMetricsPollingInterval = 2 * time.Minute
)
const (
// How often we poll for updates.
viewerMetricsPollingInterval = 2 * time.Minute
activeChatClientCountKey = "chat_client_count"
activeViewerCountKey = "viewer_count"
)
// CollectedMetrics stores different collected + timestamped values.
type CollectedMetrics struct {
streamHealthOverview *models.StreamHealthOverview
medianSegmentDownloadSeconds []TimestampedValue `json:"-"`
maximumSegmentDownloadSeconds []TimestampedValue `json:"-"`
DiskUtilizations []TimestampedValue `json:"disk"`
errorCount []TimestampedValue `json:"-"`
lowestBitrate []TimestampedValue `json:"-"`
medianBitrate []TimestampedValue `json:"-"`
RAMUtilizations []TimestampedValue `json:"memory"`
CPUUtilizations []TimestampedValue `json:"cpu"`
highestBitrate []TimestampedValue `json:"-"`
minimumSegmentDownloadSeconds []TimestampedValue `json:"-"`
minimumLatency []TimestampedValue `json:"-"`
maximumLatency []TimestampedValue `json:"-"`
medianLatency []TimestampedValue `json:"-"`
qualityVariantChanges []TimestampedValue `json:"-"`
m sync.Mutex `json:"-"`
}
// Metrics is the shared Metrics instance.
var metrics *CollectedMetrics
var _getStatus func() models.Status
// Start will begin the metrics collection and alerting.
func Start(getStatus func() models.Status) {
_getStatus = getStatus
host := data.GetServerURL()
if host == "" {
host = "unknown"
}
labels = map[string]string{
"version": config.VersionNumber,
"host": host,
}
setupPrometheusCollectors()
metrics = new(CollectedMetrics)
go startViewerCollectionMetrics()
go func() {
for range time.Tick(hardwareMetricsPollingInterval) {
handlePolling()
}
}()
go func() {
for range time.Tick(playbackMetricsPollingInterval) {
handlePlaybackPolling()
}
}()
}
func handlePolling() {
metrics.m.Lock()
defer metrics.m.Unlock()
// Collect hardware stats
collectCPUUtilization()
collectRAMUtilization()
collectDiskUtilization()
// Alerting
handleAlerting()
}
// GetMetrics will return the collected metrics.
func GetMetrics() *CollectedMetrics {
return metrics
}