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 package sinks
import ( import (
"context"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
@ -64,6 +65,13 @@ func (s *InfluxAsyncSink) connect() error {
) )
s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions) s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions)
s.writeApi = s.client.WriteAPI(s.config.Organization, s.config.Database) 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 return nil
} }
@ -108,7 +116,7 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) {
// Connect to InfluxDB server // Connect to InfluxDB server
if err := s.connect(); err != nil { 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 // 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.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions)
s.writeApi = s.client.WriteAPIBlocking(s.config.Organization, s.config.Database) 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 return nil
} }
@ -94,7 +101,7 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) {
// Connect to InfluxDB server // Connect to InfluxDB server
if err := s.connect(); err != nil { 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 return s, nil
} }