frontend metrics sending (#74, #76)

This commit is contained in:
Michael Quigley 2022-10-14 15:38:09 -04:00
parent 33404801eb
commit b3c827a3fd
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 40 additions and 17 deletions

View File

@ -6,15 +6,17 @@ import (
) )
type Config struct { type Config struct {
Identity string Identity string
Address string MetricsService string
HostMatch string Address string
HostMatch string
} }
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
Identity: "frontend", Identity: "frontend",
Address: "0.0.0.0:8080", MetricsService: "metrics",
Address: "0.0.0.0:8080",
} }
} }

View File

@ -28,7 +28,7 @@ type httpListen struct {
} }
func NewHTTP(cfg *Config) (*httpListen, error) { func NewHTTP(cfg *Config) (*httpListen, error) {
ma, err := newMetricsAgent(cfg.Identity) ma, err := newMetricsAgent(cfg.Identity, cfg.MetricsService)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -12,9 +12,10 @@ import (
) )
type metricsAgent struct { type metricsAgent struct {
metrics *model.Metrics metricsServiceName string
updates chan metricsUpdate metrics *model.Metrics
zCtx ziti.Context updates chan metricsUpdate
zCtx ziti.Context
} }
type metricsUpdate struct { type metricsUpdate struct {
@ -23,7 +24,7 @@ type metricsUpdate struct {
bytesWritten int64 bytesWritten int64
} }
func newMetricsAgent(identityName string) (*metricsAgent, error) { func newMetricsAgent(identityName, metricsServiceName string) (*metricsAgent, error) {
zif, err := zrokdir.ZitiIdentityFile(identityName) zif, err := zrokdir.ZitiIdentityFile(identityName)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error getting '%v' identity file", identityName) return nil, errors.Wrapf(err, "error getting '%v' identity file", identityName)
@ -34,9 +35,10 @@ func newMetricsAgent(identityName string) (*metricsAgent, error) {
} }
logrus.Infof("loaded '%v' identity", identityName) logrus.Infof("loaded '%v' identity", identityName)
return &metricsAgent{ return &metricsAgent{
metrics: &model.Metrics{}, metricsServiceName: metricsServiceName,
updates: make(chan metricsUpdate, 10240), metrics: &model.Metrics{},
zCtx: ziti.NewContextWithConfig(zCfg), updates: make(chan metricsUpdate, 10240),
zCtx: ziti.NewContextWithConfig(zCfg),
}, nil }, nil
} }
@ -51,11 +53,30 @@ func (ma *metricsAgent) run() {
}) })
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
if metricsJson, err := json.MarshalIndent(ma.metrics, "", " "); err == nil { if err := ma.sendMetrics(); err != nil {
logrus.Info(string(metricsJson)) logrus.Errorf("error sending metrics: %v", err)
} else {
logrus.Errorf("error marshaling metrics: %v", err)
} }
} }
} }
} }
func (ma *metricsAgent) sendMetrics() error {
metricsJson, err := json.MarshalIndent(ma.metrics, "", " ")
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
}