Browse Source

Viewer metrics api (#208)

* Add support for ending the inbound stream. Closes #191

* Add a simple success response to API requests

* Add viewers over time API

* Move controllers to admin directory
pull/216/head
Gabe Kangas 5 years ago committed by GitHub
parent
commit
236f25b772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      controllers/admin/serverConfig.go
  2. 15
      controllers/admin/viewers.go
  3. 2
      metrics/alerting.go
  4. 4
      metrics/hardware.go
  5. 11
      metrics/metrics.go
  6. 8
      metrics/timestampedValue.go
  7. 31
      metrics/viewers.go
  8. 5
      router/router.go

2
controllers/serverConfig.go → controllers/admin/serverConfig.go

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
package controllers
package admin
import (
"encoding/json"

15
controllers/admin/viewers.go

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
package admin
import (
"encoding/json"
"net/http"
"github.com/gabek/owncast/metrics"
)
// GetViewersOverTime will return the number of viewers at points in time
func GetViewersOverTime(w http.ResponseWriter, r *http.Request) {
viewersOverTime := metrics.Metrics.Viewers
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(viewersOverTime)
}

2
metrics/alerting.go

@ -36,6 +36,6 @@ func handleRAMAlerting() { @@ -36,6 +36,6 @@ func handleRAMAlerting() {
}
}
func recentAverage(values []value) int {
func recentAverage(values []timestampedValue) int {
return int((values[len(values)-1].Value + values[len(values)-2].Value) / 2)
}

4
metrics/hardware.go

@ -20,7 +20,7 @@ func collectCPUUtilization() { @@ -20,7 +20,7 @@ func collectCPUUtilization() {
panic(err)
}
metricValue := value{time.Now(), int(v[0])}
metricValue := timestampedValue{time.Now(), int(v[0])}
Metrics.CPUUtilizations = append(Metrics.CPUUtilizations, metricValue)
}
@ -30,6 +30,6 @@ func collectRAMUtilization() { @@ -30,6 +30,6 @@ func collectRAMUtilization() {
}
memoryUsage, _ := mem.VirtualMemory()
metricValue := value{time.Now(), int(memoryUsage.UsedPercent)}
metricValue := timestampedValue{time.Now(), int(memoryUsage.UsedPercent)}
Metrics.RAMUtilizations = append(Metrics.RAMUtilizations, metricValue)
}

11
metrics/metrics.go

@ -7,14 +7,10 @@ import ( @@ -7,14 +7,10 @@ import (
// How often we poll for updates
const metricsPollingInterval = 15 * time.Second
type value struct {
Time time.Time
Value int
}
type metrics struct {
CPUUtilizations []value
RAMUtilizations []value
CPUUtilizations []timestampedValue
RAMUtilizations []timestampedValue
Viewers []timestampedValue
}
// Metrics is the shared Metrics instance
@ -23,6 +19,7 @@ var Metrics *metrics @@ -23,6 +19,7 @@ var Metrics *metrics
// Start will begin the metrics collection and alerting
func Start() {
Metrics = new(metrics)
startViewerCollectionMetrics()
for range time.Tick(metricsPollingInterval) {
handlePolling()

8
metrics/timestampedValue.go

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
package metrics
import "time"
type timestampedValue struct {
Time time.Time `json:"time"`
Value int `json:"value"`
}

31
metrics/viewers.go

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
package metrics
import (
"time"
"github.com/gabek/owncast/core"
)
// How often we poll for updates
const viewerMetricsPollingInterval = 5 * time.Minute
func startViewerCollectionMetrics() {
collectViewerCount()
for range time.Tick(viewerMetricsPollingInterval) {
collectViewerCount()
}
}
func collectViewerCount() {
if len(Metrics.Viewers) > maxCollectionValues {
Metrics.Viewers = Metrics.Viewers[1:]
}
count := core.GetStatus().ViewerCount
value := timestampedValue{
Value: count,
Time: time.Now(),
}
Metrics.Viewers = append(Metrics.Viewers, value)
}

5
router/router.go

@ -58,7 +58,10 @@ func Start() error { @@ -58,7 +58,10 @@ func Start() error {
http.HandleFunc("/api/admin/changekey", middleware.RequireAdminAuth(admin.ChangeStreamKey))
// Server config
http.HandleFunc("/api/admin/serverconfig", middleware.RequireAdminAuth(controllers.GetServerConfig))
http.HandleFunc("/api/admin/serverconfig", middleware.RequireAdminAuth(admin.GetServerConfig))
// Get viewer count over time
http.HandleFunc("/api/admin/viewersOverTime", middleware.RequireAdminAuth(admin.GetViewersOverTime))
port := config.Config.GetPublicWebServerPort()

Loading…
Cancel
Save