2019-07-16 21:34:22 +00:00
|
|
|
using System.Collections.Generic;
|
2019-07-18 15:13:42 +00:00
|
|
|
using System.Diagnostics;
|
2019-07-16 21:34:22 +00:00
|
|
|
using System.Linq;
|
2019-07-20 22:01:02 +00:00
|
|
|
using System.Threading;
|
2019-07-16 21:34:22 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using App.Metrics;
|
|
|
|
using Discord;
|
|
|
|
using Discord.WebSocket;
|
2019-07-18 15:13:42 +00:00
|
|
|
using NodaTime.Extensions;
|
2019-07-16 21:34:22 +00:00
|
|
|
using PluralKit.Core;
|
2019-07-18 15:13:42 +00:00
|
|
|
using Serilog;
|
2019-07-16 21:34:22 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class PeriodicStatCollector
|
|
|
|
{
|
|
|
|
private DiscordShardedClient _client;
|
|
|
|
private IMetrics _metrics;
|
|
|
|
|
|
|
|
private SystemStore _systems;
|
|
|
|
private MemberStore _members;
|
|
|
|
private SwitchStore _switches;
|
|
|
|
private MessageStore _messages;
|
|
|
|
|
2019-07-21 02:15:47 +00:00
|
|
|
private WebhookCacheService _webhookCache;
|
|
|
|
|
2019-08-11 20:56:20 +00:00
|
|
|
private DbConnectionCountHolder _countHolder;
|
|
|
|
|
2019-07-18 15:13:42 +00:00
|
|
|
private ILogger _logger;
|
|
|
|
|
2019-08-11 20:56:20 +00:00
|
|
|
public PeriodicStatCollector(IDiscordClient client, IMetrics metrics, SystemStore systems, MemberStore members, SwitchStore switches, MessageStore messages, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder)
|
2019-07-16 21:34:22 +00:00
|
|
|
{
|
|
|
|
_client = (DiscordShardedClient) client;
|
|
|
|
_metrics = metrics;
|
|
|
|
_systems = systems;
|
|
|
|
_members = members;
|
|
|
|
_switches = switches;
|
|
|
|
_messages = messages;
|
2019-07-21 02:15:47 +00:00
|
|
|
_webhookCache = webhookCache;
|
2019-08-11 20:56:20 +00:00
|
|
|
_countHolder = countHolder;
|
2019-07-18 15:13:42 +00:00
|
|
|
_logger = logger.ForContext<PeriodicStatCollector>();
|
2019-07-16 21:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task CollectStats()
|
|
|
|
{
|
2019-07-18 15:13:42 +00:00
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
stopwatch.Start();
|
|
|
|
|
2019-07-16 21:34:22 +00:00
|
|
|
// Aggregate guild/channel stats
|
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.Guilds, _client.Guilds.Count);
|
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, _client.Guilds.Sum(g => g.TextChannels.Count));
|
2019-08-11 22:35:30 +00:00
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.ShardsConnected, _client.Shards.Count(shard => shard.ConnectionState == ConnectionState.Connected));
|
2019-07-16 21:34:22 +00:00
|
|
|
|
|
|
|
// Aggregate member stats
|
|
|
|
var usersKnown = new HashSet<ulong>();
|
|
|
|
var usersOnline = new HashSet<ulong>();
|
|
|
|
foreach (var guild in _client.Guilds)
|
|
|
|
foreach (var user in guild.Users)
|
|
|
|
{
|
|
|
|
usersKnown.Add(user.Id);
|
|
|
|
if (user.Status == UserStatus.Online) usersOnline.Add(user.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.MembersTotal, usersKnown.Count);
|
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.MembersOnline, usersOnline.Count);
|
|
|
|
|
|
|
|
// Aggregate DB stats
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, await _systems.Count());
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, await _members.Count());
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, await _switches.Count());
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, await _messages.Count());
|
2019-07-20 22:01:02 +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 EstimateCpuUsage());
|
2019-07-21 02:15:47 +00:00
|
|
|
|
2019-08-11 20:56:20 +00:00
|
|
|
// Database info
|
|
|
|
_metrics.Measure.Gauge.SetValue(CoreMetrics.DatabaseConnections, _countHolder.ConnectionCount);
|
|
|
|
|
2019-07-21 02:15:47 +00:00
|
|
|
// Other shiz
|
|
|
|
_metrics.Measure.Gauge.SetValue(BotMetrics.WebhookCacheSize, _webhookCache.CacheSize);
|
2019-07-18 15:13:42 +00:00
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
_logger.Information("Updated metrics in {Time}", stopwatch.ElapsedDuration());
|
2019-07-16 21:34:22 +00:00
|
|
|
}
|
2019-07-20 22:01:02 +00:00
|
|
|
|
|
|
|
private async Task<double> EstimateCpuUsage()
|
|
|
|
{
|
|
|
|
// We get the current processor time, wait 5 seconds, then compare
|
|
|
|
// https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
|
|
|
|
|
|
|
|
_logger.Information("Estimating CPU usage...");
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
|
|
|
stopwatch.Start();
|
|
|
|
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
|
|
|
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
|
|
|
|
|
|
|
|
var cpuTimePassed = cpuTimeAfter - cpuTimeBefore;
|
|
|
|
var timePassed = stopwatch.Elapsed;
|
|
|
|
|
|
|
|
var percent = cpuTimePassed / timePassed;
|
|
|
|
_logger.Information("CPU usage measured as {Percent:P}", percent);
|
|
|
|
return percent;
|
|
|
|
}
|
2019-07-16 21:34:22 +00:00
|
|
|
}
|
|
|
|
}
|