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(transport/grpc): support custom healthcheck #2541

Merged
merged 4 commits into from
Nov 28, 2022
Merged

feat(transport/grpc): support custom healthcheck #2541

merged 4 commits into from
Nov 28, 2022

Conversation

perriea
Copy link
Contributor

@perriea perriea commented Nov 25, 2022

Description (what this PR does / why we need it):

Currently, it is not possible to define custom health checks. This PR should solve that.

Which issue(s) this PR fixes (resolves / be part of):

resolves #1397

@shenqidebaozi
Copy link
Member

You can use it as an extension configuration instead of a default parameter. Your modifications introduce incompatible changes.

@codecov-commenter
Copy link

codecov-commenter commented Nov 26, 2022

Codecov Report

Merging #2541 (eb224fa) into main (c530d63) will decrease coverage by 0.06%.
The diff coverage is 40.00%.

@@            Coverage Diff             @@
##             main    #2541      +/-   ##
==========================================
- Coverage   85.23%   85.16%   -0.07%     
==========================================
  Files          88       88              
  Lines        3826     3830       +4     
==========================================
+ Hits         3261     3262       +1     
- Misses        391      394       +3     
  Partials      174      174              
Impacted Files Coverage Δ
transport/grpc/server.go 83.33% <40.00%> (-2.54%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@perriea
Copy link
Contributor Author

perriea commented Nov 26, 2022

@shenqidebaozi It's better now ? :)
I can create an example in https://github.com/go-kratos/examples, if you want.

// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.SplitsService, ghealth *service.HealthService, logger hclog.Logger) *grpc.Server {
	var (
		log  hclog.Logger = logger.Named("NewGRPCServer")
		opts              = []grpc.ServerOption{
			grpc.CustomHealth(),
			grpc.Middleware(
				metadata.Server(),
				recovery.Recovery(),
				validate.Validator(),
				metrics.Server(
					metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
					metrics.WithRequests(prom.NewCounter(_metricRequests)),
				),
			),
		}
	)

	if c.Grpc.Network != "" {
		opts = append(opts, grpc.Network(c.Grpc.Network))
	}

	if c.Grpc.Hostname != "" {
		opts = append(opts, grpc.Address(c.Grpc.Hostname))
	}

	if c.Grpc.Timeout != nil {
		opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
	}

	srv := grpc.NewServer(opts...)
	srv.Use("/splits.v1.Splits/*", auth.MiddlewareOptions(log, c.Keycloak))
	srv.Use("/grpc.health.v1.Health/*")

	v1.RegisterSplitsServer(srv, greeter)
	grpc_health_v1.RegisterHealthServer(srv, ghealth)

	return srv
}

@shenqidebaozi
Copy link
Member

@shenqidebaozi It's better now ? :) I can create an example in https://github.com/go-kratos/examples, if you want.

// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.SplitsService, ghealth *service.HealthService, logger hclog.Logger) *grpc.Server {
	var (
		log  hclog.Logger = logger.Named("NewGRPCServer")
		opts              = []grpc.ServerOption{
			grpc.CustomHealth(),
			grpc.Middleware(
				metadata.Server(),
				recovery.Recovery(),
				validate.Validator(),
				metrics.Server(
					metrics.WithSeconds(prom.NewHistogram(_metricSeconds)),
					metrics.WithRequests(prom.NewCounter(_metricRequests)),
				),
			),
		}
	)

	if c.Grpc.Network != "" {
		opts = append(opts, grpc.Network(c.Grpc.Network))
	}

	if c.Grpc.Hostname != "" {
		opts = append(opts, grpc.Address(c.Grpc.Hostname))
	}

	if c.Grpc.Timeout != nil {
		opts = append(opts, grpc.Timeout(c.Grpc.Timeout.AsDuration()))
	}

	srv := grpc.NewServer(opts...)
	srv.Use("/splits.v1.Splits/*", auth.MiddlewareOptions(log, c.Keycloak))
	srv.Use("/grpc.health.v1.Health/*")

	v1.RegisterSplitsServer(srv, greeter)
	grpc_health_v1.RegisterHealthServer(srv, ghealth)

	return srv
}

@perriea This looks good. Can you submit it as well?

Copy link
Member

@shenqidebaozi shenqidebaozi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shenqidebaozi shenqidebaozi merged commit d779faf into go-kratos:main Nov 28, 2022
xiongpan828 pushed a commit to xiongpan828/kratos that referenced this pull request Jan 5, 2023
* feat(transport/grpc): support custom healthcheck

* fix: parameters grpc server

* fix(transport/grpc): optional CustomHealth

* fix(transport/grpc): useless argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Question] Why does grpc server not open the configuration of health Serveroption?
4 participants