zrok/controller/metrics.go

146 lines
3.4 KiB
Go
Raw Normal View History

2022-10-12 18:42:05 +02:00
package controller
2022-10-13 20:23:52 +02:00
import (
2022-10-14 21:49:59 +02:00
"bytes"
2022-10-14 22:13:21 +02:00
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
2022-10-14 21:49:59 +02:00
"github.com/openziti-test-kitchen/zrok/model"
2022-10-14 22:13:21 +02:00
"github.com/openziti-test-kitchen/zrok/util"
2022-10-13 22:18:18 +02:00
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/pkg/errors"
2022-10-13 20:23:52 +02:00
"github.com/sirupsen/logrus"
2022-10-14 22:13:21 +02:00
"gopkg.in/mgo.v2/bson"
2022-10-13 22:18:18 +02:00
"net"
"time"
2022-10-13 20:23:52 +02:00
)
2022-10-12 18:42:05 +02:00
type MetricsConfig struct {
ServiceName string
Influx *InfluxConfig
2022-10-12 18:42:05 +02:00
}
type InfluxConfig struct {
Url string
Bucket string
Org string
Token string
}
2022-10-13 20:23:52 +02:00
type metricsAgent struct {
2022-10-13 22:18:18 +02:00
cfg *MetricsConfig
influx influxdb2.Client
2022-10-13 22:18:18 +02:00
zCtx ziti.Context
zListener edge.Listener
shutdown chan struct{}
joined chan struct{}
2022-10-13 20:23:52 +02:00
}
func newMetricsAgent(cfg *MetricsConfig) *metricsAgent {
ma := &metricsAgent{
cfg: cfg,
shutdown: make(chan struct{}),
joined: make(chan struct{}),
}
ma.influx = influxdb2.NewClient(cfg.Influx.Url, cfg.Influx.Token)
return ma
2022-10-13 20:23:52 +02:00
}
func (mtr *metricsAgent) run() {
logrus.Info("starting")
defer logrus.Info("exiting")
defer close(mtr.joined)
2022-10-13 20:23:52 +02:00
2022-10-13 22:18:18 +02:00
if err := mtr.bindService(); err != nil {
logrus.Errorf("error binding metrics service: %v", err)
return
}
<-mtr.shutdown
2022-10-13 22:18:18 +02:00
if err := mtr.zListener.Close(); err != nil {
logrus.Errorf("error closing metrics service listener: %v", err)
}
}
func (mtr *metricsAgent) bindService() error {
zif, err := zrokdir.ZitiIdentityFile("ctrl")
if err != nil {
return errors.Wrap(err, "error getting 'ctrl' identity")
}
zCfg, err := config.NewFromFile(zif)
if err != nil {
return errors.Wrap(err, "error loading 'ctrl' identity")
}
mtr.zCtx = ziti.NewContextWithConfig(zCfg)
opts := &ziti.ListenOptions{
ConnectTimeout: 5 * time.Minute,
MaxConnections: 1024,
}
mtr.zListener, err = mtr.zCtx.ListenWithOptions(mtr.cfg.ServiceName, opts)
if err != nil {
return errors.Wrapf(err, "error listening for metrics on '%v'", mtr.cfg.ServiceName)
}
2022-10-14 21:49:59 +02:00
go mtr.listen()
2022-10-13 22:18:18 +02:00
return nil
}
func (mtr *metricsAgent) listen() {
2022-10-14 21:49:59 +02:00
logrus.Info("started")
defer logrus.Info("exited")
2022-10-13 22:18:18 +02:00
for {
conn, err := mtr.zListener.Accept()
if err != nil {
logrus.Errorf("error accepting: %v", err)
return
}
2022-10-14 22:13:21 +02:00
logrus.Debugf("accepted metrics connetion from '%v'", conn.RemoteAddr())
2022-10-13 22:18:18 +02:00
go newMetricsHandler(conn).run()
}
}
func (mtr *metricsAgent) stop() {
close(mtr.shutdown)
}
func (mtr *metricsAgent) join() {
<-mtr.joined
2022-10-13 20:23:52 +02:00
}
2022-10-13 22:18:18 +02:00
type metricsHandler struct {
conn net.Conn
}
func newMetricsHandler(conn net.Conn) *metricsHandler {
return &metricsHandler{conn}
}
func (mh *metricsHandler) run() {
2022-10-14 22:13:21 +02:00
logrus.Debugf("handling metrics connection: %v", mh.conn.RemoteAddr())
2022-10-14 21:49:59 +02:00
var mtrBuf bytes.Buffer
buf := make([]byte, 4096)
for {
n, err := mh.conn.Read(buf)
if err != nil {
break
}
mtrBuf.Write(buf[:n])
}
2022-10-13 22:18:18 +02:00
if err := mh.conn.Close(); err != nil {
logrus.Errorf("error closing metrics connection")
}
2022-10-14 21:49:59 +02:00
mtr := &model.Metrics{}
2022-10-14 22:13:21 +02:00
if err := bson.Unmarshal(mtrBuf.Bytes(), &mtr); err == nil {
out := "metrics = {\n"
for k, v := range mtr.Sessions {
2022-10-17 19:45:52 +02:00
out += fmt.Sprintf("\t[%v.%v]: %v/%v (%v)\n", mtr.Namespace, k, util.BytesToSize(v.BytesRead), util.BytesToSize(v.BytesWritten), time.Duration(mtr.LocalNow-v.LastUpdate)*time.Millisecond)
2022-10-14 22:13:21 +02:00
}
out += "}"
logrus.Info(out)
2022-10-14 21:49:59 +02:00
} else {
logrus.Errorf("error unmarshaling metrics: %v", err)
}
2022-10-13 22:18:18 +02:00
}