feat: replace App.Metrics with prometheus-net

This commit is contained in:
sam 2024-08-20 20:19:24 +02:00
parent df8af75dd4
commit be01fb1d53
8 changed files with 113 additions and 137 deletions

View file

@ -1,10 +1,9 @@
using System.Diagnostics;
using App.Metrics;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Humanizer;
using Microsoft.EntityFrameworkCore;
using NodaTime.Extensions;
using Prometheus;
namespace Catalogger.Backend.Services;
@ -13,49 +12,49 @@ public class MetricsCollectionService(
GuildCache guildCache,
ChannelCache channelCache,
UserCache userCache,
IMetrics metrics,
IServiceProvider services) : BackgroundService
IServiceProvider services)
{
private readonly ILogger _logger = logger.ForContext<MetricsCollectionService>();
private async Task CollectMetricsAsync()
public async Task CollectMetricsAsync(CancellationToken ct = default)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var timer = CataloggerMetrics.MetricsCollectionTime.NewTimer();
await using var scope = services.CreateAsyncScope();
await using var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
var messageCount = await db.Messages.CountAsync();
var messageCount = await db.Messages.CountAsync(ct);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.GuildsCached, guildCache.Size);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ChannelsCached, channelCache.Size);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.UsersCached, userCache.Size);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.MessagesStored, messageCount);
CataloggerMetrics.GuildsCached.Set(guildCache.Size);
CataloggerMetrics.ChannelsCached.Set(channelCache.Size);
CataloggerMetrics.UsersCached.Set(userCache.Size);
CataloggerMetrics.MessagesStored.Set(messageCount);
CataloggerMetrics.MessageRateMinute = messageCount - CataloggerMetrics.MessageRateMinute;
var process = Process.GetCurrentProcess();
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ProcessPhysicalMemory, process.WorkingSet64);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ProcessVirtualMemory, process.VirtualMemorySize64);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ProcessPrivateMemory, process.PrivateMemorySize64);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ProcessThreads, process.Threads.Count);
metrics.Measure.Gauge.SetValue(CataloggerMetrics.ProcessHandles, process.HandleCount);
CataloggerMetrics.ProcessPhysicalMemory.Set(process.WorkingSet64);
CataloggerMetrics.ProcessVirtualMemory.Set(process.VirtualMemorySize64);
CataloggerMetrics.ProcessPrivateMemory.Set(process.PrivateMemorySize64);
CataloggerMetrics.ProcessThreads.Set(process.Threads.Count);
CataloggerMetrics.ProcessHandles.Set(process.HandleCount);
stopwatch.Stop();
metrics.Measure.Timer.Time(CataloggerMetrics.MetricsCollectionTime, stopwatch.ElapsedMilliseconds);
_logger.Information("Collected metrics in {Duration}", stopwatch.ElapsedDuration());
await Task.WhenAll(((IMetricsRoot)metrics).ReportRunner.RunAllAsync());
_logger.Information("Collected metrics in {Duration}", timer.ObserveDuration());
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
public class BackgroundMetricsCollectionService(ILogger logger, MetricsCollectionService innerService) : BackgroundService
{
private readonly ILogger _logger = logger.ForContext<BackgroundMetricsCollectionService>();
protected override async Task ExecuteAsync(CancellationToken ct)
{
_logger.Information("Metrics are disabled, periodically collecting metrics manually");
using var timer = new PeriodicTimer(1.Minutes());
while (await timer.WaitForNextTickAsync(stoppingToken))
while (await timer.WaitForNextTickAsync(ct))
{
_logger.Debug("Collecting periodic metrics");
await CollectMetricsAsync();
_logger.Debug("Reported metrics to backend");
_logger.Debug("Collecting metrics");
await innerService.CollectMetricsAsync(ct);
}
}
}