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

LNK-2883: Added Kafka health check to .NET services #494

Merged
merged 5 commits into from
Oct 7, 2024
Merged

Conversation

dvargaslantana
Copy link
Contributor

@dvargaslantana dvargaslantana commented Oct 4, 2024

Also added a missing health check endpoint to the Submission service.

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced health monitoring capabilities by integrating Kafka health checks across multiple services.
    • Added a dedicated Kafka topic for health checks in the Docker configuration.
  • Bug Fixes

    • Improved logging setup to exclude specific request paths related to health checks.
  • Documentation

    • Updated comments throughout the code for clarity on health check configurations.

These enhancements improve the application's reliability and monitoring of Kafka services, ensuring better operational oversight.

Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

Walkthrough

The pull request introduces modifications across multiple Program.cs files in various DotNet projects to enhance health check functionalities for Kafka services. A new using directive for LantanaGroup.Link.Shared.Application.Health is added, and a KafkaHealthCheckConfiguration variable is instantiated in the RegisterServices method. This variable is used to register Kafka health checks alongside existing database health checks. Additionally, a new class KafkaHealthCheckConfiguration is introduced to facilitate these health checks, and the docker-compose.yml file is updated to include a dedicated Kafka topic for health monitoring.

Changes

File Path Change Summary
DotNet/Account/Program.cs Added using directive; instantiated kafkaHealthOptions for Kafka health check registration.
DotNet/Audit/Program.cs Added using directive; updated RegisterServices and SetupMiddleware methods for Kafka checks.
DotNet/Census/Program.cs Added using directive; updated health checks to include Kafka.
DotNet/DataAcquisition/Program.cs Added using directive; modified health checks to include Kafka health check.
DotNet/Normalization/Program.cs Added using directive; updated health checks to include Kafka.
DotNet/Notification/Program.cs Added using directive; updated health check registration to include Kafka.
DotNet/QueryDispatch/Program.cs Added using directives; updated health checks and logging setup for Kafka.
DotNet/Report/Program.cs Added using directive; updated health checks to include Kafka.
DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs Added KafkaHealthCheckConfiguration class with methods for health check options.
DotNet/Shared/Shared.csproj Added package reference for AspNetCore.HealthChecks.Kafka.
DotNet/Submission/Program.cs Added using directive; configured health checks and middleware for Kafka.
DotNet/Tenant/Program.cs Added using directive; updated health checks to include Kafka.
docker-compose.yml Added Kafka topic Service-Healthcheck and updated health check command for kafka_b service.

Possibly related PRs

Suggested reviewers

  • edward-miller-lcg
  • amphillipsLGC
  • arianamihailescu

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9ad5acb and ea7d3f4.

📒 Files selected for processing (1)
  • DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@dvargaslantana dvargaslantana changed the title LNK-2883 : Added Kafka health check to .NET services LNK-2883: Added Kafka health check to .NET services Oct 4, 2024
Copy link
Contributor

@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: 3

🧹 Outside diff range and nitpick comments (7)
DotNet/Shared/Shared.csproj (1)

21-21: LGTM! Consider sorting package references alphabetically.

The addition of the AspNetCore.HealthChecks.Kafka package (version 8.0.1) aligns perfectly with the PR objective of adding Kafka health checks to .NET services. The package version is compatible with the project's target framework (.NET 8.0), and there are no apparent conflicts with other package references.

As a minor suggestion for improved readability and consistency, consider sorting all package references alphabetically.

DotNet/Submission/Program.cs (2)

153-160: Health check configuration looks good, consider adding error handling.

The Kafka health check configuration is well-implemented:

  • It correctly uses the existing Kafka connection settings.
  • The health check is properly added to the service collection.

However, consider adding error handling for the case where the Kafka connection configuration might be missing:

var kafkaConnection = builder.Configuration.GetRequiredSection(KafkaConstants.SectionName).Get<KafkaConnection>();
if (kafkaConnection == null)
{
    throw new InvalidOperationException("Kafka connection configuration is missing.");
}

