diff --git a/endpoints/frontend/config.go b/endpoints/frontend/config.go index b5e377fb..3194f7d1 100644 --- a/endpoints/frontend/config.go +++ b/endpoints/frontend/config.go @@ -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", } } diff --git a/endpoints/frontend/http.go b/endpoints/frontend/http.go index 952f61a0..11b97c2e 100644 --- a/endpoints/frontend/http.go +++ b/endpoints/frontend/http.go @@ -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 } diff --git a/endpoints/frontend/metrics.go b/endpoints/frontend/metrics.go index 4b8862cc..ba98c7db 100644 --- a/endpoints/frontend/metrics.go +++ b/endpoints/frontend/metrics.go @@ -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 +}