2022-10-10 22:15:40 +02:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
2022-10-14 19:55:35 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/model"
|
2022-10-14 19:40:29 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/zrokdir"
|
|
|
|
"github.com/openziti/sdk-golang/ziti"
|
|
|
|
"github.com/openziti/sdk-golang/ziti/config"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-10-14 22:13:21 +02:00
|
|
|
"gopkg.in/mgo.v2/bson"
|
2022-10-10 22:15:40 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-10-10 22:56:01 +02:00
|
|
|
type metricsAgent struct {
|
2022-10-14 21:38:09 +02:00
|
|
|
metricsServiceName string
|
|
|
|
metrics *model.Metrics
|
|
|
|
updates chan metricsUpdate
|
|
|
|
zCtx ziti.Context
|
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-14 21:38:09 +02:00
|
|
|
func newMetricsAgent(identityName, metricsServiceName string) (*metricsAgent, error) {
|
2022-10-14 19:40:29 +02:00
|
|
|
zif, err := zrokdir.ZitiIdentityFile(identityName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error getting '%v' identity file", identityName)
|
|
|
|
}
|
|
|
|
zCfg, err := config.NewFromFile(zif)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error loading '%v' identity", identityName)
|
|
|
|
}
|
|
|
|
logrus.Infof("loaded '%v' identity", identityName)
|
2022-10-10 22:56:01 +02:00
|
|
|
return &metricsAgent{
|
2022-10-14 21:38:09 +02:00
|
|
|
metricsServiceName: metricsServiceName,
|
|
|
|
metrics: &model.Metrics{},
|
|
|
|
updates: make(chan metricsUpdate, 10240),
|
|
|
|
zCtx: ziti.NewContextWithConfig(zCfg),
|
2022-10-14 19:40:29 +02:00
|
|
|
}, nil
|
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-14 19:55:35 +02:00
|
|
|
ma.metrics.PushSession(update.id, model.SessionMetrics{
|
|
|
|
BytesRead: update.bytesRead,
|
|
|
|
BytesWritten: update.bytesWritten,
|
|
|
|
LastUpdate: time.Now().UnixMilli(),
|
|
|
|
})
|
2022-10-10 23:11:18 +02:00
|
|
|
|
|
|
|
case <-time.After(5 * time.Second):
|
2022-10-14 21:38:09 +02:00
|
|
|
if err := ma.sendMetrics(); err != nil {
|
|
|
|
logrus.Errorf("error sending metrics: %v", err)
|
2022-10-10 23:11:18 +02:00
|
|
|
}
|
2022-10-10 22:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 22:15:40 +02:00
|
|
|
}
|
2022-10-14 21:38:09 +02:00
|
|
|
|
|
|
|
func (ma *metricsAgent) sendMetrics() error {
|
2022-10-14 22:13:21 +02:00
|
|
|
ma.metrics.Now = time.Now().UnixMilli()
|
|
|
|
metricsJson, err := bson.Marshal(ma.metrics)
|
2022-10-14 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error marshaling metrics")
|
|
|
|
}
|
|
|
|
conn, err := ma.zCtx.Dial(ma.metricsServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error connecting to metrics service")
|
|
|
|
}
|
|
|
|
n, err := conn.Write(metricsJson)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error sending metrics")
|
|
|
|
}
|
|
|
|
defer func() { _ = conn.Close() }()
|
|
|
|
if n != len(metricsJson) {
|
|
|
|
return errors.Wrap(err, "short metrics write")
|
|
|
|
}
|
|
|
|
logrus.Infof("sent %d bytes of metrics data", n)
|
|
|
|
return nil
|
|
|
|
}
|