2023-01-04 12:16:47 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rangedloop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2023-02-23 13:08:30 +00:00
|
|
|
"github.com/jtolio/eventkit"
|
2023-01-04 12:16:47 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
)
|
|
|
|
|
2023-01-11 16:03:07 +00:00
|
|
|
var (
|
|
|
|
completedObserverStatsInstance completedObserverStats
|
|
|
|
completedObserverStatsInstanceInitOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2023-01-04 12:16:47 +00:00
|
|
|
func sendObserverDurations(observerDurations []ObserverDuration) {
|
2023-02-23 13:08:30 +00:00
|
|
|
for _, od := range observerDurations {
|
|
|
|
ev.Event("rangedloop",
|
|
|
|
eventkit.String("observer", fmt.Sprintf("%T", od.Observer)),
|
|
|
|
eventkit.Duration("duration", od.Duration))
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:16:47 +00:00
|
|
|
completedObserverStatsInstance.setObserverDurations(observerDurations)
|
2023-01-11 16:03:07 +00:00
|
|
|
completedObserverStatsInstanceInitOnce.Do(func() {
|
|
|
|
mon.Chain(&completedObserverStatsInstance)
|
|
|
|
})
|
2023-01-04 12:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements monkit.StatSource.
|
|
|
|
// Reports the duration per observer from the last completed run of the ranged segment loop.
|
|
|
|
type completedObserverStats struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
observerDurations []ObserverDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stats implements monkit.StatSource to send the observer durations every time monkit is polled externally.
|
|
|
|
func (o *completedObserverStats) Stats(cb func(key monkit.SeriesKey, field string, val float64)) {
|
|
|
|
o.mu.Lock()
|
|
|
|
defer o.mu.Unlock()
|
|
|
|
|
|
|
|
// if there are no completed observers yet, no statistics will be sent
|
|
|
|
for _, observerDuration := range o.observerDurations {
|
|
|
|
key := monkit.NewSeriesKey("completed-observer-duration")
|
|
|
|
key = key.WithTag("observer", fmt.Sprintf("%T", observerDuration.Observer))
|
|
|
|
|
|
|
|
cb(key, "duration", observerDuration.Duration.Seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setObserverDurations sets the observer durations to report at ranged segment loop completion.
|
|
|
|
func (o *completedObserverStats) setObserverDurations(observerDurations []ObserverDuration) {
|
|
|
|
o.mu.Lock()
|
|
|
|
defer o.mu.Unlock()
|
|
|
|
|
|
|
|
o.observerDurations = observerDurations
|
|
|
|
}
|