mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-07-19 03:11:41 +02:00
Hand over full config to Sink and Receiver
This commit is contained in:
@@ -2,6 +2,7 @@ package sinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
||||
@@ -11,27 +12,42 @@ import (
|
||||
|
||||
type InfluxSink struct {
|
||||
Sink
|
||||
client influxdb2.Client
|
||||
writeApi influxdb2Api.WriteAPIBlocking
|
||||
retPolicy string
|
||||
organization string
|
||||
client influxdb2.Client
|
||||
writeApi influxdb2Api.WriteAPIBlocking
|
||||
retPolicy string
|
||||
}
|
||||
|
||||
func (s *InfluxSink) Init(host string, port string, user string, password string, database string) error {
|
||||
s.host = host
|
||||
s.port = port
|
||||
s.user = user
|
||||
s.password = password
|
||||
s.database = database
|
||||
s.organization = ""
|
||||
uri := fmt.Sprintf("http://%s:%s", host, port)
|
||||
auth := fmt.Sprintf("%s:%s", user, password)
|
||||
log.Print("Using URI ", uri, " for connection")
|
||||
func (s *InfluxSink) connect() error {
|
||||
var auth string
|
||||
uri := fmt.Sprintf("http://%s:%s", s.host, s.port)
|
||||
if len(s.user) == 0 {
|
||||
auth = s.password
|
||||
} else {
|
||||
auth = fmt.Sprintf("%s:%s", s.user, s.password)
|
||||
}
|
||||
log.Print("Using URI ", uri, " Org ", s.organization, " Bucket ", s.database)
|
||||
s.client = influxdb2.NewClient(uri, auth)
|
||||
s.writeApi = s.client.WriteAPIBlocking(s.organization, s.database)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *InfluxSink) Init(config SinkConfig) error {
|
||||
if len(config.Host) == 0 ||
|
||||
len(config.Port) == 0 ||
|
||||
len(config.Database) == 0 ||
|
||||
len(config.Organization) == 0 ||
|
||||
len(config.Password) == 0 {
|
||||
return errors.New("Not all configuration variables set required by InfluxSink")
|
||||
}
|
||||
s.host = config.Host
|
||||
s.port = config.Port
|
||||
s.database = config.Database
|
||||
s.organization = config.Organization
|
||||
s.user = config.User
|
||||
s.password = config.Password
|
||||
return s.connect()
|
||||
}
|
||||
|
||||
func (s *InfluxSink) Write(measurement string, tags map[string]string, fields map[string]interface{}, t time.Time) error {
|
||||
p := influxdb2.NewPoint(measurement, tags, fields, t)
|
||||
err := s.writeApi.WritePoint(context.Background(), p)
|
||||
|
@@ -2,6 +2,7 @@ package sinks
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
protocol "github.com/influxdata/line-protocol"
|
||||
nats "github.com/nats-io/nats.go"
|
||||
@@ -16,21 +17,11 @@ type NatsSink struct {
|
||||
buffer *bytes.Buffer
|
||||
}
|
||||
|
||||
func (s *NatsSink) Init(host string, port string, user string, password string, database string) error {
|
||||
s.host = host
|
||||
s.port = port
|
||||
s.user = user
|
||||
s.password = password
|
||||
s.database = database
|
||||
// Setup Influx line protocol
|
||||
s.buffer = &bytes.Buffer{}
|
||||
s.buffer.Grow(1025)
|
||||
s.encoder = protocol.NewEncoder(s.buffer)
|
||||
s.encoder.SetPrecision(time.Second)
|
||||
s.encoder.SetMaxLineBytes(1024)
|
||||
// Setup infos for connection
|
||||
func (s *NatsSink) connect() error {
|
||||
uinfo := nats.UserInfo(s.user, s.password)
|
||||
uri := fmt.Sprintf("nats://%s:%s", s.host, s.port)
|
||||
log.Print("Using URI ", uri)
|
||||
s.client = nil
|
||||
nc, err := nats.Connect(uri, uinfo)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -40,21 +31,49 @@ func (s *NatsSink) Init(host string, port string, user string, password string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NatsSink) Init(config SinkConfig) error {
|
||||
if len(config.Host) == 0 ||
|
||||
len(config.Port) == 0 ||
|
||||
len(config.Database) == 0 {
|
||||
return errors.New("Not all configuration variables set required by NatsSink")
|
||||
}
|
||||
s.host = config.Host
|
||||
s.port = config.Port
|
||||
s.database = config.Database
|
||||
s.organization = config.Organization
|
||||
s.user = config.User
|
||||
s.password = config.Password
|
||||
// Setup Influx line protocol
|
||||
s.buffer = &bytes.Buffer{}
|
||||
s.buffer.Grow(1025)
|
||||
s.encoder = protocol.NewEncoder(s.buffer)
|
||||
s.encoder.SetPrecision(time.Second)
|
||||
s.encoder.SetMaxLineBytes(1024)
|
||||
// Setup infos for connection
|
||||
return s.connect()
|
||||
}
|
||||
|
||||
func (s *NatsSink) Write(measurement string, tags map[string]string, fields map[string]interface{}, t time.Time) error {
|
||||
m, err := protocol.New(measurement, tags, fields, t)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return err
|
||||
if s.client != nil {
|
||||
m, err := protocol.New(measurement, tags, fields, t)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
_, err = s.encoder.Encode(m)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
s.client.Publish(s.database, s.buffer.Bytes())
|
||||
|
||||
}
|
||||
_, err = s.encoder.Encode(m)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
s.client.Publish(s.database, s.buffer.Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NatsSink) Close() {
|
||||
s.client.Close()
|
||||
log.Print("Closing Nats connection")
|
||||
if s.client != nil {
|
||||
s.client.Close()
|
||||
}
|
||||
}
|
||||
|
@@ -4,16 +4,27 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SinkConfig struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
Database string `json:"database"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Organization string `json:"organization"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type Sink struct {
|
||||
host string
|
||||
port string
|
||||
user string
|
||||
password string
|
||||
database string
|
||||
host string
|
||||
port string
|
||||
user string
|
||||
password string
|
||||
database string
|
||||
organization string
|
||||
}
|
||||
|
||||
type SinkFuncs interface {
|
||||
Init(host string, port string, user string, password string, database string) error
|
||||
Init(config SinkConfig) error
|
||||
Write(measurement string, tags map[string]string, fields map[string]interface{}, t time.Time) error
|
||||
Close()
|
||||
}
|
||||
|
@@ -11,12 +11,7 @@ type StdoutSink struct {
|
||||
Sink
|
||||
}
|
||||
|
||||
func (s *StdoutSink) Init(host string, port string, user string, password string, database string) error {
|
||||
s.host = host
|
||||
s.port = port
|
||||
s.user = user
|
||||
s.password = password
|
||||
s.database = database
|
||||
func (s *StdoutSink) Init(config SinkConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,13 +22,13 @@ func (s *StdoutSink) Write(measurement string, tags map[string]string, fields ma
|
||||
tagsstr = append(tagsstr, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
for k, v := range fields {
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
if !math.IsNaN(v.(float64)) {
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", k, v.(float64)))
|
||||
}
|
||||
switch v.(type) {
|
||||
case float64:
|
||||
if !math.IsNaN(v.(float64)) {
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%v", k, v.(float64)))
|
||||
}
|
||||
case string:
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", k, v.(string)))
|
||||
fieldstr = append(fieldstr, fmt.Sprintf("%s=%q", k, v.(string)))
|
||||
}
|
||||
}
|
||||
if len(tagsstr) > 0 {
|
||||
|
Reference in New Issue
Block a user