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 (
|
import (
|
||||||
"github.com/michaelquigley/cf"
|
"github.com/michaelquigley/cf"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Identity string
|
Identity string
|
||||||
MetricsService string
|
Metrics *MetricsConfig
|
||||||
Address string
|
Address string
|
||||||
HostMatch string
|
HostMatch string
|
||||||
|
}
|
||||||
|
|
||||||
|
type MetricsConfig struct {
|
||||||
|
Service string
|
||||||
|
DropoutTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultConfig() *Config {
|
func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Identity: "frontend",
|
Identity: "frontend",
|
||||||
MetricsService: "metrics",
|
Metrics: &MetricsConfig{
|
||||||
Address: "0.0.0.0:8080",
|
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) {
|
func NewHTTP(cfg *Config) (*httpListen, error) {
|
||||||
ma, err := newMetricsAgent(cfg.Identity, cfg.MetricsService)
|
ma, err := newMetricsAgent(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -12,10 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type metricsAgent struct {
|
type metricsAgent struct {
|
||||||
metricsServiceName string
|
cfg *Config
|
||||||
metrics *model.Metrics
|
metrics *model.Metrics
|
||||||
updates chan metricsUpdate
|
updates chan metricsUpdate
|
||||||
zCtx ziti.Context
|
zCtx ziti.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
type metricsUpdate struct {
|
type metricsUpdate struct {
|
||||||
@ -24,21 +24,21 @@ type metricsUpdate struct {
|
|||||||
bytesWritten int64
|
bytesWritten int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMetricsAgent(identityName, metricsServiceName string) (*metricsAgent, error) {
|
func newMetricsAgent(cfg *Config) (*metricsAgent, error) {
|
||||||
zif, err := zrokdir.ZitiIdentityFile(identityName)
|
zif, err := zrokdir.ZitiIdentityFile(cfg.Identity)
|
||||||
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", cfg.Identity)
|
||||||
}
|
}
|
||||||
zCfg, err := config.NewFromFile(zif)
|
zCfg, err := config.NewFromFile(zif)
|
||||||
if err != nil {
|
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{
|
return &metricsAgent{
|
||||||
metricsServiceName: metricsServiceName,
|
cfg: cfg,
|
||||||
metrics: &model.Metrics{Namespace: identityName},
|
metrics: &model.Metrics{Namespace: cfg.Identity},
|
||||||
updates: make(chan metricsUpdate, 10240),
|
updates: make(chan metricsUpdate, 10240),
|
||||||
zCtx: ziti.NewContextWithConfig(zCfg),
|
zCtx: ziti.NewContextWithConfig(zCfg),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,6 +56,16 @@ func (ma *metricsAgent) run() {
|
|||||||
if err := ma.sendMetrics(); err != nil {
|
if err := ma.sendMetrics(); err != nil {
|
||||||
logrus.Errorf("error sending metrics: %v", err)
|
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 {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error marshaling metrics")
|
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 {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error connecting to metrics service")
|
return errors.Wrap(err, "error connecting to metrics service")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user