This will provide a more informative error message if the configuration is missing.


Line range hint 1-234: Consider adding unit tests for health check functionality.

While the changes look good, it's important to ensure that the newly added health check functionality is properly tested. Consider adding unit tests to verify:

  1. The health check configuration is correctly set up.
  2. The health check endpoint responds as expected.
  3. The Kafka health check accurately reflects the state of the Kafka connection.

This will help ensure the reliability of the health check feature and catch any potential issues early.

Would you like assistance in generating sample unit tests for the health check functionality?

DotNet/Tenant/Program.cs (1)

176-180: LGTM: Kafka health check added correctly.

The Kafka health check configuration and registration are implemented correctly. Good job on preserving the existing database health check while adding the new Kafka health check.

For improved readability, consider extracting the health checks configuration into a separate method.

Here's a suggested refactor for improved readability:

private static void ConfigureHealthChecks(IServiceCollection services, KafkaConnection kafkaConnection)
{
    var kafkaHealthOptions = new KafkaHealthCheckConfiguration(kafkaConnection, TenantConstants.ServiceName).GetHealthCheckOptions();

    services.AddHealthChecks()
        .AddCheck<DatabaseHealthCheck>("Database")
        .AddKafka(kafkaHealthOptions);
}

Then, in the RegisterServices method, you can call:

ConfigureHealthChecks(builder.Services, kafkaConnection);
DotNet/Report/Program.cs (1)

172-173: LGTM: Kafka health check options added.

The addition of kafkaHealthOptions using KafkaHealthCheckConfiguration is consistent with the PR objective. It correctly uses the existing kafkaConnection and ReportConstants.ServiceName to configure the health check options.

Consider adding a blank line after this declaration for improved readability:

var kafkaHealthOptions = new KafkaHealthCheckConfiguration(kafkaConnection, ReportConstants.ServiceName).GetHealthCheckOptions();
+
DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs (2)

18-40: Add XML documentation comments to public method.

Adding XML documentation comments to the GetHealthCheckOptions method will enhance maintainability and provide better IntelliSense support for consumers of this class.

Example:

/// <summary>
/// Constructs and returns the Kafka health check options.
/// </summary>
/// <returns>A configured <see cref="KafkaHealthCheckOptions"/> instance.</returns>
public KafkaHealthCheckOptions GetHealthCheckOptions()
{
    // Method implementation...
}

7-46: Add XML documentation comments to the class.

Including XML documentation for the KafkaHealthCheckConfiguration class will improve code readability and assist other developers in understanding the purpose and usage of the class.

Example:

/// <summary>
/// Provides configuration settings for Kafka health checks.
/// </summary>
public class KafkaHealthCheckConfiguration
{
    // Class implementation...
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between be3006a and 9ad5acb.

📒 Files selected for processing (13)
  • DotNet/Account/Program.cs (2 hunks)
  • DotNet/Audit/Program.cs (2 hunks)
  • DotNet/Census/Program.cs (2 hunks)
  • DotNet/DataAcquisition/Program.cs (2 hunks)
  • DotNet/Normalization/Program.cs (2 hunks)
  • DotNet/Notification/Program.cs (2 hunks)
  • DotNet/QueryDispatch/Program.cs (2 hunks)
  • DotNet/Report/Program.cs (2 hunks)
  • DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs (1 hunks)
  • DotNet/Shared/Shared.csproj (1 hunks)
  • DotNet/Submission/Program.cs (3 hunks)
  • DotNet/Tenant/Program.cs (2 hunks)
  • docker-compose.yml (1 hunks)
🔇 Additional comments (40)
DotNet/Submission/Program.cs (2)

8-8: LGTM: Health check namespace added.

The new using statement for LantanaGroup.Link.Shared.Application.Health is correctly added and necessary for the health check functionality being introduced.


230-234: Health check middleware setup looks good, verify CORS policy.

The health check middleware is well-configured:

