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

Modify health check endpoint and add HEAD method #958

Merged
merged 14 commits into from
Aug 15, 2024

Conversation

taeng0204
Copy link
Contributor

@taeng0204 taeng0204 commented Aug 10, 2024

What this PR does / why we need it:
In PR #952 , There were issues with existing health checks needing to support HEAD methods for UptimeRobot and overlapping with envoy's health check path.
So I added the HEAD method and test code, and modified the health check endpoint.

Which issue(s) this PR fixes:

Fixes #832

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Introduced a new HTTP health check handler, allowing services to respond to health check requests via GET and HEAD methods.
    • Enhanced server functionality with the registration of an additional health check endpoint.
  • Bug Fixes

    • Improved clarity and structure of the health check response handling.
  • Tests

    • Expanded health check tests to include both gRPC and HTTP checks for multiple services, improving coverage and robustness.

taeng0204 and others added 10 commits August 5, 2024 12:33
Added a handler to allow health checks to be performed with plain HTTP GET
requests, rather than health checks using rpc.
Rename existing names used by other packages for lint
The package name includes health, so we remove health from the handler function
Write a health check for each service and a check failure test for the unknown service.
Modified test functions for each service to simplify them
Added the handler to allow health checks to be performed with plain HTTP GET requests needed for traditional uptime checker or load balancer, along with existing gRPC health check.
Need HEAD method for UptimeRobot and fix path overlap with Envoy
In the existing test code, the order of expected and actual values is different,
which makes it inconvenient to check the log when the test fails.
Copy link

coderabbitai bot commented Aug 10, 2024

Walkthrough

The recent updates introduce a plain HTTP health check endpoint to the server, enhancing monitoring capabilities by allowing both HTTP GET and HEAD requests. This functionality complements the existing gRPC health checks, providing a user-friendly method for uptime verification. The changes include new handlers and tests, ensuring robust validation for service health across different protocols.

Changes

Files Change Summary
server/rpc/httphealth/httphealth.go Introduced HTTP health check handler with NewHandler for service health checks using GET/HEAD.
server/rpc/server.go Modified NewServer to include integration of the new HTTP health check handler alongside gRPC.
test/integration/health_test.go Expanded health check tests to cover both gRPC and HTTP methods with new test functions.

Assessment against linked issues

