healthOverview.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package metrics
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/owncast/owncast/core"
  6. "github.com/owncast/owncast/core/data"
  7. "github.com/owncast/owncast/models"
  8. "github.com/owncast/owncast/utils"
  9. )
  10. const (
  11. healthyPercentageMinValue = 75
  12. maxCPUUsage = 90
  13. minClientCountForDetails = 3
  14. )
  15. // GetStreamHealthOverview will return the stream health overview.
  16. func GetStreamHealthOverview() *models.StreamHealthOverview {
  17. return metrics.streamHealthOverview
  18. }
  19. func generateStreamHealthOverview() {
  20. // Determine what percentage of total players are represented in our overview.
  21. totalPlayerCount := len(core.GetActiveViewers())
  22. if totalPlayerCount == 0 {
  23. metrics.streamHealthOverview = nil
  24. return
  25. }
  26. pct := getClientErrorHeathyPercentage()
  27. if pct == -1 {
  28. metrics.streamHealthOverview = nil
  29. return
  30. }
  31. overview := &models.StreamHealthOverview{
  32. Healthy: pct > healthyPercentageMinValue,
  33. HealthyPercentage: pct,
  34. Message: getStreamHealthOverviewMessage(),
  35. }
  36. if totalPlayerCount > 0 && len(windowedBandwidths) > 0 {
  37. representation := utils.IntPercentage(len(windowedBandwidths), totalPlayerCount)
  38. overview.Representation = representation
  39. }
  40. metrics.streamHealthOverview = overview
  41. }
  42. func getStreamHealthOverviewMessage() string {
  43. if message := wastefulBitrateOverviewMessage(); message != "" {
  44. return message
  45. } else if message := cpuUsageHealthOverviewMessage(); message != "" {
  46. return message
  47. } else if message := networkSpeedHealthOverviewMessage(); message != "" {
  48. return message
  49. } else if message := errorCountHealthOverviewMessage(); message != "" {
  50. return message
  51. }
  52. return ""
  53. }
  54. func networkSpeedHealthOverviewMessage() string {
  55. type singleVariant struct {
  56. isVideoPassthrough bool
  57. bitrate int
  58. }
  59. outputVariants := data.GetStreamOutputVariants()
  60. streamSortVariants := make([]singleVariant, len(outputVariants))
  61. for i, variant := range outputVariants {
  62. variantSort := singleVariant{
  63. bitrate: variant.VideoBitrate,
  64. isVideoPassthrough: variant.IsVideoPassthrough,
  65. }
  66. streamSortVariants[i] = variantSort
  67. }
  68. sort.Slice(streamSortVariants, func(i, j int) bool {
  69. if streamSortVariants[i].isVideoPassthrough && !streamSortVariants[j].isVideoPassthrough {
  70. return true
  71. }
  72. if !streamSortVariants[i].isVideoPassthrough && streamSortVariants[j].isVideoPassthrough {
  73. return false
  74. }
  75. return streamSortVariants[i].bitrate > streamSortVariants[j].bitrate
  76. })
  77. lowestSupportedBitrate := float64(streamSortVariants[len(streamSortVariants)-1].bitrate)
  78. totalNumberOfClients := len(windowedBandwidths)
  79. if totalNumberOfClients == 0 {
  80. return ""
  81. }
  82. // Determine healthy status based on bandwidth speeds of clients.
  83. unhealthyClientCount := 0
  84. for _, speed := range windowedBandwidths {
  85. if int(speed) < int(lowestSupportedBitrate*1.1) {
  86. unhealthyClientCount++
  87. }
  88. }
  89. if unhealthyClientCount == 0 {
  90. return ""
  91. }
  92. return fmt.Sprintf("%d of %d viewers (%d%%) are consuming video slower than, or too close to your bitrate of %d kbps.", unhealthyClientCount, totalNumberOfClients, int((float64(unhealthyClientCount)/float64(totalNumberOfClients))*100), int(lowestSupportedBitrate))
  93. }
  94. // wastefulBitrateOverviewMessage attempts to determine if a streamer is sending to
  95. // Owncast at a bitrate higher than they're streaming to their viewers leading
  96. // to wasted CPU by having to compress it.
  97. func wastefulBitrateOverviewMessage() string {
  98. if len(metrics.CPUUtilizations) < 2 {
  99. return ""
  100. }
  101. // Only return an alert if the CPU usage is around the max cpu threshold.
  102. recentCPUUses := metrics.CPUUtilizations[len(metrics.CPUUtilizations)-2:]
  103. values := make([]float64, len(recentCPUUses))
  104. for i, val := range recentCPUUses {
  105. values[i] = val.Value
  106. }
  107. recentCPUUse := utils.Avg(values)
  108. if recentCPUUse < maxCPUUsage-10 {
  109. return ""
  110. }
  111. currentBroadcast := core.GetCurrentBroadcast()
  112. if currentBroadcast == nil {
  113. return ""
  114. }
  115. currentBroadcaster := core.GetBroadcaster()
  116. if currentBroadcast == nil {
  117. return ""
  118. }
  119. if currentBroadcaster.StreamDetails.AudioBitrate == 0 {
  120. return ""
  121. }
  122. inboundBitrate := currentBroadcaster.StreamDetails.VideoBitrate
  123. maxBitrate := 0
  124. if inboundBitrate > maxBitrate {
  125. return fmt.Sprintf("You're broadcasting to Owncast at %dkbps but only sending to your viewers at %dkbps, requiring unnecessary work to be performed. You may want to decrease what you're sending to Owncast or increase what you send to your viewers to match.", inboundBitrate, maxBitrate)
  126. }
  127. return ""
  128. }
  129. func cpuUsageHealthOverviewMessage() string {
  130. if len(metrics.CPUUtilizations) < 2 {
  131. return ""
  132. }
  133. recentCPUUses := metrics.CPUUtilizations[len(metrics.CPUUtilizations)-2:]
  134. values := make([]float64, len(recentCPUUses))
  135. for i, val := range recentCPUUses {
  136. values[i] = val.Value
  137. }
  138. recentCPUUse := utils.Avg(values)
  139. if recentCPUUse < maxCPUUsage {
  140. return ""
  141. }
  142. return fmt.Sprintf("The CPU usage on your server is over %d%%. This may cause video to be provided slower than necessary, causing buffering for your viewers. Consider increasing the resources available or reducing the number of output variants you made available.", maxCPUUsage)
  143. }
  144. func errorCountHealthOverviewMessage() string {
  145. totalNumberOfClients := len(windowedBandwidths)
  146. if totalNumberOfClients == 0 {
  147. return ""
  148. }
  149. clientsWithErrors := getClientsWithErrorsCount()
  150. if clientsWithErrors == 0 {
  151. return ""
  152. }
  153. // Only return these detailed values and messages if we feel we have enough
  154. // clients to be able to make a reasonable assessment. This is an arbitrary
  155. // number but 1 out of 1 isn't helpful.
  156. if totalNumberOfClients >= minClientCountForDetails {
  157. healthyPercentage := utils.IntPercentage(clientsWithErrors, totalNumberOfClients)
  158. isUsingPassthrough := false
  159. outputVariants := data.GetStreamOutputVariants()
  160. for _, variant := range outputVariants {
  161. if variant.IsVideoPassthrough {
  162. isUsingPassthrough = true
  163. }
  164. }
  165. if isUsingPassthrough {
  166. return fmt.Sprintf("%d of %d viewers (%d%%) are experiencing errors. You're currently using a video passthrough output, often known for causing playback issues for people. It is suggested you turn it off.", clientsWithErrors, totalNumberOfClients, healthyPercentage)
  167. }
  168. return fmt.Sprintf("%d of %d viewers (%d%%) may be experiencing some issues.", clientsWithErrors, totalNumberOfClients, healthyPercentage)
  169. }
  170. return ""
  171. }
  172. func getClientsWithErrorsCount() int {
  173. clientsWithErrors := 0
  174. for _, errors := range windowedErrorCounts {
  175. if errors > 0 {
  176. clientsWithErrors++
  177. }
  178. }
  179. return clientsWithErrors
  180. }
  181. func getClientErrorHeathyPercentage() int {
  182. totalNumberOfClients := len(windowedErrorCounts)
  183. if totalNumberOfClients == 0 {
  184. return -1
  185. }
  186. clientsWithErrors := getClientsWithErrorsCount()
  187. if clientsWithErrors == 0 {
  188. return 100
  189. }
  190. pct := 100 - utils.IntPercentage(clientsWithErrors, totalNumberOfClients)
  191. return pct
  192. }