zrok/controller/metrics/agent.go

51 lines
837 B
Go
Raw Normal View History

2023-03-03 19:31:57 +01:00
package metrics
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type MetricsAgent struct {
src Source
join chan struct{}
}
func Run(cfg *Config) (*MetricsAgent, error) {
2023-03-03 19:31:57 +01:00
logrus.Info("starting")
if cfg.Source == nil {
return nil, errors.New("no 'source' configured; exiting")
2023-03-03 19:31:57 +01:00
}
src, ok := cfg.Source.(Source)
if !ok {
return nil, errors.New("invalid 'source'; exiting")
2023-03-03 19:31:57 +01:00
}
events := make(chan map[string]interface{}, 1024)
join, err := src.Start(events)
2023-03-03 19:31:57 +01:00
if err != nil {
return nil, errors.Wrap(err, "error starting source")
2023-03-03 19:31:57 +01:00
}
go func() {
for {
select {
case event := <-events:
2023-03-03 22:45:18 +01:00
logrus.Info(Ingest(event))
}
}
}()
return &MetricsAgent{src, join}, nil
}
func (ma *MetricsAgent) Stop() {
logrus.Info("stopping")
ma.src.Stop()
}
2023-03-03 19:31:57 +01:00
func (ma *MetricsAgent) Join() {
<-ma.join
2023-03-03 19:31:57 +01:00
}