zrok/endpoints/frontend/metrics.go

63 lines
1.3 KiB
Go
Raw Normal View History

package frontend
import (
"fmt"
"github.com/openziti-test-kitchen/zrok/util"
"time"
)
2022-10-10 22:56:01 +02:00
type metricsAgent struct {
metrics map[string]sessionMetrics
updates chan metricsUpdate
}
2022-10-10 22:56:01 +02:00
type sessionMetrics struct {
bytesRead int64
bytesWritten int64
lastUpdate time.Time
}
2022-10-10 22:56:01 +02:00
type metricsUpdate struct {
id string
bytesRead int64
bytesWritten int64
}
2022-10-10 22:56:01 +02:00
func newMetricsAgent() *metricsAgent {
return &metricsAgent{
metrics: make(map[string]sessionMetrics),
updates: make(chan metricsUpdate, 10240),
}
}
2022-10-10 22:56:01 +02:00
func (ma *metricsAgent) run() {
for {
select {
case update := <-ma.updates:
if sm, found := ma.metrics[update.id]; found {
sm.bytesRead += update.bytesRead
sm.bytesWritten += update.bytesWritten
sm.lastUpdate = time.Now()
ma.metrics[update.id] = sm
} else {
sm := sessionMetrics{
bytesRead: update.bytesRead,
bytesWritten: update.bytesWritten,
lastUpdate: time.Now(),
}
ma.metrics[update.id] = sm
}
case <-time.After(5 * time.Second):
now := time.Now()
out := "metrics = {\n"
for k, v := range ma.metrics {
age := now.Sub(v.lastUpdate)
out += fmt.Sprintf("\t[%v]: %s/%s (%s)\n", k, util.BytesToSize(v.bytesRead), util.BytesToSize(v.bytesWritten), age.String())
}
out += "}\n"
//fmt.Println(out)
2022-10-10 22:56:01 +02:00
}
}
}