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 custom-healthcheck #41

Open
wants to merge 1 commit into
base: main
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
69 changes: 69 additions & 0 deletions custom-healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
"os"

"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport/grpc"
)

type HealthCheckService struct {
log *log.Helper
}

func NewHealthCheckService(logger log.Logger) *HealthCheckService {
return &HealthCheckService{
log: log.NewHelper(logger),
}
}

func (s *HealthCheckService) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
s.log.WithContext(ctx).Infof("HealthCheckService.Check: %v", req)

// TODO: add your health check logic here

return &grpc_health_v1.HealthCheckResponse{
Status: grpc_health_v1.HealthCheckResponse_SERVING,
}, nil
}

func (s *HealthCheckService) Watch(req *grpc_health_v1.HealthCheckRequest, srv grpc_health_v1.Health_WatchServer) error {
s.log.Infof("HealthCheckService.Watch: %v, %v", req, srv)

// simple implementation
resp := &grpc_health_v1.HealthCheckResponse{
Status: grpc_health_v1.HealthCheckResponse_SERVING,
}
if err := srv.Send(resp); err != nil {
return status.Errorf(codes.Unknown, "failed to send response: %v", err)
}
return nil
}

func main() {
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(),
grpc.CustomHealth(),
)
app := kratos.New(
kratos.Name("helloworld"),
kratos.Server(
grpcSrv,
),
)
logger := log.With(log.NewStdLogger(os.Stdout),
"ts", log.DefaultTimestamp,
"caller", log.DefaultCaller,
)
health := NewHealthCheckService(logger)
grpc_health_v1.RegisterHealthServer(grpcSrv, health)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}
43 changes: 43 additions & 0 deletions custom-healthcheck/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"context"
"github.com/go-kratos/kratos/v2/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
)

func main() {
// init grpc conn
conn, err := grpc.Dial(":9000", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
healthClient := grpc_health_v1.NewHealthClient(conn)
checkReq := &grpc_health_v1.HealthCheckRequest{
Service: "helloworld.Greeter",
}
ctx := context.Background()
checkResp, err := healthClient.Check(ctx, checkReq)
if err != nil {
panic(err)
}
log.Info("checkResp: ", checkResp)

watchReq := &grpc_health_v1.HealthCheckRequest{
Service: "helloworld.Greeter",
}
watchResp, err := healthClient.Watch(ctx, watchReq)
if err != nil {
panic(err)
}
// loop watch
for {
resp, err := watchResp.Recv()
if err != nil {
panic(err)
}
log.Info("watchResp: ", resp)
}
}