-
Notifications
You must be signed in to change notification settings - Fork 7
feat: proper health-check for github #10
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using SS14.Labeller.GitHubApi; | ||
|
|
||
| namespace SS14.Labeller.HealthChecks; | ||
|
|
||
| public class GitHubApiKeyHealthCheck(IHttpClientFactory httpClientFactory, IMemoryCache cache) : IHealthCheck | ||
| { | ||
| public async Task<HealthCheckResult> CheckHealthAsync( | ||
| HealthCheckContext context, | ||
| CancellationToken cancellationToken = default | ||
| ) | ||
| { | ||
| var result = await cache.GetOrCreateAsync( | ||
| "GHKeyHealthCheck", | ||
| async entry => | ||
| { | ||
| entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5); | ||
| return await CheckHealthAsyncInternal(cancellationToken); | ||
| } | ||
| ); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private async Task<HealthCheckResult> CheckHealthAsyncInternal(CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| 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", exception: ex); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return HealthCheckResult.Unhealthy("An unexpected error occurred during GitHub API key health check", exception: ex); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,5 @@ | ||
| using Dapper; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Options; | ||
| using SS14.Labeller.Configuration; | ||
| using SS14.Labeller.Endpoints; | ||
| using SS14.Labeller.Handlers; | ||
| using SS14.Labeller.Middlewares; | ||
| using SS14.Labeller.Models; | ||
|
|
||
| [module:DapperAot] | ||
|
|
||
|
|
@@ -31,6 +25,9 @@ public static void Main(string[] args) | |
| app.UseHttpLogging(); | ||
|
|
||
| app.MapGet("/", () => Results.Ok("Nik is a cat!")); | ||
|
|
||
| app.MapHealthChecks("/health"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smort! true.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cache added, i propose leave it as is - having basic auth just for that endpoint seems off. |
||
|
|
||
| app.MapGithubWebhook(); | ||
|
|
||
| app.Run(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing
ex.Messagejust passexitself so it also includes the full stacktrace. (Considering this endpoint is supposed to be internal)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.