2021-05-18 15:15:26 +02:00
|
|
|
package receivers
|
|
|
|
|
|
|
|
import (
|
2021-05-18 15:44:42 +02:00
|
|
|
"errors"
|
2022-01-25 15:37:43 +01:00
|
|
|
"fmt"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
|
|
|
influx "github.com/influxdata/line-protocol"
|
2021-05-18 15:15:26 +02:00
|
|
|
nats "github.com/nats-io/nats.go"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
type NatsReceiverConfig struct {
|
|
|
|
Addr string `json:"address"`
|
|
|
|
Port string `json:"port"`
|
|
|
|
Database string `json:"database"`
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:15:26 +02:00
|
|
|
type NatsReceiver struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
receiver
|
2021-10-08 13:28:44 +02:00
|
|
|
nc *nats.Conn
|
2022-01-25 15:37:43 +01:00
|
|
|
handler *influx.MetricHandler
|
|
|
|
parser *influx.Parser
|
|
|
|
meta map[string]string
|
|
|
|
config ReceiverConfig
|
2021-05-18 15:15:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultTime = func() time.Time {
|
|
|
|
return time.Unix(42, 0)
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (r *NatsReceiver) Init(config ReceiverConfig) error {
|
|
|
|
r.name = "NatsReceiver"
|
|
|
|
r.config = config
|
|
|
|
if len(r.config.Addr) == 0 ||
|
|
|
|
len(r.config.Port) == 0 ||
|
|
|
|
len(r.config.Database) == 0 {
|
2021-05-18 15:15:26 +02:00
|
|
|
return errors.New("Not all configuration variables set required by NatsReceiver")
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
r.meta = map[string]string{"source": r.name}
|
|
|
|
r.addr = r.config.Addr
|
2021-05-18 15:15:26 +02:00
|
|
|
if len(r.addr) == 0 {
|
|
|
|
r.addr = nats.DefaultURL
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
r.port = r.config.Port
|
2021-05-18 15:15:26 +02:00
|
|
|
if len(r.port) == 0 {
|
|
|
|
r.port = "4222"
|
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
log.Print("[NatsReceiver] INIT")
|
|
|
|
uri := fmt.Sprintf("%s:%s", r.addr, r.port)
|
|
|
|
nc, err := nats.Connect(uri)
|
2021-05-18 15:15:26 +02:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
r.database = r.config.Database
|
2021-05-18 15:15:26 +02:00
|
|
|
r.nc = nc
|
|
|
|
} else {
|
|
|
|
r.nc = nil
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
2021-05-18 15:15:26 +02:00
|
|
|
}
|
2022-01-25 15:37:43 +01:00
|
|
|
r.handler = influx.NewMetricHandler()
|
|
|
|
r.parser = influx.NewParser(r.handler)
|
2021-10-08 13:28:44 +02:00
|
|
|
r.parser.SetTimeFunc(DefaultTime)
|
2021-05-18 15:15:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *NatsReceiver) Start() {
|
2022-01-25 15:37:43 +01:00
|
|
|
log.Print("[NatsReceiver] START")
|
2021-05-18 15:15:26 +02:00
|
|
|
r.nc.Subscribe(r.database, r._NatsReceive)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *NatsReceiver) _NatsReceive(m *nats.Msg) {
|
2021-10-08 13:28:44 +02:00
|
|
|
metrics, err := r.parser.Parse(m.Data)
|
2021-05-18 15:15:26 +02:00
|
|
|
if err == nil {
|
|
|
|
for _, m := range metrics {
|
2022-01-25 15:37:43 +01:00
|
|
|
y := lp.FromInfluxMetric(m)
|
|
|
|
for k, v := range r.meta {
|
|
|
|
y.AddMeta(k, v)
|
|
|
|
}
|
|
|
|
//y, err := lp.New(m.Name(), Tags2Map(m), r.meta, Fields2Map(m), m.Time())
|
|
|
|
if r.sink != nil {
|
|
|
|
r.sink <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-05-18 15:15:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *NatsReceiver) Close() {
|
|
|
|
if r.nc != nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
log.Print("[NatsReceiver] CLOSE")
|
2021-05-18 15:15:26 +02:00
|
|
|
r.nc.Close()
|
|
|
|
}
|
|
|
|
}
|