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