From 992e819f3895656630af1f7c891124b91699944c Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Thu, 17 Mar 2022 18:07:55 -0700 Subject: [PATCH] Lock metrics on mutation --- metrics/metrics.go | 6 ++++++ metrics/playback.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/metrics/metrics.go b/metrics/metrics.go index a2fb6955a..377a566e2 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -1,6 +1,7 @@ package metrics import ( + "sync" "time" "github.com/owncast/owncast/config" @@ -20,6 +21,8 @@ const ( // CollectedMetrics stores different collected + timestamped values. type CollectedMetrics struct { + m sync.Mutex `json:"-"` + CPUUtilizations []TimestampedValue `json:"cpu"` RAMUtilizations []TimestampedValue `json:"memory"` DiskUtilizations []TimestampedValue `json:"disk"` @@ -65,6 +68,9 @@ func Start(getStatus func() models.Status) { } func handlePolling() { + metrics.m.Lock() + defer metrics.m.Unlock() + // Collect hardware stats collectCPUUtilization() collectRAMUtilization() diff --git a/metrics/playback.go b/metrics/playback.go index e9c02a9a0..45312cd47 100644 --- a/metrics/playback.go +++ b/metrics/playback.go @@ -18,28 +18,38 @@ var ( // RegisterPlaybackErrorCount will add to the windowed playback error count. func RegisterPlaybackErrorCount(count float64) { + metrics.m.Lock() + defer metrics.m.Unlock() windowedErrorCounts = append(windowedErrorCounts, count) } // RegisterQualityVariantChangesCount will add to the windowed quality variant // change count. func RegisterQualityVariantChangesCount(count float64) { + metrics.m.Lock() + defer metrics.m.Unlock() windowedQualityVariantChanges = append(windowedQualityVariantChanges, count) } // RegisterPlayerBandwidth will add to the windowed playback bandwidth. func RegisterPlayerBandwidth(kbps float64) { + metrics.m.Lock() + defer metrics.m.Unlock() windowedBandwidths = append(windowedBandwidths, kbps) } // RegisterPlayerLatency will add to the windowed player latency values. func RegisterPlayerLatency(seconds float64) { + metrics.m.Lock() + defer metrics.m.Unlock() windowedLatencies = append(windowedLatencies, seconds) } // RegisterPlayerSegmentDownloadDuration will add to the windowed player segment // download duration values. func RegisterPlayerSegmentDownloadDuration(seconds float64) { + metrics.m.Lock() + defer metrics.m.Unlock() windowedDownloadDurations = append(windowedDownloadDurations, seconds) }