From 6fb979e74d13cc618494675a67f8350d7ffb100d Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 16 Nov 2020 09:57:16 +0100 Subject: [PATCH] Add metrics for error/event rates --- PluralKit.Bot/Bot.cs | 8 +++++++- PluralKit.Bot/BotMetrics.cs | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index b4d6632b..bb7f53c5 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -117,12 +117,16 @@ namespace PluralKit.Bot // Find an event handler that can handle the type of event () we're given var handler = serviceScope.Resolve>(); var queue = serviceScope.ResolveOptional>(); + try { + using var timer = _metrics.Measure.Timer.Time(BotMetrics.EventsHandled, + new MetricTags("event", typeof(T).Name.Replace("EventArgs", ""))); + // Delegate to the queue to see if it wants to handle this event // the TryHandle call returns true if it's handled the event // Usually it won't, so just pass it on to the main handler - if (queue == null || !await queue.TryHandle(evt)) + if (queue == null || !await queue.TryHandle(evt)) await handler.Handle(shard, evt); } catch (Exception exc) @@ -135,6 +139,8 @@ namespace PluralKit.Bot private async Task HandleError(IEventHandler handler, T evt, ILifetimeScope serviceScope, Exception exc) where T: DiscordEventArgs { + _metrics.Measure.Meter.Mark(BotMetrics.BotErrors, exc.GetType().FullName); + // Make this beforehand so we can access the event ID for logging var sentryEvent = new SentryEvent(exc); diff --git a/PluralKit.Bot/BotMetrics.cs b/PluralKit.Bot/BotMetrics.cs index 689010ce..2686e570 100644 --- a/PluralKit.Bot/BotMetrics.cs +++ b/PluralKit.Bot/BotMetrics.cs @@ -22,5 +22,7 @@ namespace PluralKit.Bot public static TimerOptions MessageContextQueryTime => new TimerOptions { Name = "Message context query duration", Context = "Bot", RateUnit = TimeUnit.Seconds, DurationUnit = TimeUnit.Seconds, MeasurementUnit = Unit.Calls }; public static TimerOptions ProxyMembersQueryTime => new TimerOptions { Name = "Proxy member query duration", Context = "Bot", RateUnit = TimeUnit.Seconds, DurationUnit = TimeUnit.Seconds, MeasurementUnit = Unit.Calls }; public static TimerOptions DiscordApiRequests => new TimerOptions { Name = "Discord API requests", MeasurementUnit = Unit.Requests, Context = "Bot"}; + public static MeterOptions BotErrors => new MeterOptions { Name = "Bot errors", MeasurementUnit = Unit.Errors, RateUnit = TimeUnit.Seconds, Context = "Bot"}; + public static TimerOptions EventsHandled => new TimerOptions { Name = "Events handled", MeasurementUnit = Unit.Errors, RateUnit = TimeUnit.Seconds, DurationUnit = TimeUnit.Seconds, Context = "Bot"}; } } \ No newline at end of file