From 2f6810453c9e8c639446d756f342f5307c09d7ef Mon Sep 17 00:00:00 2001 From: liyang Date: Wed, 11 Sep 2024 01:57:14 +0800 Subject: [PATCH 1/2] feat: add health check interface --- client.go | 24 +++++++++++++++++++ examples/README.md | 1 + examples/healthcheck/README.md | 0 examples/healthcheck/main.go | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 examples/healthcheck/README.md create mode 100644 examples/healthcheck/main.go diff --git a/client.go b/client.go index 91e7071..8897aa8 100644 --- a/client.go +++ b/client.go @@ -35,6 +35,8 @@ type Client struct { client gpb.GreptimeDatabaseClient stream gpb.GreptimeDatabase_HandleRequestsClient + + healthCheckClient gpb.HealthCheckClient } // NewClient helps to create the greptimedb client, which will be responsible write data into GreptimeDB. @@ -48,6 +50,17 @@ func NewClient(cfg *Config) (*Client, error) { return &Client{cfg: cfg, client: client}, nil } +// NewHealthCheckClient helps to create the health check client, which will be responsible checking GreptimeDB health status. +func NewHealthCheckClient(cfg *Config) (*Client, error) { + conn, err := grpc.Dial(cfg.endpoint(), cfg.build()...) + if err != nil { + return nil, err + } + + client := gpb.NewHealthCheckClient(conn) + return &Client{cfg: cfg, healthCheckClient: client}, nil +} + // submit is to build request and send it to GreptimeDB. // The operations can be set: // - INSERT @@ -284,3 +297,14 @@ func (c *Client) CloseStream(ctx context.Context) (*gpb.AffectedRows, error) { c.stream = nil return resp.GetAffectedRows(), nil } + +// HealthCheck will check GreptimeDB health status. +func (c *Client) HealthCheck(ctx context.Context) (*gpb.HealthCheckResponse, error) { + req := &gpb.HealthCheckRequest{} + resp, err := c.healthCheckClient.HealthCheck(ctx, req) + if err != nil { + return nil, err + } + + return resp, nil +} diff --git a/examples/README.md b/examples/README.md index 32499be..3a450de 100644 --- a/examples/README.md +++ b/examples/README.md @@ -15,6 +15,7 @@ docker run --rm -p 4000-4003:4000-4003 \ - [table](table/README.md) - [object](object/README.md) +- [healthcheck](healthcheck/README.md) ## Query diff --git a/examples/healthcheck/README.md b/examples/healthcheck/README.md new file mode 100644 index 0000000..e69de29 diff --git a/examples/healthcheck/main.go b/examples/healthcheck/main.go new file mode 100644 index 0000000..a0d4adc --- /dev/null +++ b/examples/healthcheck/main.go @@ -0,0 +1,44 @@ +// Copyright 2024 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "log" + + greptime "github.com/GreptimeTeam/greptimedb-ingester-go" +) + +var ( + client *greptime.Client +) + +func init() { + cfg := greptime.NewConfig("127.0.0.1").WithDatabase("public") + + cli_, err := greptime.NewHealthCheckClient(cfg) + if err != nil { + log.Panic(err) + } + client = cli_ +} + +func main() { + resp, err := client.HealthCheck(context.TODO()) + if err != nil { + log.Println(err) + } + log.Println("health check response:", resp) +} From 0b8c05465943a7851b8e73bf2f647dbab1048277 Mon Sep 17 00:00:00 2001 From: liyang Date: Wed, 11 Sep 2024 12:15:18 +0800 Subject: [PATCH 2/2] feat: add health check example --- examples/healthcheck/README.md | 13 +++++++++++++ examples/healthcheck/main.go | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/healthcheck/README.md b/examples/healthcheck/README.md index e69de29..ccb3ac0 100644 --- a/examples/healthcheck/README.md +++ b/examples/healthcheck/README.md @@ -0,0 +1,13 @@ +# Health check for GreptimeDB + +## Run Health Check + +```go +go run main.go +``` + +Output: + +```log +2024/09/11 12:13:11 the greptimedb is health +``` \ No newline at end of file diff --git a/examples/healthcheck/main.go b/examples/healthcheck/main.go index a0d4adc..525d671 100644 --- a/examples/healthcheck/main.go +++ b/examples/healthcheck/main.go @@ -36,9 +36,9 @@ func init() { } func main() { - resp, err := client.HealthCheck(context.TODO()) + _, err := client.HealthCheck(context.Background()) if err != nil { - log.Println(err) + log.Println("failed to health check:", err) } - log.Println("health check response:", resp) + log.Println("the greptimedb is health") }