Fix: Kafka replication factor hardcoded to 1 and No health check endpoints
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using Elastic.Clients.Elasticsearch;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
public sealed class ElasticsearchHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly ElasticsearchClient _client;
|
||||
|
||||
public ElasticsearchHealthCheck(ElasticsearchClient client) => _client = client;
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var response = await _client.PingAsync(cancellationToken);
|
||||
|
||||
if (response.IsValidResponse)
|
||||
return HealthCheckResult.Healthy();
|
||||
|
||||
return HealthCheckResult.Unhealthy("Elasticsearch ping failed.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
public static class HealthCheckResponseWriter
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
public static async Task WriteAsync(HttpContext context, HealthReport report)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
|
||||
var response = new
|
||||
{
|
||||
status = report.Status.ToString(),
|
||||
totalDurationMs = report.TotalDuration.TotalMilliseconds,
|
||||
checks = report.Entries.Select(e => new
|
||||
{
|
||||
name = e.Key,
|
||||
status = e.Value.Status.ToString(),
|
||||
durationMs = e.Value.Duration.TotalMilliseconds,
|
||||
description = e.Value.Description,
|
||||
data = e.Value.Data.Count > 0 ? e.Value.Data : null,
|
||||
exception = e.Value.Exception?.Message
|
||||
})
|
||||
};
|
||||
|
||||
await context.Response.WriteAsync(JsonSerializer.Serialize(response, JsonOptions));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Confluent.Kafka;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
public sealed class KafkaHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly KafkaOptions _options;
|
||||
|
||||
public KafkaHealthCheck(IOptions<KafkaOptions> options) => _options = options.Value;
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var admin = new AdminClientBuilder(new AdminClientConfig
|
||||
{
|
||||
BootstrapServers = _options.BootstrapServers
|
||||
}).Build();
|
||||
|
||||
var metadata = await Task.Run(
|
||||
() => admin.GetMetadata(TimeSpan.FromSeconds(5)), cancellationToken);
|
||||
|
||||
var data = new Dictionary<string, object> { ["brokers"] = metadata.Brokers.Count };
|
||||
return HealthCheckResult.Healthy(data: data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
public sealed class RabbitMqHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly RabbitMqOptions _options;
|
||||
|
||||
public RabbitMqHealthCheck(IOptions<RabbitMqOptions> options) => _options = options.Value;
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
HostName = _options.Host,
|
||||
Port = _options.Port,
|
||||
UserName = _options.Username,
|
||||
Password = _options.Password
|
||||
};
|
||||
|
||||
using var connection = await Task.Run(() => factory.CreateConnection(), cancellationToken);
|
||||
var data = new Dictionary<string, object> { ["endpoint"] = connection.Endpoint.ToString() };
|
||||
return HealthCheckResult.Healthy(data: data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using StackExchange.Redis;
|
||||
|
||||
public sealed class RedisHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly IConnectionMultiplexer _redis;
|
||||
|
||||
public RedisHealthCheck(IConnectionMultiplexer redis) => _redis = redis;
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
var latency = await db.PingAsync();
|
||||
var data = new Dictionary<string, object> { ["ping_ms"] = latency.TotalMilliseconds };
|
||||
return HealthCheckResult.Healthy(data: data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user