  • The /health endpoint is correctly mapped.
  • Using UIResponseWriter.WriteHealthCheckUIResponse ensures consistent health check responses.
  • Requiring a CORS policy is good for security.

However, please verify that the "HealthCheckPolicy" is defined elsewhere in the application. If not, you should define it or use an existing policy. For example:

app.UseCors(policy => policy
    .WithOrigins("http://example.com")
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials());

app.MapHealthChecks("/health", new HealthCheckOptions
{
    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
}).RequireCors(policy);

Replace "http://example.com" with the appropriate origin(s) for your application.

DotNet/Tenant/Program.cs (2)

33-33: LGTM: New using directive added for health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is appropriate for implementing Kafka health checks.


Line range hint 1-280: Summary: Kafka health check successfully implemented.

The changes in this PR successfully add Kafka health checks to the .NET Tenant service, aligning with the PR objective (LNK-2883). The implementation is correct and well-integrated with the existing health check system.

Key points:

  1. Appropriate using directive added for health checks.
  2. Kafka health check configuration and registration implemented correctly.
  3. Existing health checks (e.g., database) preserved.

The code changes are minimal and focused, which is good for maintainability. The suggested refactoring for improved readability is optional but could enhance the code structure.

Overall, this PR achieves its goal effectively and can be approved after addressing the minor suggestion if desired.

DotNet/Audit/Program.cs (4)

45-45: LGTM: New using directive added for Kafka health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is appropriate for implementing Kafka health checks.


185-186: LGTM: Kafka health check configuration added.

The addition of Kafka health check configuration using KafkaHealthCheckConfiguration is appropriate. It correctly uses the existing kafkaConnection and AuditConstants.ServiceName to create the configuration.


188-189: LGTM: Kafka health check added to health checks registration.

The addition of the Kafka health check using AddKafka(kafkaHealthOptions) alongside the existing database health check is appropriate. This enhances the application's health monitoring capabilities.


45-45: Summary: Kafka health check implementation is well-integrated and follows best practices.

The changes to add Kafka health checks to the application are well-implemented:

  1. The necessary using directive is added.
  2. Kafka health check configuration is properly set up.
  3. The health checks registration is updated to include the Kafka health check.

These changes enhance the application's health monitoring capabilities without disrupting the existing functionality.

Also applies to: 185-189

DotNet/Account/Program.cs (4)

24-24: LGTM: New using directive for health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is appropriate for implementing Kafka health checks.


192-193: LGTM: Kafka health check options configured.

The kafkaHealthOptions variable is correctly instantiated using KafkaHealthCheckConfiguration with appropriate parameters. This setup will enable Kafka-specific health checks.


195-196: LGTM: Kafka health check added to configuration.

The health checks configuration has been correctly updated to include both the existing database check and the new Kafka health check. This ensures comprehensive monitoring of critical system components.


192-196: Verify the impact of Kafka health checks on system monitoring.

The implementation of Kafka health checks looks good. To ensure it functions as expected:

  1. Confirm that the Kafka health check doesn't negatively impact the existing health check performance.
  2. Verify that the health check endpoint (/health) now includes Kafka status.
  3. Test the behavior when Kafka is unavailable to ensure proper error handling.

To assist with verification, you can run the following script:

This script will help you verify the health check implementation and ensure that Kafka health status is properly included in the response.

DotNet/Normalization/Program.cs (5)

42-42: LGTM: New using directive for health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is appropriate for implementing Kafka health checks.


215-215: LGTM: Kafka connection retrieval.

The Kafka connection is correctly retrieved from the configuration using GetRequiredSection, which ensures the section exists. This is good practice for fail-fast behavior.


216-216: LGTM: Kafka health check options creation.

The Kafka health check options are correctly created using the KafkaHealthCheckConfiguration class, which centralizes the configuration and makes it easier to manage.


218-221: LGTM: Kafka health check added.

The Kafka health check is correctly added to the existing health checks using the AddKafka method with the previously created kafkaHealthOptions. This addition enhances the service's health monitoring capabilities.


42-42: Summary: Kafka health checks successfully implemented.

The changes effectively implement Kafka health checks for the Normalization service:

