mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-07-19 03:11:41 +02:00
Add sink for InfluxDB (with the original InfluxDB client)
This commit is contained in:
44
sinks/influxSink.go
Normal file
44
sinks/influxSink.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package sinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
influxdb2Api "github.com/influxdata/influxdb-client-go/v2/api"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type InfluxSink struct {
|
||||
Sink
|
||||
client influxdb2.Client
|
||||
writeApi influxdb2Api.WriteAPIBlocking
|
||||
retPolicy string
|
||||
organization 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")
|
||||
s.client = influxdb2.NewClient(uri, auth)
|
||||
s.writeApi = s.client.WriteAPIBlocking(s.organization, s.database)
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *InfluxSink) Close() {
|
||||
log.Print("Closing InfluxDB connection")
|
||||
s.client.Close()
|
||||
}
|
19
sinks/sink.go
Normal file
19
sinks/sink.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package sinks
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Sink struct {
|
||||
host string
|
||||
port string
|
||||
user string
|
||||
password string
|
||||
database string
|
||||
}
|
||||
|
||||
type SinkFuncs interface {
|
||||
Init(host string, port string, user string, password string, database string) error
|
||||
Write(measurement string, tags map[string]string, fields map[string]interface{}, t time.Time) error
|
||||
Close()
|
||||
}
|
Reference in New Issue
Block a user