From a689d6178892548a139a0670b094a44369fc05de Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 12 Aug 2019 00:57:23 +0200 Subject: [PATCH] Collect webhook response time and shard latency --- PluralKit.Bot/Bot.cs | 20 ++++++++++++++++++-- PluralKit.Bot/BotMetrics.cs | 4 ++++ PluralKit.Bot/Services/ProxyService.cs | 5 ++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 315d5300..efac818e 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -312,8 +312,8 @@ namespace PluralKit.Bot public async Task HandleMessage(SocketMessage msg) { - _metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived); - + RegisterMessageMetrics(msg); + // _client.CurrentUser will be null if we've connected *some* shards but not shard #0 yet // This will cause an error in WebhookCacheServices so we just workaround and don't process any messages // until we properly connect. TODO: can we do this without chucking away a bunch of messages? @@ -347,6 +347,7 @@ namespace PluralKit.Bot system = await conn.QueryFirstOrDefaultAsync( "select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new {Id = arg.Author.Id}); + await _commands.ExecuteAsync(new PKCommandContext(_client, arg, system, _services), argPos, _services); } @@ -357,6 +358,21 @@ namespace PluralKit.Bot } } + private void RegisterMessageMetrics(SocketMessage msg) + { + var guild = (msg.Channel as IGuildChannel)?.Guild; + if (guild != null) + { + var shard = _client.GetShardFor(guild); + var latencyMillis = shard.Latency; + + _metrics.Provider.Timer.Instance(BotMetrics.GatewayLatency, new MetricTags("shard", shard.ShardId.ToString())) + .Record(latencyMillis, TimeUnit.Milliseconds); + } + + _metrics.Measure.Meter.Mark(BotMetrics.MessagesReceived); + } + public Task HandleReactionAdded(Cacheable message, ISocketMessageChannel channel, SocketReaction reaction) => _proxy.HandleReactionAddedAsync(message, channel, reaction); diff --git a/PluralKit.Bot/BotMetrics.cs b/PluralKit.Bot/BotMetrics.cs index 2ec66925..e335c653 100644 --- a/PluralKit.Bot/BotMetrics.cs +++ b/PluralKit.Bot/BotMetrics.cs @@ -1,6 +1,8 @@ using App.Metrics; using App.Metrics.Gauge; +using App.Metrics.Histogram; using App.Metrics.Meter; +using App.Metrics.Timer; namespace PluralKit.Bot { @@ -15,5 +17,7 @@ namespace PluralKit.Bot public static GaugeOptions Channels => new GaugeOptions {Name = "Channels", MeasurementUnit = Unit.None, Context = "Bot"}; public static GaugeOptions ShardsConnected => new GaugeOptions { Name = "Shards Connected", Context = "Bot" }; public static GaugeOptions WebhookCacheSize => new GaugeOptions { Name = "Webhook Cache Size", Context = "Bot" }; + public static TimerOptions WebhookResponseTime => new TimerOptions { Name = "Webhook Response Time", Context = "Bot" }; + public static TimerOptions GatewayLatency => new TimerOptions { Name = "Gateway Latency", Context = "Bot" }; } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index c65d6662..289f7b64 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -117,7 +117,10 @@ namespace PluralKit.Bot var webhook = await _webhookCache.GetWebhook(message.Channel as ITextChannel); var avatarUrl = match.Member.AvatarUrl ?? match.System.AvatarUrl; var proxyName = match.Member.ProxyName(match.System.Tag); - var hookMessageId = await ExecuteWebhook(webhook, messageContents, proxyName, avatarUrl, message.Attachments.FirstOrDefault()); + + ulong hookMessageId; + using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime)) + hookMessageId = await ExecuteWebhook(webhook, messageContents, proxyName, avatarUrl, message.Attachments.FirstOrDefault()); // Store the message in the database, and log it in the log channel (if applicable) await _messageStorage.Store(message.Author.Id, hookMessageId, message.Channel.Id, message.Id, match.Member);