snapshots/collector (#948)

This commit is contained in:
Michael Quigley 2025-04-21 14:05:45 -04:00
parent 1fc20c202d
commit afbd67f386
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

55
canary/metrics.go Normal file
View File

@ -0,0 +1,55 @@
package canary
import (
"context"
"github.com/sirupsen/logrus"
"slices"
"sort"
"time"
)
type Snapshot struct {
Stamp time.Time
Operation string
Instance uint
Started time.Time
Completed time.Time
Size uint64
}
type SnapshotCollector struct {
InputQueue chan *Snapshot
Closed chan struct{}
ctx context.Context
snapshots map[string][]*Snapshot
}
func NewSnapshotCollector(ctx context.Context) *SnapshotCollector {
return &SnapshotCollector{
InputQueue: make(chan *Snapshot),
Closed: make(chan struct{}),
ctx: ctx,
snapshots: make(map[string][]*Snapshot),
}
}
func (sc *SnapshotCollector) Run() {
defer close(sc.Closed)
defer logrus.Info("stopping")
logrus.Info("starting")
for {
select {
case <-sc.ctx.Done():
return
case snapshot := <-sc.InputQueue:
var snapshots []*Snapshot
if v, ok := sc.snapshots[snapshot.Operation]; ok {
snapshots = v
}
i := sort.Search(len(snapshots), func(i int) bool { return snapshots[i].Completed.After(snapshot.Started) })
snapshots = slices.Insert(snapshots, i, snapshot)
sc.snapshots[snapshot.Operation] = snapshots
}
}
}