metrics.go 2.3 KB

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