owncast/controllers/playbackMetrics.go
Gabe Kangas babbcecc9c
Stream performance metrics (#1785)
* WIP playback metrics

* Playback metrics collecting + APIs. Closes #793

* Cleanup console messages

* Update test

* Increase browser test timeout

* Update browser tests to not fail
2022-03-16 17:34:44 -07:00

41 lines
1.3 KiB
Go

package controllers
import (
"encoding/json"
"net/http"
"github.com/owncast/owncast/metrics"
log "github.com/sirupsen/logrus"
)
// ReportPlaybackMetrics will accept playback metrics from a client and save
// them for future video health reporting.
func ReportPlaybackMetrics(w http.ResponseWriter, r *http.Request) {
if r.Method != POST {
WriteSimpleResponse(w, false, r.Method+" not supported")
return
}
type reportPlaybackMetricsRequest struct {
Bandwidth float64 `json:"bandwidth"`
Latency float64 `json:"latency"`
Errors float64 `json:"errors"`
DownloadDuration float64 `json:"downloadDuration"`
QualityVariantChanges float64 `json:"qualityVariantChanges"`
}
decoder := json.NewDecoder(r.Body)
var request reportPlaybackMetricsRequest
if err := decoder.Decode(&request); err != nil {
log.Errorln("error decoding playback metrics payload:", err)
WriteSimpleResponse(w, false, err.Error())
return
}
metrics.RegisterPlaybackErrorCount(request.Errors)
metrics.RegisterPlayerBandwidth(request.Bandwidth)
metrics.RegisterPlayerLatency(request.Latency)
metrics.RegisterPlayerSegmentDownloadDuration(request.DownloadDuration)
metrics.RegisterQualityVariantChangesCount(request.QualityVariantChanges)
}