Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from nsuprun/feature-check-influxdb
InfluxDB health check
- Loading branch information
Showing
7 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Package influxdb implements a health check for InfluxDB instance. | ||
package influxdb | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
"github.com/influxdata/influxdb-client-go/v2/domain" | ||
) | ||
|
||
// Config stores InfluxDB API host and possibly parameters. | ||
type Config struct { | ||
URL string | ||
} | ||
|
||
// New returns a check function. It uses InfluxDB health api | ||
// to get status of the instance. | ||
func New(config Config) func(ctx context.Context) error { | ||
return func(ctx context.Context) error { | ||
// since only health api will be used, we don't need to pass | ||
// any Authorization data (token in this case) | ||
client := influxdb2.NewClient(config.URL, "") | ||
defer client.Close() | ||
|
||
h, err := client.Health(ctx) | ||
|
||
if err != nil { | ||
return fmt.Errorf("InfluxDB health check failed: %w", err) | ||
} | ||
|
||
// any status different from "pass" is considered as failed | ||
if h.Status != domain.HealthCheckStatusPass { | ||
return errors.New("InfluxDB health check failed, didn't get PASS status") | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package influxdb | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const InfluxDbURLEnv = "HEALTH_GO_INFLUXDB_URL" | ||
|
||
func TestNew(t *testing.T) { | ||
check := New(Config{ | ||
URL: getURL(t), | ||
}) | ||
|
||
err := check(context.Background()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestNewWithError(t *testing.T) { | ||
check := New(Config{ | ||
URL: "", | ||
}) | ||
|
||
err := check(context.Background()) | ||
require.Error(t, err) | ||
} | ||
|
||
func getURL(t *testing.T) string { | ||
t.Helper() | ||
|
||
url, ok := os.LookupEnv(InfluxDbURLEnv) | ||
|
||
require.True(t, ok) | ||
|
||
return url | ||
} |