storj/satellite/metabase/metaloop/stats.go
Egon Elbre 4c9ed64f75 satellite/metabase/metaloop: move loop under metabase
Currently the loop handling is heavily related to the metabase rather
than metainfo.

metainfo over time has become related to the "public API" for accessing
the metabase data.

Currently updates monkit.lock, because monkit monitoring does not handle
ScopeNamed correctly. Needs a followup change to monitoring check.

Change-Id: Ie50519991d718dfb872ec9a0176a82e732c97584
2021-04-22 12:58:09 +03:00

86 lines
1.8 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package metaloop
import (
"sync"
"time"
"github.com/spacemonkeygo/monkit/v3"
)
var allObserverStatsCollectors = newObserverStatsCollectors()
type observerStatsCollectors struct {
mu sync.Mutex
observer map[string]*observerStats
}
func newObserverStatsCollectors() *observerStatsCollectors {
return &observerStatsCollectors{
observer: make(map[string]*observerStats),
}
}
func (list *observerStatsCollectors) GetStats(name string) *observerStats {
list.mu.Lock()
defer list.mu.Unlock()
stats, ok := list.observer[name]
if !ok {
stats = newObserverStats(name)
mon.Chain(stats)
list.observer[name] = stats
}
return stats
}
// observerStats tracks the most recent observer stats.
type observerStats struct {
mu sync.Mutex
key monkit.SeriesKey
total time.Duration
object *monkit.DurationDist
inline *monkit.DurationDist
remote *monkit.DurationDist
}
func newObserverStats(name string) *observerStats {
return &observerStats{
key: monkit.NewSeriesKey("observer").WithTag("name", name),
total: 0,
object: nil,
inline: nil,
remote: nil,
}
}
func (stats *observerStats) Observe(observer *observerContext) {
stats.mu.Lock()
defer stats.mu.Unlock()
stats.total = observer.object.Sum + observer.inline.Sum + observer.remote.Sum
stats.object = observer.object
stats.inline = observer.inline
stats.remote = observer.remote
}
func (stats *observerStats) Stats(cb func(key monkit.SeriesKey, field string, val float64)) {
stats.mu.Lock()
defer stats.mu.Unlock()
cb(stats.key, "sum", stats.total.Seconds())
if stats.object != nil {
stats.object.Stats(cb)
}
if stats.inline != nil {
stats.inline.Stats(cb)
}
if stats.remote != nil {
stats.remote.Stats(cb)
}
}