feat: proper health-check for github#10
Conversation
|
This thing will return only Healthy/Unhealthy/Degraded string, no details. It cannot return detailed statuses because that will require either custom serialization code or turning NAOT off. Need to add health-check for discourse. And for database too. Then this status being Unhealthy would be quite problematic to decypher ;( |
|
|
||
| app.MapGet("/", () => Results.Ok("Nik is a cat!")); | ||
|
|
||
| app.MapHealthChecks("/health"); |
There was a problem hiding this comment.
Can you gate this behind some form of auth? Maybe a basic-auth check that is configured via appsettings? We wouldn't want people spamming this endpoint and getting us ratelimited that way.
There was a problem hiding this comment.
Smort! true.
actually i was not expecting to have this in open field
hmm. i need to do cache for gh status, so we won't re-request it too often anyways!
There was a problem hiding this comment.
Cache added, i propose leave it as is - having basic auth just for that endpoint seems off.
| var client = httpClientFactory.CreateClient(nameof(IGitHubApiClient)); | ||
|
|
||
| // Make a simple, low-impact request to verify the key | ||
| var response = await client.GetAsync("https://api.github.com/user", cancellationToken); | ||
|
|
||
| if (response.IsSuccessStatusCode) | ||
| { | ||
| return HealthCheckResult.Healthy("GitHub API key is valid."); | ||
| } | ||
|
|
||
| if (response.StatusCode is System.Net.HttpStatusCode.Unauthorized or System.Net.HttpStatusCode.Forbidden) | ||
| { | ||
| return HealthCheckResult.Unhealthy("GitHub API key is invalid or lacks necessary permissions."); | ||
| } | ||
|
|
||
| return HealthCheckResult.Degraded($"GitHub API returned an unexpected status code: {response.StatusCode}"); | ||
| } | ||
| catch (HttpRequestException ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"Failed to connect to GitHub API: {ex.Message}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy($"An error occurred during GitHub API key health check: {ex.Message}"); | ||
| } |
There was a problem hiding this comment.
Instead of doing ex.Message just pass ex itself so it also includes the full stacktrace. (Considering this endpoint is supposed to be internal)
There was a problem hiding this comment.
I will do that, but serializer that we are using (which is the only available by default w/ NAOT) is not going to show anything anyways, not even that text - just healthy/unhealthy. So probably fine by now. Later tho i would better like that thing blocking requests from the outside on the level of reverse proxy.
No description provided.