grpc-dotnet icon indicating copy to clipboard operation
grpc-dotnet copied to clipboard

gRPC HealthChecks returning '''Unknown''' Rather than Serving

Open ExtremeSwat opened this issue 1 year ago • 2 comments

What version of gRPC and what language are you using?

2.55.0 / 2.56.0-pre2 --> C#

What operating system (Linux, Windows,...) and version?

Windows 11

What runtime / compiler are you using (e.g. .NET Core SDK version dotnet --info)

net7.0 (7.0.307)

What did you do?

I was able to replicate this issue with the default grpc template from VS. Basically dialing into the grpc.health.v1.services --> Check/Watch will return Unkwnown as a status.

This is my Program.cs

using GrpcService1.HealthCheck;
using GrpcService1.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGrpc();
builder.Services.AddGrpcHealthChecks(o =>
{
    o.Services.Map("", r => r.Tags.Contains("public"));
}).AddCheck<PingHealthCheck>("ping");

var app = builder.Build();

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGrpcHealthChecksService();

app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.Run();

And this is my healthCheck:

 public class PingHealthCheck : IHealthCheck
    {
        public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
            CancellationToken cancellationToken = new())
        {

            using var channel = GrpcChannel.ForAddress("http://localhost:5127/", new GrpcChannelOptions
            {
             
            });

            var client = new Health.HealthClient(channel);
            var response = await client.CheckAsync(new HealthCheckRequest());
            var status = response.Status;

            return status == HealthCheckResponse.Types.ServingStatus.Serving
                ? HealthCheckResult.Healthy()
                : HealthCheckResult.Unhealthy();
        }
    }

I've been following this doc: https://learn.microsoft.com/en-us/aspnet/core/grpc/health-checks?view=aspnetcore-7.0

Health results determine what the gRPC service reports:

1. Unknown is reported when there are no health results.
2. NotServing is reported when there are any health results of [HealthStatus.Unhealthy](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.diagnostics.healthchecks.healthstatus#microsoft-extensions-diagnostics-healthchecks-healthstatus-unhealthy).
3. Otherwise, Serving is reported.

This is a bit weird, because I don't get why there aren't any health results, do I need to kickstart anything? Like if I comment out the MapGrpcHealthChecksService the service will rightfully fail, telling me that it's not implemented.

I have attached my sample.

What did you expect to see?

{
    "status": "SERVING"
}

What did you see instead?

{
    "status": "UNKNOWN"
}

GrpcService1.zip

ExtremeSwat avatar Aug 22 '23 07:08 ExtremeSwat