more metrics infrastructure (#74, #76)

This commit is contained in:
Michael Quigley 2022-10-13 16:18:18 -04:00
parent 41d5c2b652
commit e519b4f16a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -1,7 +1,14 @@
package controller
import (
"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"
"github.com/sirupsen/logrus"
"net"
"time"
)
type MetricsConfig struct {
@ -17,9 +24,11 @@ type InfluxConfig struct {
}
type metricsAgent struct {
cfg *MetricsConfig
shutdown chan struct{}
joined chan struct{}
cfg *MetricsConfig
shutdown chan struct{}
joined chan struct{}
zCtx ziti.Context
zListener edge.Listener
}
func newMetricsAgent(cfg *MetricsConfig) *metricsAgent {
@ -35,7 +44,48 @@ func (mtr *metricsAgent) run() {
defer logrus.Info("exiting")
defer close(mtr.joined)
if err := mtr.bindService(); err != nil {
logrus.Errorf("error binding metrics service: %v", err)
return
}
<-mtr.shutdown
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)
}
return nil
}
func (mtr *metricsAgent) listen() {
for {
conn, err := mtr.zListener.Accept()
if err != nil {
logrus.Errorf("error accepting: %v", err)
return
}
go newMetricsHandler(conn).run()
}
}
func (mtr *metricsAgent) stop() {
@ -45,3 +95,18 @@ func (mtr *metricsAgent) stop() {
func (mtr *metricsAgent) join() {
<-mtr.joined
}
type metricsHandler struct {
conn net.Conn
}
func newMetricsHandler(conn net.Conn) *metricsHandler {
return &metricsHandler{conn}
}
func (mh *metricsHandler) run() {
logrus.Infof("handling metrics connection: %v", mh.conn.RemoteAddr())
if err := mh.conn.Close(); err != nil {
logrus.Errorf("error closing metrics connection")
}
}