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

feat: add health check interface #45

Merged
merged 2 commits into from
Sep 11, 2024
Merged
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
24 changes: 24 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions examples/healthcheck/README.md
Original file line number Diff line number Diff line change
@@ -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
```
44 changes: 44 additions & 0 deletions examples/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading