Ping InfluxDB server after connecting to recognize faulty connections

This commit is contained in:
Thomas Roehl 2022-02-25 13:51:52 +01:00
parent bac1f18b1d
commit fe3a8d59b0
2 changed files with 17 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package sinks
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
@ -64,6 +65,13 @@ func (s *InfluxAsyncSink) connect() error {
)
s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions)
s.writeApi = s.client.WriteAPI(s.config.Organization, s.config.Database)
ok, err := s.client.Ping(context.Background())
if err != nil {
return err
}
if !ok {
return fmt.Errorf("connection to %s not healthy", uri)
}
return nil
}
@ -108,7 +116,7 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) {
// Connect to InfluxDB server
if err := s.connect(); err != nil {
return nil, fmt.Errorf("Unable to connect: %v", err)
return nil, fmt.Errorf("unable to connect: %v", err)
}
// Start background: Read from error channel

View File

@ -54,6 +54,13 @@ func (s *InfluxSink) connect() error {
)
s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions)
s.writeApi = s.client.WriteAPIBlocking(s.config.Organization, s.config.Database)
ok, err := s.client.Ping(context.Background())
if err != nil {
return err
}
if !ok {
return fmt.Errorf("connection to %s not healthy", uri)
}
return nil
}
@ -94,7 +101,7 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) {
// Connect to InfluxDB server
if err := s.connect(); err != nil {
return nil, fmt.Errorf("Unable to connect: %v", err)
return nil, fmt.Errorf("unable to connect: %v", err)
}
return s, nil
}