2021-08-31 10:43:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-08 09:08:51 +02:00
|
|
|
"context"
|
2022-01-20 10:14:28 +01:00
|
|
|
"fmt"
|
2021-08-31 10:43:16 +02:00
|
|
|
"log"
|
2021-09-07 09:28:41 +02:00
|
|
|
"sync"
|
2022-01-20 10:14:28 +01:00
|
|
|
"time"
|
2021-08-31 10:43:16 +02:00
|
|
|
|
2021-10-07 14:52:16 +02:00
|
|
|
"github.com/influxdata/line-protocol/v2/lineprotocol"
|
2021-08-31 10:43:16 +02:00
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Metric struct {
|
|
|
|
Name string
|
2022-01-24 09:50:12 +01:00
|
|
|
minfo metricInfo
|
2021-08-31 10:43:16 +02:00
|
|
|
Value Float
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to a nats server and subscribe to "updates". This is a blocking
|
|
|
|
// function. handleLine will be called for each line recieved via nats.
|
|
|
|
// Send `true` through the done channel for gracefull termination.
|
2022-02-22 13:58:10 +01:00
|
|
|
func ReceiveNats(conf *NatsConfig, handleLine func(*lineprotocol.Decoder, string) error, workers int, ctx context.Context) error {
|
2022-02-04 08:52:53 +01:00
|
|
|
var opts []nats.Option
|
|
|
|
if conf.Username != "" && conf.Password != "" {
|
|
|
|
opts = append(opts, nats.UserInfo(conf.Username, conf.Password))
|
|
|
|
}
|
|
|
|
|
|
|
|
nc, err := nats.Connect(conf.Address, opts...)
|
2021-08-31 10:43:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer nc.Close()
|
|
|
|
|
2021-10-07 14:52:16 +02:00
|
|
|
var wg sync.WaitGroup
|
2021-09-07 09:28:41 +02:00
|
|
|
var sub *nats.Subscription
|
|
|
|
|
2021-10-07 14:52:16 +02:00
|
|
|
msgs := make(chan *nats.Msg, workers*2)
|
2021-09-08 09:08:51 +02:00
|
|
|
|
2021-10-07 14:52:16 +02:00
|
|
|
if workers > 1 {
|
2021-09-07 09:28:41 +02:00
|
|
|
wg.Add(workers)
|
|
|
|
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go func() {
|
2021-09-13 12:26:55 +02:00
|
|
|
for m := range msgs {
|
2021-10-07 14:52:16 +02:00
|
|
|
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
2022-02-22 13:58:10 +01:00
|
|
|
if err := handleLine(dec, conf.ClusterTag); err != nil {
|
2021-10-11 16:28:05 +02:00
|
|
|
log.Printf("error: %s\n", err.Error())
|
|
|
|
}
|
2021-09-07 09:28:41 +02:00
|
|
|
}
|
2021-09-08 09:08:51 +02:00
|
|
|
|
|
|
|
wg.Done()
|
2021-09-07 09:28:41 +02:00
|
|
|
}()
|
2021-08-31 10:43:16 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 08:52:53 +01:00
|
|
|
sub, err = nc.Subscribe(conf.SubscribeTo, func(m *nats.Msg) {
|
2021-09-07 09:28:41 +02:00
|
|
|
msgs <- m
|
|
|
|
})
|
2021-10-07 14:52:16 +02:00
|
|
|
} else {
|
2022-02-04 08:52:53 +01:00
|
|
|
sub, err = nc.Subscribe(conf.SubscribeTo, func(m *nats.Msg) {
|
2021-10-07 14:52:16 +02:00
|
|
|
dec := lineprotocol.NewDecoderWithBytes(m.Data)
|
2022-02-22 13:58:10 +01:00
|
|
|
if err := handleLine(dec, conf.ClusterTag); err != nil {
|
2021-10-11 16:28:05 +02:00
|
|
|
log.Printf("error: %s\n", err.Error())
|
|
|
|
}
|
2021-10-07 14:52:16 +02:00
|
|
|
})
|
|
|
|
}
|
2021-09-08 09:08:51 +02:00
|
|
|
|
2021-10-07 14:52:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-07 09:28:41 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 08:52:53 +01:00
|
|
|
log.Printf("NATS subscription to '%s' on '%s' established\n", conf.SubscribeTo, conf.Address)
|
2021-10-07 14:52:16 +02:00
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
err = sub.Unsubscribe()
|
|
|
|
close(msgs)
|
|
|
|
wg.Wait()
|
|
|
|
|
2021-09-07 09:28:41 +02:00
|
|
|
if err != nil {
|
2021-08-31 10:43:16 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:08:51 +02:00
|
|
|
nc.Close()
|
|
|
|
log.Println("NATS connection closed")
|
|
|
|
return nil
|
2021-08-31 10:43:16 +02:00
|
|
|
}
|
2022-01-20 10:14:28 +01:00
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
// Place `prefix` in front of `buf` but if possible,
|
|
|
|
// do that inplace in `buf`.
|
|
|
|
func reorder(buf, prefix []byte) []byte {
|
|
|
|
n := len(prefix)
|
|
|
|
m := len(buf)
|
|
|
|
if cap(buf) < m+n {
|
|
|
|
return append(prefix[:n:n], buf...)
|
|
|
|
} else {
|
|
|
|
buf = buf[:n+m]
|
|
|
|
for i := m - 1; i >= 0; i-- {
|
|
|
|
buf[i+n] = buf[i]
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
buf[i] = prefix[i]
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:58:10 +01:00
|
|
|
// Decode lines using dec and make write calls to the MemoryStore.
|
|
|
|
// If a line is missing its cluster tag, use clusterDefault as default.
|
|
|
|
func decodeLine(dec *lineprotocol.Decoder, clusterDefault string) error {
|
2022-01-21 10:47:40 +01:00
|
|
|
// Reduce allocations in loop:
|
|
|
|
t := time.Now()
|
|
|
|
metrics := make([]Metric, 0, 10)
|
|
|
|
selector := make([]string, 0, 4)
|
|
|
|
typeBuf, subTypeBuf := make([]byte, 0, 20), make([]byte, 0)
|
|
|
|
|
|
|
|
// Optimize for the case where all lines in a "batch" are about the same
|
|
|
|
// cluster and host. By using `WriteToLevel` (level = host), we do not need
|
|
|
|
// to take the root- and cluster-level lock as often.
|
2022-01-24 09:50:12 +01:00
|
|
|
var lvl *level = nil
|
2022-01-21 10:47:40 +01:00
|
|
|
var prevCluster, prevHost string = "", ""
|
|
|
|
|
2022-01-20 10:14:28 +01:00
|
|
|
for dec.Next() {
|
2022-01-24 09:50:12 +01:00
|
|
|
metrics = metrics[:0]
|
2022-01-20 10:14:28 +01:00
|
|
|
rawmeasurement, err := dec.Measurement()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
// A more dense lp format if supported if the measurement is 'data'.
|
|
|
|
// In that case, the field keys are used as metric names.
|
|
|
|
if string(rawmeasurement) != "data" {
|
|
|
|
minfo, ok := memoryStore.metrics[string(rawmeasurement)]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics = append(metrics, Metric{
|
|
|
|
minfo: minfo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
typeBuf, subTypeBuf := typeBuf[:0], subTypeBuf[:0]
|
2022-02-22 13:58:10 +01:00
|
|
|
cluster, host := clusterDefault, ""
|
2022-01-20 10:14:28 +01:00
|
|
|
for {
|
|
|
|
key, val, err := dec.NextTag()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if key == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:47:40 +01:00
|
|
|
// The go compiler optimizes string([]byte{...}) == "...":
|
2022-01-20 10:14:28 +01:00
|
|
|
switch string(key) {
|
|
|
|
case "cluster":
|
2022-01-21 10:47:40 +01:00
|
|
|
if string(val) == prevCluster {
|
|
|
|
cluster = prevCluster
|
|
|
|
} else {
|
|
|
|
cluster = string(val)
|
2022-01-24 09:50:12 +01:00
|
|
|
lvl = nil
|
2022-01-21 10:47:40 +01:00
|
|
|
}
|
2022-01-24 09:50:12 +01:00
|
|
|
case "hostname", "host":
|
2022-01-21 10:47:40 +01:00
|
|
|
if string(val) == prevHost {
|
|
|
|
host = prevHost
|
|
|
|
} else {
|
|
|
|
host = string(val)
|
2022-01-24 09:50:12 +01:00
|
|
|
lvl = nil
|
2022-01-21 10:47:40 +01:00
|
|
|
}
|
2022-01-20 10:14:28 +01:00
|
|
|
case "type":
|
2022-01-24 09:50:12 +01:00
|
|
|
if string(val) == "node" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(typeBuf) == 0 {
|
|
|
|
typeBuf = append(typeBuf, val...)
|
|
|
|
} else {
|
|
|
|
typeBuf = reorder(typeBuf, val)
|
|
|
|
}
|
2022-01-20 10:14:28 +01:00
|
|
|
case "type-id":
|
2022-01-24 09:50:12 +01:00
|
|
|
typeBuf = append(typeBuf, val...)
|
2022-01-20 10:14:28 +01:00
|
|
|
case "subtype":
|
2022-01-24 09:50:12 +01:00
|
|
|
if len(subTypeBuf) == 0 {
|
|
|
|
subTypeBuf = append(subTypeBuf, val...)
|
|
|
|
} else {
|
|
|
|
subTypeBuf = reorder(typeBuf, val)
|
|
|
|
}
|
2022-01-20 10:14:28 +01:00
|
|
|
case "stype-id":
|
2022-01-24 09:50:12 +01:00
|
|
|
subTypeBuf = append(subTypeBuf, val...)
|
2022-01-20 10:14:28 +01:00
|
|
|
default:
|
|
|
|
// Ignore unkown tags (cc-metric-collector might send us a unit for example that we do not need)
|
|
|
|
// return fmt.Errorf("unkown tag: '%s' (value: '%s')", string(key), string(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
if lvl == nil {
|
2022-01-21 10:47:40 +01:00
|
|
|
selector = selector[:2]
|
2022-01-24 09:50:12 +01:00
|
|
|
selector[0], selector[1] = cluster, host
|
|
|
|
lvl = memoryStore.GetLevel(selector)
|
|
|
|
prevCluster, prevHost = cluster, host
|
2022-01-21 10:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
selector = selector[:0]
|
2022-01-24 09:50:12 +01:00
|
|
|
if len(typeBuf) > 0 {
|
2022-01-21 10:47:40 +01:00
|
|
|
selector = append(selector, string(typeBuf)) // <- Allocation :(
|
2022-01-24 09:50:12 +01:00
|
|
|
if len(subTypeBuf) > 0 {
|
2022-01-21 10:47:40 +01:00
|
|
|
selector = append(selector, string(subTypeBuf))
|
2022-01-20 10:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
if len(metrics) == 0 {
|
2022-01-20 10:14:28 +01:00
|
|
|
for {
|
|
|
|
key, val, err := dec.NextField()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var value Float
|
|
|
|
if val.Kind() == lineprotocol.Float {
|
|
|
|
value = Float(val.FloatV())
|
|
|
|
} else if val.Kind() == lineprotocol.Int {
|
|
|
|
value = Float(val.IntV())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unsupported value type in message: %s", val.Kind().String())
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
minfo, ok := memoryStore.metrics[string(key)]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:14:28 +01:00
|
|
|
metrics = append(metrics, Metric{
|
2022-01-24 09:50:12 +01:00
|
|
|
minfo: minfo,
|
2022-01-20 10:14:28 +01:00
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var value Float
|
|
|
|
for {
|
|
|
|
key, val, err := dec.NextField()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(key) != "value" {
|
|
|
|
return fmt.Errorf("unkown field: '%s' (value: %#v)", string(key), val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.Kind() == lineprotocol.Float {
|
|
|
|
value = Float(val.FloatV())
|
|
|
|
} else if val.Kind() == lineprotocol.Int {
|
|
|
|
value = Float(val.IntV())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unsupported value type in message: %s", val.Kind().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
metrics[0].Value = value
|
2022-01-20 10:14:28 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:47:40 +01:00
|
|
|
t, err = dec.Time(lineprotocol.Second, t)
|
2022-01-20 10:14:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Printf("write: %s (%v) -> %v\n", string(measurement), selector, value)
|
2022-01-24 09:50:12 +01:00
|
|
|
if err := memoryStore.WriteToLevel(lvl, selector, t.Unix(), metrics); err != nil {
|
2022-01-20 10:14:28 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|