Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ClickHouse health check #195

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Memcached
* InfluxDB
* Nats
* ClickHouse

## Usage

Expand Down Expand Up @@ -120,7 +121,7 @@ func main() {

r := chi.NewRouter()
r.Get("/status", h.HandlerFunc)
http.ListenAndServe(":3000", nil)
http.ListenAndServe(":3000", r)
}
```

Expand Down
3 changes: 3 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ tasks:
sh: docker-compose port cassandra 9042
NATS_HOST:
sh: docker-compose port nats 4222
CLICKHOUSE_HOST:
sh: docker-compose port clickhouse 9000

env:
CGO_ENABLED: '{{if .RACE}}1{{else}}0{{end}}'
HEALTH_GO_CLICKHOUSE_DSN: 'postgres://default@{{.CLICKHOUSE_HOST}}/default'
HEALTH_GO_PG_PQ_DSN: 'postgres://test:test@{{.PG_PQ_HOST}}/test?sslmode=disable'
HEALTH_GO_PG_PGX4_DSN: 'postgres://test:test@{{.PG_PGX4_HOST}}/test?sslmode=disable'
HEALTH_GO_PG_PGX5_DSN: 'postgres://test:test@{{.PG_PGX5_HOST}}/test?sslmode=disable'
Expand Down
53 changes: 53 additions & 0 deletions checks/clickhouse/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package clickhouse

import (
"context"
"fmt"

ch "github.com/ClickHouse/clickhouse-go/v2"
)

// Config is the ClickHouse checker configuration settings container.
type Config struct {
// DSN is the ClickHouse instance connection DSN. Required.
DSN string
}

// New creates a new ClickHouse health check that verifies the following:
// - connection establishing
// - doing the ping command
// - selecting clickhouse version
func New(config Config) func(ctx context.Context) error {
return func(ctx context.Context) (checkErr error) {
opts, err := ch.ParseDSN(config.DSN)
if err != nil {
return fmt.Errorf("ClickHouse health check failed to parse DSN: %w", err)
}

conn, err := ch.Open(opts)
if err != nil {
return fmt.Errorf("ClickHouse health check failed on connect: %w", err)
}

defer func() {
if err := conn.Close(); err != nil && checkErr == nil {
checkErr = fmt.Errorf("clickhouse health check failed on connection closing: %w", err)
}
}()

err = conn.Ping(ctx)
if err != nil {
checkErr = fmt.Errorf("ClickHouse health check failed on ping: %w", err)
return
}

rows, err := conn.Query(ctx, `SELECT version()`)
if err != nil {
checkErr = fmt.Errorf("ClickHouse health check failed on select: %w", err)
return
}
defer rows.Close()

return
}
}
76 changes: 76 additions & 0 deletions checks/clickhouse/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package clickhouse

import (
"context"
"os"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ch "github.com/ClickHouse/clickhouse-go/v2"
)

const chDSNEnv = "HEALTH_GO_CLICKHOUSE_DSN"

func TestNew(t *testing.T) {
initDB(t)

check := New(Config{
DSN: getDSN(t),
})

err := check(context.Background())
require.NoError(t, err)
}

func TestNewWithError(t *testing.T) {
check := New(Config{})

err := check(context.Background())
require.Error(t, err)
}

func getDSN(t *testing.T) string {
t.Helper()

chDSN, ok := os.LookupEnv(chDSNEnv)
require.True(t, ok)

return chDSN
}

var dbInit sync.Once

func initDB(t *testing.T) {
t.Helper()

dbInit.Do(func() {
ctx := context.Background()

// 1. Parse DSN
opts, err := ch.ParseDSN(getDSN(t))
require.NoError(t, err)

// 2. Acquire connection
conn, err := ch.Open(opts)
require.NoError(t, err)

defer func() {
err := conn.Close()
assert.NoError(t, err)
}()

// 3. Create test table
err = conn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS test_clickhouse (
id Int64,
timestamp DateTime
)
ENGINE = MergeTree()
ORDER BY id;`)
require.NoError(t, err)

})
}
14 changes: 13 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,16 @@ services:
image: nats:2.9.11
command: "-js -sd /data"
ports:
- "4222:4222"
- "4222:4222"

clickhouse:
container_name: clickhouse
image: clickhouse/clickhouse-server:24.5
ports:
- "9000"
healthcheck:
test: ['CMD', 'wget', '--spider', '-q', '127.0.0.1:8123/ping']
interval: 2s
retries: 30
start_period: 5s
timeout: 5s
32 changes: 21 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module github.com/hellofresh/health-go/v5

go 1.20
go 1.21

toolchain go1.22.4

require (
github.com/ClickHouse/clickhouse-go/v2 v2.29.0
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
github.com/go-sql-driver/mysql v1.7.1
github.com/gocql/gocql v1.6.0
Expand All @@ -16,16 +19,20 @@ require (
github.com/stretchr/testify v1.9.0
github.com/vitorsalgado/mocha/v2 v2.0.2
go.mongodb.org/mongo-driver v1.14.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
go.opentelemetry.io/otel v1.26.0
go.opentelemetry.io/otel/trace v1.26.0
google.golang.org/grpc v1.62.1
)

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand All @@ -38,25 +45,28 @@ require (
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/oapi-codegen/runtime v1.1.0 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading