Add Kaigen API health check and configuration

- Introduced Kaigen configuration section in appsettings.Oda.json with BaseUrl.
- Added HTTP client for Kaigen API health check in Program.cs.
- Registered KaigenHealthCheck service for monitoring Kaigen API connectivity.
This commit is contained in:
2025-11-19 00:59:49 +07:00
parent 5176e41583
commit 3236edd2bb
3 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
using System.Text.Json;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Managing.Api.HealthChecks
{
public class KaigenHealthCheck : IHealthCheck
{
private readonly HttpClient _httpClient;
private readonly string _kaigenBaseUrl;
public KaigenHealthCheck(IHttpClientFactory httpClientFactory, string kaigenBaseUrl)
{
_httpClient = httpClientFactory.CreateClient("KaigenHealthCheck");
_kaigenBaseUrl = kaigenBaseUrl;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
var response = await _httpClient.GetAsync($"{_kaigenBaseUrl}/api/health", cancellationToken);
if (!response.IsSuccessStatusCode)
{
return HealthCheckResult.Degraded(
$"Kaigen API health check failed with status code: {response.StatusCode}",
data: new Dictionary<string, object>
{
["StatusCode"] = (int)response.StatusCode,
["Endpoint"] = $"{_kaigenBaseUrl}/api/health"
});
}
var content = await response.Content.ReadAsStringAsync(cancellationToken);
// Parse the JSON response to extract the detailed data
using (JsonDocument document = JsonDocument.Parse(content))
{
var root = document.RootElement;
string status = "OK";
string message = "Server is healthy";
if (root.TryGetProperty("status", out var statusElement))
{
status = statusElement.GetString();
}
if (root.TryGetProperty("message", out var messageElement))
{
message = messageElement.GetString();
}
// Extract the detailed data from the Kaigen response
var data = new Dictionary<string, object>();
// Parse timestamp if available
if (root.TryGetProperty("timestamp", out var timestampElement))
{
data["timestamp"] = timestampElement.GetString();
}
data["message"] = message;
// Determine overall health result based on status
if (status.ToUpper() == "OK")
{
return HealthCheckResult.Healthy(
"Kaigen API is healthy",
data: data);
}
else
{
return HealthCheckResult.Unhealthy(
$"Kaigen API returned status: {status}",
data: data);
}
}
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy(
"Failed to connect to Kaigen API",
ex,
data: new Dictionary<string, object>
{
["Endpoint"] = $"{_kaigenBaseUrl}/api/health",
["ErrorMessage"] = ex.Message,
["ErrorType"] = ex.GetType().Name
});
}
}
}
}