41 lines
825 B
Go
41 lines
825 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||
|
"github.com/influxdata/influxdb-client-go/v2/api"
|
||
|
"github.com/influxdata/influxdb-client-go/v2/api/write"
|
||
|
)
|
||
|
|
||
|
type InfluxWriter struct {
|
||
|
client influxdb2.Client
|
||
|
write_api api.WriteAPIBlocking
|
||
|
channel chan *write.Point
|
||
|
}
|
||
|
|
||
|
func (w *InfluxWriter) Write(point *write.Point) {
|
||
|
w.channel <- point
|
||
|
}
|
||
|
|
||
|
func (w *InfluxWriter) Init(
|
||
|
url string,
|
||
|
token string,
|
||
|
org string,
|
||
|
bucket string,
|
||
|
) {
|
||
|
w.channel = make(chan *write.Point)
|
||
|
w.client = influxdb2.NewClient(url, token)
|
||
|
w.write_api = w.client.WriteAPIBlocking(org, bucket)
|
||
|
|
||
|
go func() {
|
||
|
for point := range w.channel {
|
||
|
err := w.write_api.WritePoint(context.Background(), point)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Error writing to InfluxDB: %s", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|