Update shard status tracker for new library
This commit is contained in:
parent
d0f1aeb548
commit
48833639e2
@ -118,9 +118,6 @@ namespace PluralKit.Bot
|
||||
|
||||
public Task Init()
|
||||
{
|
||||
// DiscordShardedClient SocketErrored/Ready events also fire whenever an individual shard's respective events fire
|
||||
_client.SocketErrored += ShardDisconnected;
|
||||
_client.Ready += ShardReady;
|
||||
_client.DebugLogger.LogMessageReceived += FrameworkLog;
|
||||
|
||||
_client.MessageCreated += args => HandleEvent(eh => eh.HandleMessage(args));
|
||||
@ -137,12 +134,6 @@ namespace PluralKit.Bot
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task ShardDisconnected(SocketErrorEventArgs e)
|
||||
{
|
||||
_logger.Warning(e.Exception, $"Shard #{e.Client.ShardId} disconnected");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void FrameworkLog(object sender, DebugLogMessageEventArgs args)
|
||||
{
|
||||
// Bridge D#+ logging to Serilog
|
||||
@ -185,12 +176,6 @@ namespace PluralKit.Bot
|
||||
}
|
||||
}
|
||||
|
||||
private Task ShardReady(ReadyEventArgs e)
|
||||
{
|
||||
_logger.Information("Shard {Shard} connected to {ChannelCount} channels in {GuildCount} guilds", e.Client.ShardId, e.Client.Guilds.Sum(g => g.Value.Channels.Count), e.Client.Guilds.Count);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task HandleEvent(Func<PKEventHandler, Task> handler)
|
||||
{
|
||||
// Inner function so we can await the handler without stalling the entire pipeline
|
||||
|
@ -52,20 +52,23 @@ namespace PluralKit.Bot {
|
||||
|
||||
public async Task Stats(Context ctx)
|
||||
{
|
||||
var timeBefore = SystemClock.Instance.GetCurrentInstant();
|
||||
var msg = await ctx.Reply($"...");
|
||||
var timeAfter = SystemClock.Instance.GetCurrentInstant();
|
||||
var apiLatency = timeAfter - timeBefore;
|
||||
|
||||
var messagesReceived = _metrics.Snapshot.GetForContext("Bot").Meters.First(m => m.MultidimensionalName == BotMetrics.MessagesReceived.Name).Value;
|
||||
var messagesProxied = _metrics.Snapshot.GetForContext("Bot").Meters.First(m => m.MultidimensionalName == BotMetrics.MessagesProxied.Name).Value;
|
||||
var commandsRun = _metrics.Snapshot.GetForContext("Bot").Meters.First(m => m.MultidimensionalName == BotMetrics.CommandsRun.Name).Value;
|
||||
var messagesReceived = _metrics.Snapshot.GetForContext("Bot").Meters.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesReceived.Name)?.Value;
|
||||
var messagesProxied = _metrics.Snapshot.GetForContext("Bot").Meters.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesProxied.Name)?.Value;
|
||||
var commandsRun = _metrics.Snapshot.GetForContext("Bot").Meters.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.CommandsRun.Name)?.Value;
|
||||
|
||||
var totalSystems = _metrics.Snapshot.GetForContext("Application").Gauges.First(m => m.MultidimensionalName == CoreMetrics.SystemCount.Name).Value;
|
||||
var totalMembers = _metrics.Snapshot.GetForContext("Application").Gauges.First(m => m.MultidimensionalName == CoreMetrics.MemberCount.Name).Value;
|
||||
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.First(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name).Value;
|
||||
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.First(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name).Value;
|
||||
var totalSystems = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SystemCount.Name)?.Value ?? 0;
|
||||
var totalMembers = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MemberCount.Name)?.Value ?? 0;
|
||||
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0;
|
||||
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0;
|
||||
|
||||
var shardId = ctx.Shard.ShardId;
|
||||
var shardTotal = ctx.Client.ShardClients.Count;
|
||||
var shardUpTotal = ctx.Client.ShardClients.Where(x => x.Value.IsConnected()).Count();
|
||||
var shardUpTotal = _shards.Shards.Where(x => x.Connected).Count();
|
||||
var shardInfo = _shards.GetShardInfo(ctx.Shard);
|
||||
|
||||
var process = Process.GetCurrentProcess();
|
||||
@ -73,15 +76,17 @@ namespace PluralKit.Bot {
|
||||
|
||||
var shardUptime = SystemClock.Instance.GetCurrentInstant() - shardInfo.LastConnectionTime;
|
||||
|
||||
var embed = new DiscordEmbedBuilder()
|
||||
.AddField("Messages processed", $"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true)
|
||||
.AddField("Messages proxied", $"{messagesProxied.OneMinuteRate * 60:F1}/m ({messagesProxied.FifteenMinuteRate * 60:F1}/m over 15m)", true)
|
||||
.AddField("Commands executed", $"{commandsRun.OneMinuteRate * 60:F1}/m ({commandsRun.FifteenMinuteRate * 60:F1}/m over 15m)", true)
|
||||
var embed = new DiscordEmbedBuilder();
|
||||
if (messagesReceived != null) embed.AddField("Messages processed",$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true);
|
||||
if (messagesProxied != null) embed.AddField("Messages proxied", $"{messagesProxied.OneMinuteRate * 60:F1}/m ({messagesProxied.FifteenMinuteRate * 60:F1}/m over 15m)", true);
|
||||
if (commandsRun != null) embed.AddField("Commands executed", $"{commandsRun.OneMinuteRate * 60:F1}/m ({commandsRun.FifteenMinuteRate * 60:F1}/m over 15m)", true);
|
||||
|
||||
embed
|
||||
.AddField("Current shard", $"Shard #{shardId} (of {shardTotal} total, {shardUpTotal} are up)", true)
|
||||
.AddField("Shard uptime", $"{DateTimeFormats.DurationFormat.Format(shardUptime)} ({shardInfo.DisconnectionCount} disconnections)", true)
|
||||
.AddField("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true)
|
||||
.AddField("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true)
|
||||
.AddField("Latency", $"API: {(msg.Timestamp - ctx.Message.Timestamp).TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency} ms", true)
|
||||
.AddField("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true)
|
||||
.AddField("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalSwitches:N0} switches, {totalMessages:N0} messages");
|
||||
await msg.ModifyAsync("", embed.Build());
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.EventArgs;
|
||||
|
||||
using NodaTime;
|
||||
using NodaTime.Extensions;
|
||||
|
||||
using Serilog;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
@ -13,47 +16,79 @@ namespace PluralKit.Bot
|
||||
public class ShardInfo
|
||||
{
|
||||
public Instant LastConnectionTime;
|
||||
public Instant LastHeartbeatTime;
|
||||
public int DisconnectionCount;
|
||||
public int ShardLatency;
|
||||
public Duration ShardLatency;
|
||||
public bool Connected;
|
||||
}
|
||||
|
||||
private ILogger _logger;
|
||||
private Dictionary<int, ShardInfo> _shardInfo = new Dictionary<int, ShardInfo>();
|
||||
|
||||
public ShardInfoService(ILogger logger)
|
||||
{
|
||||
_logger = logger.ForContext<ShardInfoService>();
|
||||
}
|
||||
|
||||
public void Init(DiscordShardedClient client)
|
||||
{
|
||||
foreach (var i in client.ShardClients.Keys)
|
||||
foreach (var i in client.ShardClients.Keys)
|
||||
{
|
||||
_shardInfo[i] = new ShardInfo();
|
||||
|
||||
// TODO
|
||||
// client.ShardConnected += ShardConnected;
|
||||
// client.ShardDisconnected += ShardDisconnected;
|
||||
// client.ShardReady += ShardReady;
|
||||
// client.ShardLatencyUpdated += ShardLatencyUpdated;
|
||||
|
||||
var shard = client.ShardClients[i];
|
||||
shard.Heartbeated += Heartbeated;
|
||||
shard.SocketClosed += SocketClosed;
|
||||
shard.SocketOpened += () => SocketOpened(shard);
|
||||
shard.Ready += Ready;
|
||||
shard.Resumed += Resumed;
|
||||
}
|
||||
}
|
||||
|
||||
private Task Resumed(ReadyEventArgs e)
|
||||
{
|
||||
_logger.Information("Shard #{Shard} resumed connection", e.Client.ShardId);
|
||||
_shardInfo[e.Client.ShardId].LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
_shardInfo[e.Client.ShardId].Connected = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task Ready(ReadyEventArgs e)
|
||||
{
|
||||
_logger.Information("Shard #{Shard} sent Ready event", e.Client.ShardId);
|
||||
_shardInfo[e.Client.ShardId].LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
_shardInfo[e.Client.ShardId].Connected = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task SocketOpened(DiscordClient shard)
|
||||
{
|
||||
// TODO: do we need this at all? vs. Ready/Resumed
|
||||
_logger.Information("Shard #{Shard} connected", shard.ShardId);
|
||||
_shardInfo[shard.ShardId].LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
_shardInfo[shard.ShardId].Connected = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task SocketClosed(SocketCloseEventArgs e)
|
||||
{
|
||||
_logger.Warning("Shard #{Shard} disconnected ({CloseCode}: {CloseMessage})", e.Client.ShardId, e.CloseCode, e.CloseMessage);
|
||||
_shardInfo[e.Client.ShardId].DisconnectionCount++;
|
||||
_shardInfo[e.Client.ShardId].Connected = false;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task Heartbeated(HeartbeatEventArgs e)
|
||||
{
|
||||
var latency = Duration.FromMilliseconds(e.Ping);
|
||||
_logger.Information("Shard #{Shard} received heartbeat (latency: {Latency} ms)", e.Client.ShardId, latency.Milliseconds);
|
||||
_shardInfo[e.Client.ShardId].LastHeartbeatTime = e.Timestamp.ToInstant();
|
||||
_shardInfo[e.Client.ShardId].ShardLatency = latency;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public ShardInfo GetShardInfo(DiscordClient shard) => _shardInfo[shard.ShardId];
|
||||
|
||||
private Task ShardLatencyUpdated(int oldLatency, int newLatency, DiscordClient shard)
|
||||
{
|
||||
_shardInfo[shard.ShardId].ShardLatency = newLatency;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task ShardReady(DiscordClient shard)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task ShardDisconnected(Exception e, DiscordClient shard)
|
||||
{
|
||||
_shardInfo[shard.ShardId].DisconnectionCount++;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task ShardConnected(DiscordClient shard)
|
||||
{
|
||||
_shardInfo[shard.ShardId].LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public ICollection<ShardInfo> Shards => _shardInfo.Values;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user