  1. Necessary using directive added.
  2. Kafka connection retrieved from configuration.
  3. Kafka health check options created with appropriate parameters.
  4. Kafka health check added to the existing health checks.

These changes enhance the service's health monitoring capabilities by including Kafka alongside the existing database health check. The implementation is clean, follows good practices, and aligns well with the PR objective.

Also applies to: 215-221

DotNet/Census/Program.cs (4)

47-47: LGTM: Import statement for Kafka health check added.

The new import statement for LantanaGroup.Link.Shared.Application.Health is correctly placed and necessary for the Kafka health check functionality.


187-188: LGTM: Kafka health check configuration added.

The Kafka health check configuration is correctly set up using the existing kafkaConnection and CensusConstants.ServiceName. The variable name kafkaHealthOptions is clear and descriptive.


190-191: LGTM: Kafka health check added to health checks builder.

The Kafka health check is correctly added to the existing health checks configuration using the kafkaHealthOptions created earlier. It's appropriately chained after the database health check.


47-47: Summary: Kafka health check successfully implemented.

The changes in this file successfully implement the Kafka health check as per the PR objective. The implementation includes:

  1. Adding the necessary import statement.
  2. Creating a Kafka health check configuration.
  3. Integrating the Kafka health check into the existing health checks builder.

These changes enhance the application's health monitoring capabilities by including Kafka connectivity in the health checks. The implementation is clean, well-integrated, and follows good practices.

Also applies to: 187-191

DotNet/Notification/Program.cs (4)

41-41: LGTM: Import statement for Kafka health check added.

The import of LantanaGroup.Link.Shared.Application.Health is correctly added and necessary for implementing the Kafka health check.


202-203: LGTM: Kafka health check configuration added.

The Kafka health check configuration is correctly implemented using the existing kafkaConnection and the service name constant. This approach ensures consistency and allows for centralized configuration.


205-206: LGTM: Kafka health check added to the service configuration.

The Kafka health check is correctly added to the existing health checks configuration. The database health check is preserved, and the new Kafka health check is properly integrated using the AddKafka method with the previously created kafkaHealthOptions.


41-41: Summary: Kafka health check successfully implemented.

The changes in this file successfully implement the Kafka health check as per the PR objective. The implementation includes:

  1. Adding the necessary import statement.
  2. Creating a Kafka health check configuration.
  3. Integrating the Kafka health check into the existing health checks service.

The changes are well-structured, follow good practices, and maintain consistency with the existing codebase.

Also applies to: 202-206

DotNet/Report/Program.cs (3)

20-20: LGTM: New using statement for health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is consistent with the PR objective of adding Kafka health checks to the .NET services.


175-176: LGTM: Kafka health check added to the configuration.

The addition of .AddKafka(kafkaHealthOptions) to the health checks configuration is correct and consistent with the PR objective. It's properly chained to the existing health checks and uses the kafkaHealthOptions created earlier.


20-20: Summary: Kafka health checks successfully implemented.

The changes in this PR successfully implement Kafka health checks for the .NET services as intended. The implementation includes:

  1. Adding the necessary using statement for health-related functionality.
  2. Creating Kafka health check options using the existing Kafka connection and service name.
  3. Adding the Kafka health check to the existing health checks configuration.

These changes enhance the service's health monitoring capabilities without affecting the existing functionality. The implementation is clean, focused, and follows good practices.

Also applies to: 172-176

DotNet/DataAcquisition/Program.cs (4)

54-54: LGTM: New using directive for Kafka health checks.

The addition of using LantanaGroup.Link.Shared.Application.Health; is appropriate for implementing Kafka health checks.


342-344: LGTM: Kafka health check configuration setup.

The Kafka health check configuration is properly set up using the correct connection settings and service name.


346-347: LGTM: Health checks configuration updated with Kafka.

The health checks configuration has been correctly updated to include both the existing DbContextCheck and the new Kafka health check.


54-54: Summary: Kafka health checks successfully implemented.

The changes effectively implement Kafka health checks in the Data Acquisition service:

