41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StellaOps.Provcache;
|
|
|
|
public sealed partial class ProvcacheService
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task<ProvcacheMetrics> GetMetricsAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var stats = await _repository.GetStatisticsAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
ProvcacheTelemetry.SetItemsCount(stats.TotalEntries);
|
|
|
|
double avgLatency, p99Latency;
|
|
lock (_metricsLock)
|
|
{
|
|
avgLatency = _latencies.Count > 0 ? _latencies.Average() : 0;
|
|
p99Latency = _latencies.Count > 0
|
|
? _latencies.OrderBy(x => x).ElementAt((int)(_latencies.Count * 0.99))
|
|
: 0;
|
|
}
|
|
|
|
return new ProvcacheMetrics
|
|
{
|
|
TotalRequests = Interlocked.Read(ref _totalRequests),
|
|
TotalHits = Interlocked.Read(ref _totalHits),
|
|
TotalMisses = Interlocked.Read(ref _totalMisses),
|
|
TotalInvalidations = Interlocked.Read(ref _totalInvalidations),
|
|
CurrentEntryCount = stats.TotalEntries,
|
|
AvgLatencyMs = avgLatency,
|
|
P99LatencyMs = p99Latency,
|
|
ValkeyCacheHealthy = true, // TODO: Add health check
|
|
PostgresRepositoryHealthy = true, // TODO: Add health check
|
|
CollectedAt = _timeProvider.GetUtcNow()
|
|
};
|
|
}
|
|
}
|