mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 09:48:07 +02:00
parent
1542e0131f
commit
8c214fcdd8
@ -3,20 +3,29 @@ package frontend
|
||||
import (
|
||||
"github.com/michaelquigley/cf"
|
||||
"github.com/pkg/errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Identity string
|
||||
MetricsService string
|
||||
Address string
|
||||
HostMatch string
|
||||
Identity string
|
||||
Metrics *MetricsConfig
|
||||
Address string
|
||||
HostMatch string
|
||||
}
|
||||
|
||||
type MetricsConfig struct {
|
||||
Service string
|
||||
DropoutTimeout time.Duration
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Identity: "frontend",
|
||||
MetricsService: "metrics",
|
||||
Address: "0.0.0.0:8080",
|
||||
Identity: "frontend",
|
||||
Metrics: &MetricsConfig{
|
||||
Service: "metrics",
|
||||
DropoutTimeout: 30 * time.Second,
|
||||
},
|
||||
Address: "0.0.0.0:8080",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ type httpListen struct {
|
||||
}
|
||||
|
||||
func NewHTTP(cfg *Config) (*httpListen, error) {
|
||||
ma, err := newMetricsAgent(cfg.Identity, cfg.MetricsService)
|
||||
ma, err := newMetricsAgent(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -12,10 +12,10 @@ import (
|
||||
)
|
||||
|
||||
type metricsAgent struct {
|
||||
metricsServiceName string
|
||||
metrics *model.Metrics
|
||||
updates chan metricsUpdate
|
||||
zCtx ziti.Context
|
||||
cfg *Config
|
||||
metrics *model.Metrics
|
||||
updates chan metricsUpdate
|
||||
zCtx ziti.Context
|
||||
}
|
||||
|
||||
type metricsUpdate struct {
|
||||
@ -24,21 +24,21 @@ type metricsUpdate struct {
|
||||
bytesWritten int64
|
||||
}
|
||||
|
||||
func newMetricsAgent(identityName, metricsServiceName string) (*metricsAgent, error) {
|
||||
zif, err := zrokdir.ZitiIdentityFile(identityName)
|
||||
func newMetricsAgent(cfg *Config) (*metricsAgent, error) {
|
||||
zif, err := zrokdir.ZitiIdentityFile(cfg.Identity)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error getting '%v' identity file", identityName)
|
||||
return nil, errors.Wrapf(err, "error getting '%v' identity file", cfg.Identity)
|
||||
}
|
||||
zCfg, err := config.NewFromFile(zif)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error loading '%v' identity", identityName)
|
||||
return nil, errors.Wrapf(err, "error loading '%v' identity", cfg.Identity)
|
||||
}
|
||||
logrus.Infof("loaded '%v' identity", identityName)
|
||||
logrus.Infof("loaded '%v' identity", cfg.Identity)
|
||||
return &metricsAgent{
|
||||
metricsServiceName: metricsServiceName,
|
||||
metrics: &model.Metrics{Namespace: identityName},
|
||||
updates: make(chan metricsUpdate, 10240),
|
||||
zCtx: ziti.NewContextWithConfig(zCfg),
|
||||
cfg: cfg,
|
||||
metrics: &model.Metrics{Namespace: cfg.Identity},
|
||||
updates: make(chan metricsUpdate, 10240),
|
||||
zCtx: ziti.NewContextWithConfig(zCfg),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -56,6 +56,16 @@ func (ma *metricsAgent) run() {
|
||||
if err := ma.sendMetrics(); err != nil {
|
||||
logrus.Errorf("error sending metrics: %v", err)
|
||||
}
|
||||
var dropouts []string
|
||||
for k, v := range ma.metrics.Sessions {
|
||||
if time.Now().Sub(time.UnixMilli(v.LastUpdate)) > ma.cfg.Metrics.DropoutTimeout {
|
||||
dropouts = append(dropouts, k)
|
||||
}
|
||||
}
|
||||
for _, dropout := range dropouts {
|
||||
delete(ma.metrics.Sessions, dropout)
|
||||
logrus.Infof("dropout: %v", dropout)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -66,7 +76,7 @@ func (ma *metricsAgent) sendMetrics() error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error marshaling metrics")
|
||||
}
|
||||
conn, err := ma.zCtx.Dial(ma.metricsServiceName)
|
||||
conn, err := ma.zCtx.Dial(ma.cfg.Metrics.Service)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error connecting to metrics service")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user