  1. Added the necessary using directive for Kafka health checks.
  2. Set up the Kafka health check configuration using the correct connection settings.
  3. Updated the health checks configuration to include both existing and new Kafka checks.

These changes enhance the service's health monitoring capabilities by including Kafka connectivity in the health checks.

Also applies to: 342-347

docker-compose.yml (4)

267-267: LGTM! Consider clarifying the retention period.

The addition of the "Service-Healthcheck" topic aligns with the PR objective. However, the retention period of 1ms is extremely short. While this might be intentional for health check purposes, it's worth confirming if this is the desired behavior.

Could you please clarify the reasoning behind the 1ms retention period for the "Service-Healthcheck" topic?


Line range hint 232-237: LGTM! Healthcheck addition improves service reliability.

The addition of the healthcheck to the kafka_b service is a good improvement. It ensures that the Kafka broker is functioning correctly by verifying that it can list topics. The frequency, timeout, and retry settings are reasonable for a healthcheck.

🧰 Tools
🪛 yamllint

[error] 269-269: trailing spaces

(trailing-spaces)


267-267: Verify usage of the new health check topic in .NET services.

The new "Service-Healthcheck" topic has been added, but it's not clear which services will be using it. To ensure full implementation of the Kafka health check for .NET services (as mentioned in the PR objective), it would be beneficial to verify that the relevant .NET services are configured to use this new topic.

Could you please confirm which .NET services are expected to use the new "Service-Healthcheck" topic and ensure they are properly configured to do so?


Line range hint 1-267: Summary: Kafka health check implementation looks good, with a few points to verify.

The changes to add Kafka health check capabilities to the .NET services have been implemented in the docker-compose.yml file. The main modifications include:

  1. Adding a new "Service-Healthcheck" topic in the kafka_init service.
  2. Implementing a healthcheck for the kafka_b service.

These changes align with the PR objective (LNK-2883: Added Kafka health check to .NET services). However, there are a few points that require attention:

  1. The retention period of 1ms for the "Service-Healthcheck" topic is unusually short. Please confirm if this is intentional.
  2. Verify which .NET services are expected to use the new "Service-Healthcheck" topic and ensure they are properly configured to do so.

Once these points are addressed, the implementation looks solid and should improve the overall reliability of the Kafka-related services in the system.

🧰 Tools
🪛 yamllint

[error] 269-269: trailing spaces

(trailing-spaces)

DotNet/Shared/Application/Health/KafkaHealthConfiguration.cs (1)

42-45: Ensure _serviceName is not null or empty in MessageBuilder.

The MessageBuilder method uses _serviceName as the message key. If _serviceName is null or empty, it could lead to unexpected behavior. Since you've added null checks in the constructor, this should be safe, but it's good to be cautious.

DotNet/QueryDispatch/Program.cs (3)

48-49: Correct inclusion of namespaces for Kafka health checks.

The added using directives HealthChecks.Kafka and LantanaGroup.Link.Shared.Application.Health are necessary for implementing Kafka health checks.


248-249: Proper initialization of Kafka health check options.

The kafkaHealthOptions are correctly instantiated using KafkaHealthCheckConfiguration, providing the necessary configuration for Kafka health checks.


251-252: Kafka health checks are successfully integrated into the health check pipeline.

Adding .AddKafka(kafkaHealthOptions) ensures that Kafka's health status is included in the application's health checks.

Copy link
Contributor

@amphillipsLGC amphillipsLGC left a comment

Choose a reason for hiding this comment

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

Looks good!

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.

2 participants