metrics marshaling (#74, #76)

This commit is contained in:
Michael Quigley 2022-10-14 13:45:55 -04:00
parent 8c0261f3ad
commit f578db3406
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -1,8 +1,7 @@
package frontend package frontend
import ( import (
"fmt" "encoding/json"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/config"
@ -18,9 +17,9 @@ type metricsAgent struct {
} }
type sessionMetrics struct { type sessionMetrics struct {
bytesRead int64 BytesRead int64
bytesWritten int64 BytesWritten int64
lastUpdate time.Time LastUpdate time.Time
} }
type metricsUpdate struct { type metricsUpdate struct {
@ -51,28 +50,25 @@ func (ma *metricsAgent) run() {
select { select {
case update := <-ma.updates: case update := <-ma.updates:
if sm, found := ma.metrics[update.id]; found { if sm, found := ma.metrics[update.id]; found {
sm.bytesRead += update.bytesRead sm.BytesRead += update.bytesRead
sm.bytesWritten += update.bytesWritten sm.BytesWritten += update.bytesWritten
sm.lastUpdate = time.Now() sm.LastUpdate = time.Now()
ma.metrics[update.id] = sm ma.metrics[update.id] = sm
} else { } else {
sm := sessionMetrics{ sm := sessionMetrics{
bytesRead: update.bytesRead, BytesRead: update.bytesRead,
bytesWritten: update.bytesWritten, BytesWritten: update.bytesWritten,
lastUpdate: time.Now(), LastUpdate: time.Now(),
} }
ma.metrics[update.id] = sm ma.metrics[update.id] = sm
} }
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
now := time.Now() if metricsJson, err := json.MarshalIndent(ma.metrics, "", " "); err == nil {
out := "metrics = {\n" logrus.Info(string(metricsJson))
for k, v := range ma.metrics { } else {
age := now.Sub(v.lastUpdate) logrus.Errorf("error marshaling metrics: %v", err)
out += fmt.Sprintf("\t[%v]: %s/%s (%s)\n", k, util.BytesToSize(v.bytesRead), util.BytesToSize(v.bytesWritten), age.String())
} }
out += "}\n"
} }
} }
} }