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..ccb3ac0 --- /dev/null +++ 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 new file mode 100644 index 0000000..525d671 --- /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() { + _, err := client.HealthCheck(context.Background()) + if err != nil { + log.Println("failed to health check:", err) + } + log.Println("the greptimedb is health") +}