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
This commit is contained in:
Gabe Kangas 2020-10-02 00:06:14 -07:00 committed by GitHub
parent d8c43d2c56
commit 236f25b772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 12 deletions

View file

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

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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() {
}
memoryUsage, _ := mem.VirtualMemory()
metricValue := value{time.Now(), int(memoryUsage.UsedPercent)}
metricValue := timestampedValue{time.Now(), int(memoryUsage.UsedPercent)}
Metrics.RAMUtilizations = append(Metrics.RAMUtilizations, metricValue)
}

View file

@ -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
// Start will begin the metrics collection and alerting
func Start() {
Metrics = new(metrics)
startViewerCollectionMetrics()
for range time.Tick(metricsPollingInterval) {
handlePolling()

View file

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

31
metrics/viewers.go Normal file
View file

@ -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)
}

View file

@ -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()