2024-10-14 14:56:40 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
2024-08-15 17:23:56 +02:00
|
|
|
using System.Diagnostics;
|
2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
2024-08-15 17:23:56 +02:00
|
|
|
using Catalogger.Backend.Database;
|
2024-10-28 02:14:41 +01:00
|
|
|
using Dapper;
|
2024-08-15 17:23:56 +02:00
|
|
|
using Humanizer;
|
2024-08-20 20:19:24 +02:00
|
|
|
using Prometheus;
|
2024-08-15 17:23:56 +02:00
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Services;
|
|
|
|
|
|
|
|
|
|
public class MetricsCollectionService(
|
|
|
|
|
ILogger logger,
|
2024-08-19 16:12:28 +02:00
|
|
|
GuildCache guildCache,
|
|
|
|
|
ChannelCache channelCache,
|
2024-11-05 15:32:53 +01:00
|
|
|
RoleCache roleCache,
|
2024-08-19 16:12:28 +02:00
|
|
|
UserCache userCache,
|
2024-10-14 17:09:12 +02:00
|
|
|
EmojiCache emojiCache,
|
2024-10-09 17:35:11 +02:00
|
|
|
IServiceProvider services
|
|
|
|
|
)
|
2024-08-15 17:23:56 +02:00
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<MetricsCollectionService>();
|
|
|
|
|
|
2024-08-20 20:19:24 +02:00
|
|
|
public async Task CollectMetricsAsync(CancellationToken ct = default)
|
2024-08-15 17:23:56 +02:00
|
|
|
{
|
2024-08-20 20:19:24 +02:00
|
|
|
var timer = CataloggerMetrics.MetricsCollectionTime.NewTimer();
|
2024-08-15 17:23:56 +02:00
|
|
|
|
|
|
|
|
await using var scope = services.CreateAsyncScope();
|
2024-10-28 02:14:41 +01:00
|
|
|
await using var conn = scope.ServiceProvider.GetRequiredService<DatabaseConnection>();
|
2024-08-15 17:23:56 +02:00
|
|
|
|
2024-10-28 02:14:41 +01:00
|
|
|
var messageCount = await conn.ExecuteScalarAsync<int>("select count(id) from messages");
|
2024-08-15 17:23:56 +02:00
|
|
|
|
2024-11-27 16:17:11 +01:00
|
|
|
CataloggerMetrics.DatabaseConnections.Set(DatabasePool.OpenConnections);
|
2024-08-20 20:19:24 +02:00
|
|
|
CataloggerMetrics.GuildsCached.Set(guildCache.Size);
|
|
|
|
|
CataloggerMetrics.ChannelsCached.Set(channelCache.Size);
|
2024-11-05 15:32:53 +01:00
|
|
|
CataloggerMetrics.RolesCached.Set(roleCache.Size);
|
2024-08-20 20:19:24 +02:00
|
|
|
CataloggerMetrics.UsersCached.Set(userCache.Size);
|
2024-10-14 17:09:12 +02:00
|
|
|
CataloggerMetrics.EmojiCached.Set(emojiCache.Size);
|
2024-08-20 20:19:24 +02:00
|
|
|
CataloggerMetrics.MessagesStored.Set(messageCount);
|
|
|
|
|
CataloggerMetrics.MessageRateMinute = messageCount - CataloggerMetrics.MessageRateMinute;
|
2024-08-15 17:23:56 +02:00
|
|
|
|
|
|
|
|
var process = Process.GetCurrentProcess();
|
2024-08-20 20:19:24 +02:00
|
|
|
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);
|
2024-08-15 17:23:56 +02:00
|
|
|
|
2024-08-20 20:19:24 +02:00
|
|
|
_logger.Information("Collected metrics in {Duration}", timer.ObserveDuration());
|
2024-08-15 17:23:56 +02:00
|
|
|
}
|
2024-08-20 20:19:24 +02:00
|
|
|
}
|
2024-08-15 17:23:56 +02:00
|
|
|
|
2024-10-09 17:35:11 +02:00
|
|
|
public class BackgroundMetricsCollectionService(
|
|
|
|
|
ILogger logger,
|
|
|
|
|
MetricsCollectionService innerService
|
|
|
|
|
) : BackgroundService
|
2024-08-20 20:19:24 +02:00
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<BackgroundMetricsCollectionService>();
|
2024-08-21 17:31:39 +02:00
|
|
|
|
2024-08-20 20:19:24 +02:00
|
|
|
protected override async Task ExecuteAsync(CancellationToken ct)
|
2024-08-15 17:23:56 +02:00
|
|
|
{
|
2024-08-20 20:19:24 +02:00
|
|
|
_logger.Information("Metrics are disabled, periodically collecting metrics manually");
|
2024-08-21 17:31:39 +02:00
|
|
|
|
2024-08-15 17:23:56 +02:00
|
|
|
using var timer = new PeriodicTimer(1.Minutes());
|
2024-08-20 20:19:24 +02:00
|
|
|
while (await timer.WaitForNextTickAsync(ct))
|
2024-08-15 17:23:56 +02:00
|
|
|
{
|
2024-08-20 20:19:24 +02:00
|
|
|
_logger.Debug("Collecting metrics");
|
|
|
|
|
await innerService.CollectMetricsAsync(ct);
|
2024-08-15 17:23:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-09 17:35:11 +02:00
|
|
|
}
|