Objective Addressed Explanation
Add Plain HTTP Health Check Endpoint (#832)

🐰 In the meadow, where bunnies hop,
A health check blooms, no need to stop!
With GET and HEAD, we watch and play,
Uptime’s a joy, come what may!
Hooray for health, let’s cheer and sing,
Monitoring made easy, oh what joy it brings! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
server/rpc/httphealth/httphealth.go (1)

33-64: Recommendation: Validate the service parameter in NewHandler.

The NewHandler function in server/rpc/httphealth/httphealth.go does not validate the service parameter. This can lead to potential security vulnerabilities. Consider adding validation logic to ensure the service parameter is safe and expected before using it.

  • File: server/rpc/httphealth/httphealth.go
  • Function: NewHandler
Analysis chain

Ensure proper error handling and response construction.

The NewHandler function is well-implemented, supporting both GET and HEAD methods. However, consider the following improvements:

  1. Error Handling: Ensure that all potential errors are logged for better debugging.
  2. Security: Validate and sanitize the service parameter to prevent injection attacks.
  3. Performance: Consider using a sync.Pool for CheckResponse to reduce allocations in high-load scenarios.

Overall, the function is correctly handling HTTP methods and constructing responses.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `NewHandler` function and ensure the service parameter is validated.

# Test: Search for the `NewHandler` function usage. Expect: Proper validation of the service parameter.
rg --type go 'NewHandler'

Length of output: 440


Script:

#!/bin/bash
# Extract lines around the usage of NewHandler in server/rpc/server.go to check for service parameter validation.
rg --type go -A 10 -B 10 'NewHandler' server/rpc/server.go

Length of output: 1011

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 80c6ea0 and 592302a.

Files selected for processing (3)
  • server/rpc/httphealth/httphealth.go (1 hunks)
  • server/rpc/server.go (2 hunks)
  • test/integration/health_test.go (2 hunks)
Additional comments not posted (4)
server/rpc/server.go (1)

Line range hint 39-77:
Verify server configuration and integration of new health check handler.

The integration of the HTTP health check handler is correctly done. However, consider the following:

  1. Configuration: Ensure that the server configuration, especially for the new health check route, is thoroughly tested.
  2. Security: Verify that the health check endpoint is secured appropriately, especially if exposed to the public.

Overall, the integration appears sound, but verification is recommended to ensure robustness.

test/integration/health_test.go (3)

Line range hint 42-79:
Comprehensive gRPC health check tests.

The TestRPCHealthCheck function is well-implemented, covering default, specific, and unknown services. Ensure that any additional services are added to the services list for comprehensive coverage.


81-125: Thorough HTTP GET health check tests.

The TestHTTPGETHealthCheck function is comprehensive, testing default, specific, and unknown services. The use of sub-tests enhances clarity and reporting.


127-160: Comprehensive HTTP HEAD health check tests.

The TestHTTPHEADHealthCheck function is well-implemented, covering default, specific, and unknown services. The tests are consistent with the GET tests, ensuring robust validation.

@krapie krapie added the enhancement 🌟 New feature or request label Aug 10, 2024
@krapie
Copy link
Member

krapie commented Aug 10, 2024

@taeng0204 Could you please resolve the conflicts?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 592302a and 599cf2a.

Files selected for processing (1)
  • test/integration/health_test.go (4 hunks)
Additional comments not posted (3)
test/integration/health_test.go (3)

Line range hint 48-79:
LGTM!

The restructuring of the gRPC health check tests into sub-tests is well-organized and enhances readability.


81-125: LGTM!

The implementation of HTTP GET health checks with sub-tests and response decoding is clear and effective.


127-161: LGTM!

The implementation of HTTP HEAD health checks with sub-tests is well-structured and effective.

Copy link

codecov bot commented Aug 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 51.06%. Comparing base (80c6ea0) to head (505a40b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #958   +/-   ##
=======================================
  Coverage   51.06%   51.06%           
=======================================
  Files          73       73           
  Lines       10782    10782           
=======================================
  Hits         5506     5506           
  Misses       4725     4725           
  Partials      551      551           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@krapie krapie self-requested a review August 11, 2024 11:11
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.

I think we need to change the service endpoint for the readability and consistency with the other RPCs.

// CheckResponse represents the response structure for health checks.
type CheckResponse struct {
Status string `json:"status"`
}

// NewHandler creates a new HTTP handler for health checks.
func NewHandler(checker grpchealth.Checker) (string, http.Handler) {
const serviceName = "/yorkie.v1/healthz/"
Copy link
Member

Choose a reason for hiding this comment

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

How about changing the service name to /yorkie.v1.Health/Check to correspond with /grpc.health.v1.Health/Check and /yorkie.v1.YorkieService/XXX to keep consistency with other RPCs?

Also, I think we don't need the ending / slash, since we are using ? query parameter after.

@krapie
Copy link
Member

krapie commented Aug 11, 2024

@taeng0204 I think we should include that the health check is within Yorkie service to state that this health check is related to Yorkie, so /yorkie.v1.YorkieService/health or other names that includes /yorkie.v1.YorkieService will be better.

@taeng0204
Copy link
Contributor Author

@krapie In the existing grpc health check, there is a grpc.health.v1.Health that supports health checks for the yorkie service, admin service, and health check service, so why not http.health.v1.Health instead?

@krapie
Copy link
Member

krapie commented Aug 12, 2024

@taeng0204 That is also a good approach. When we proceeding with http.health.v1.Health, we need reconfigure yorkie cluster's routing conditions.

Note that we are using /yorkie.v1 for yorkie service route, /grafana for Grafana dashbaord, and /argocd for ArgoCD dashboard.

@taeng0204
Copy link
Contributor Author

taeng0204 commented Aug 13, 2024

@krapie thanks for the answer:)
I've been thinking a lot about it, and I think it's better to integrate it into the existing /yorkie.v1. This code was written for a special situation for ncp lb, and I don't think it changes the existing system too much.
I'll fix the code:)

Modified the endpoint for consistency with gRPC
@taeng0204
Copy link
Contributor Author

@krapie I tried modifying it like grpchealth's code, but I'm not sure if it's okay to use the variable names and comments as is.

// HealthV1ServiceName is the fully-qualified name of the v1 version of the health service.
const HealthV1ServiceName = "/yorkie.v1.YorkieService/health"

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 599cf2a and 505a40b.

Files selected for processing (2)
  • server/rpc/httphealth/httphealth.go (2 hunks)
  • test/integration/health_test.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • server/rpc/httphealth/httphealth.go
Additional comments not posted (3)
test/integration/health_test.go (3)

Line range hint 41-79:
LGTM! Enhanced test coverage for gRPC health checks.

The restructuring of the health check tests into sub-tests for each service improves clarity and coverage.


81-125: LGTM! Comprehensive HTTP GET health check tests.

The addition of HTTP GET health checks for multiple services, including error handling for unknown services, is well-implemented.


127-161: LGTM! Well-structured HTTP HEAD health check tests.

The addition of HTTP HEAD health checks for multiple services, including error handling for unknown services, is well-implemented.

@taeng0204 taeng0204 requested a review from krapie August 13, 2024 16:02
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

I'm not sure if it's okay to use the variable names and comments as is.

It is fully okay 👍🏼
We now only have one last change before the merge :)

@@ -34,36 +34,34 @@ type CheckResponse struct {

// NewHandler creates a new HTTP handler for health checks.
func NewHandler(checker grpchealth.Checker) (string, http.Handler) {
const serviceName = "/yorkie.v1.YorkieService/health"
Copy link
Member

Choose a reason for hiding this comment

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

I think now we have introduced HealthV1ServiceName constant, we can remove this variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It preserves the structure of the existing grpchealth, which we don't need! Thanks:)

We only left one duplicate const service name.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 505a40b and 9573d8f.

Files selected for processing (1)
  • server/rpc/httphealth/httphealth.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • server/rpc/httphealth/httphealth.go

@taeng0204 taeng0204 requested a review from krapie August 15, 2024 07:40
Copy link
Member

@krapie krapie left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏼

@krapie krapie merged commit ad23c56 into yorkie-team:main Aug 15, 2024
4 checks passed
raararaara pushed a commit that referenced this pull request Oct 7, 2024
Change health check endpoint to include yorkie service endpoint along with HEAD method to support various health checkers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🌟 New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add Plain HTTP Health Check Endpoint
2 participants