2019-07-18 15:13:42 +00:00
|
|
|
using System.Diagnostics;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
using App.Metrics;
|
2020-06-13 17:15:29 +00:00
|
|
|
|
2021-01-31 13:59:45 +00:00
|
|
|
using Myriad.Cache;
|
2020-04-17 21:10:01 +00:00
|
|
|
|
2022-03-15 03:33:22 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
using NodaTime.Extensions;
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2019-07-16 21:34:22 +00:00
|
|
|
using PluralKit.Core;
|
2020-02-12 14:16:19 +00:00
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
using Serilog;
|
2019-07-16 21:34:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
using Stopwatch = System.Diagnostics.Stopwatch;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class PeriodicStatCollector
|
2019-07-16 21:34:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly IDiscordCache _cache;
|
2019-07-16 21:34:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly DbConnectionCountHolder _countHolder;
|
|
|
|
private readonly CpuStatService _cpu;
|
2022-03-15 03:33:22 +00:00
|
|
|
private readonly BotConfig _botConfig;
|
|
|
|
private readonly CoreConfig _config;
|
2019-07-16 21:34:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IMetrics _metrics;
|
2019-07-21 02:15:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ModelRepository _repo;
|
2022-03-15 03:33:22 +00:00
|
|
|
private readonly RedisService _redis;
|
2019-08-11 20:56:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly WebhookCacheService _webhookCache;
|
2019-07-18 15:13:42 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache,
|
|
|
|
DbConnectionCountHolder countHolder, CpuStatService cpu, ModelRepository repo,
|
2022-03-15 03:33:22 +00:00
|
|
|
BotConfig botConfig, CoreConfig config, RedisService redis, IDiscordCache cache)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
_metrics = metrics;
|
|
|
|
_webhookCache = webhookCache;
|
|
|
|
_countHolder = countHolder;
|
|
|
|
_cpu = cpu;
|
|
|
|
_repo = repo;
|
|
|
|
_cache = cache;
|
2022-03-15 03:33:22 +00:00
|
|
|
_botConfig = botConfig;
|
|
|
|
_config = config;
|
|
|
|
_redis = redis;
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger = logger.ForContext<PeriodicStatCollector>();
|
|
|
|
}
|
2019-07-16 21:34:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task CollectStats()
|
|
|
|
{
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
|
|
// Aggregate guild/channel stats
|
|
|
|
var guildCount = 0;
|
|
|
|
var channelCount = 0;
|
|
|
|
|
|
|
|
// No LINQ today, sorry
|
|
|
|
await foreach (var guild in _cache.GetAllGuilds())
|
2019-07-16 21:34:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
guildCount++;
|
|
|
|
foreach (var channel in await _cache.GetGuildChannels(guild.Id))
|
|
|
|
if (DiscordUtils.IsValidGuildChannel(channel))
|
|
|
|
channelCount++;
|
2019-07-16 21:34:22 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-03-15 03:33:22 +00:00
|
|
|
if (_config.UseRedisMetrics)
|
|
|
|
{
|
|
|
|
var db = _redis.Connection.GetDatabase();
|
|
|
|
await db.HashSetAsync("pluralkit:cluster_stats", new StackExchange.Redis.HashEntry[] {
|
|
|
|
new(_botConfig.Cluster.NodeIndex, JsonConvert.SerializeObject(new ClusterMetricInfo
|
|
|
|
{
|
|
|
|
GuildCount = guildCount,
|
|
|
|
ChannelCount = channelCount,
|
|
|
|
DatabaseConnectionCount = _countHolder.ConnectionCount,
|
|
|
|
WebhookCacheSize = _webhookCache.CacheSize,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
// Process info
|
|
|
|
var process = Process.GetCurrentProcess();
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPhysicalMemory, process.WorkingSet64);
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessVirtualMemory, process.VirtualMemorySize64);
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessPrivateMemory, process.PrivateMemorySize64);
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessThreads, process.Threads.Count);
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.ProcessHandles, process.HandleCount);
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.CpuUsage, await _cpu.EstimateCpuUsage());
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
_logger.Debug("Updated metrics in {Time}", stopwatch.ElapsedDuration());
|
2019-07-16 21:34:22 +00:00
|
|
|
}
|
|
|
|
}
|