Skip to content

Commit

Permalink
feat: add health check interface (#45)
Browse files Browse the repository at this point in the history
* feat: add health check interface

* feat: add health check example
  • Loading branch information
daviderli614 authored Sep 11, 2024
1 parent e3578dc commit fa24df2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
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")
}

0 comments on commit fa24df2

Please sign in to comment.