metrics.go 2.4 KB

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