2022-10-10 22:15:40 +02:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
2022-10-10 23:11:18 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/util"
|
2022-10-10 22:15:40 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-10-10 22:56:01 +02:00
|
|
|
type metricsAgent struct {
|
|
|
|
metrics map[string]sessionMetrics
|
|
|
|
updates chan metricsUpdate
|
2022-10-10 22:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 22:56:01 +02:00
|
|
|
type sessionMetrics struct {
|
|
|
|
bytesRead int64
|
|
|
|
bytesWritten int64
|
|
|
|
lastUpdate time.Time
|
2022-10-10 22:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 22:56:01 +02:00
|
|
|
type metricsUpdate struct {
|
|
|
|
id string
|
|
|
|
bytesRead int64
|
|
|
|
bytesWritten int64
|
2022-10-10 22:15:40 +02:00
|
|
|
}
|
|
|
|
|
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:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 22:56:01 +02:00
|
|
|
func (ma *metricsAgent) run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case update := <-ma.updates:
|
2022-10-10 23:11:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-10-10 22:15:40 +02:00
|
|
|
}
|