Catalogger.NET/Catalogger.Backend/Services/MetricsCollectionService.cs

82 lines
3.1 KiB
C#
Raw Normal View History

// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.Diagnostics;
using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Humanizer;
using Microsoft.EntityFrameworkCore;
using Prometheus;
namespace Catalogger.Backend.Services;
public class MetricsCollectionService(
ILogger logger,
GuildCache guildCache,
ChannelCache channelCache,
UserCache userCache,
2024-10-14 17:09:12 +02:00
EmojiCache emojiCache,
2024-10-09 17:35:11 +02:00
IServiceProvider services
)
{
private readonly ILogger _logger = logger.ForContext<MetricsCollectionService>();
public async Task CollectMetricsAsync(CancellationToken ct = default)
{
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(ct);
CataloggerMetrics.GuildsCached.Set(guildCache.Size);
CataloggerMetrics.ChannelsCached.Set(channelCache.Size);
CataloggerMetrics.UsersCached.Set(userCache.Size);
2024-10-14 17:09:12 +02:00
CataloggerMetrics.EmojiCached.Set(emojiCache.Size);
CataloggerMetrics.MessagesStored.Set(messageCount);
CataloggerMetrics.MessageRateMinute = messageCount - CataloggerMetrics.MessageRateMinute;
var process = Process.GetCurrentProcess();
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);
_logger.Information("Collected metrics in {Duration}", timer.ObserveDuration());
}
}
2024-10-09 17:35:11 +02:00
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(ct))
{
_logger.Debug("Collecting metrics");
await innerService.CollectMetricsAsync(ct);
}
}
2024-10-09 17:35:11 +02:00
}