feat(bot): add stats tracking for dm channel cache hits/misses

This commit is contained in:
spiral 2022-06-10 18:08:56 -04:00
parent 0f47042dd1
commit 1b5a90f612
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 33 additions and 1 deletions

View File

@ -22,6 +22,27 @@ public static class BotMetrics
Context = "Bot" Context = "Bot"
}; };
public static MeterOptions LocalDMCacheHits => new()
{
Name = "Cluster local DM Cache Hits",
MeasurementUnit = Unit.Calls,
Context = "Bot"
};
public static MeterOptions DatabaseDMCacheHits => new()
{
Name = "Database DM Cache Hits",
MeasurementUnit = Unit.Calls,
Context = "Bot"
};
public static MeterOptions DMCacheMisses => new()
{
Name = "DM Cache Misses",
MeasurementUnit = Unit.Calls,
Context = "Bot"
};
public static MeterOptions CommandsRun => new() public static MeterOptions CommandsRun => new()
{ {
Name = "Commands run", Name = "Commands run",

View File

@ -1,3 +1,5 @@
using App.Metrics;
using Serilog; using Serilog;
using Myriad.Gateway; using Myriad.Gateway;
@ -11,11 +13,13 @@ public class PrivateChannelService
{ {
private static readonly Dictionary<ulong, ulong> _channelsCache = new(); private static readonly Dictionary<ulong, ulong> _channelsCache = new();
private readonly IMetrics _metrics;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly DiscordApiClient _rest; private readonly DiscordApiClient _rest;
public PrivateChannelService(ILogger logger, ModelRepository repo, DiscordApiClient rest) public PrivateChannelService(IMetrics metrics, ILogger logger, ModelRepository repo, DiscordApiClient rest)
{ {
_metrics = metrics;
_logger = logger; _logger = logger;
_repo = repo; _repo = repo;
_rest = rest; _rest = rest;
@ -32,16 +36,23 @@ public class PrivateChannelService
public async Task<ulong> GetOrCreateDmChannel(ulong userId) public async Task<ulong> GetOrCreateDmChannel(ulong userId)
{ {
if (_channelsCache.TryGetValue(userId, out var cachedChannelId)) if (_channelsCache.TryGetValue(userId, out var cachedChannelId))
{
_metrics.Measure.Meter.Mark(BotMetrics.LocalDMCacheHits);
return cachedChannelId; return cachedChannelId;
}
var channelId = await _repo.GetDmChannel(userId); var channelId = await _repo.GetDmChannel(userId);
if (channelId == null) if (channelId == null)
{ {
_metrics.Measure.Meter.Mark(BotMetrics.DMCacheMisses);
var channel = await _rest.CreateDm(userId); var channel = await _rest.CreateDm(userId);
channelId = channel.Id; channelId = channel.Id;
} }
_metrics.Measure.Meter.Mark(BotMetrics.DatabaseDMCacheHits);
// spawn off saving the channel as to not block the current thread // spawn off saving the channel as to not block the current thread
// todo: don't save to database again if we just fetched it from there
_ = SaveDmChannel(userId, channelId.Value); _ = SaveDmChannel(userId, channelId.Value);
return channelId.Value; return channelId.Value;