home_data/gatherer/piper.go

38 lines
933 B
Go

package main
import (
"context"
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api/write"
amqp "github.com/rabbitmq/amqp091-go"
)
func RunPipe(
channel <-chan amqp.Delivery,
url string, token string,
org string, bucket string,
conversion func(amqp.Delivery) ([]*write.Point, error),
) {
// Create a new client using an InfluxDB server base URL and an authentication token
client := influxdb2.NewClient(
url, token,
)
// Get blocking write client
writeAPI := client.WriteAPIBlocking(org, bucket)
// Create point using fluent style
for msg := range channel {
points, _ := conversion(msg)
// Write data
err := writeAPI.WritePoint(context.Background(), points...)
if err != nil {
fmt.Printf("Write error: %s", err.Error())
// reset writeAPI
writeAPI.Flush(context.Background())
writeAPI = client.WriteAPIBlocking(org, bucket)
}
}
}