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 {
Identity string
Address string
HostMatch string
Identity string
MetricsService string
Address string
HostMatch string
}
func DefaultConfig() *Config {
return &Config{
Identity: "frontend",
Address: "0.0.0.0:8080",
Identity: "frontend",
MetricsService: "metrics",
Address: "0.0.0.0:8080",
}
}

View File

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

View File

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