fix: better logging

- verbose logging for log channel resolving logic
- don't LOG TOKENS TO THE CONSOLE OR SEQ
- actually log verbose logs to seq
- report open db connections to prometheus
This commit is contained in:
sam 2024-11-27 16:17:11 +01:00
parent 7749c9d9e2
commit 5157105c35
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
7 changed files with 109 additions and 50 deletions

View file

@ -43,6 +43,7 @@ public class MetricsCollectionService(
var messageCount = await conn.ExecuteScalarAsync<int>("select count(id) from messages");
CataloggerMetrics.DatabaseConnections.Set(DatabasePool.OpenConnections);
CataloggerMetrics.GuildsCached.Set(guildCache.Size);
CataloggerMetrics.ChannelsCached.Set(channelCache.Size);
CataloggerMetrics.RolesCached.Set(roleCache.Size);

View file

@ -315,6 +315,13 @@ public class WebhookExecutorService(
Snowflake channelId
)
{
_logger.Verbose(
"Getting log channel for event {Event} in guild {GuildId} and channel {ChannelId}",
logChannelType,
guild.Id,
channelId
);
if (!channelCache.TryGet(channelId, out var channel))
return GetDefaultLogChannel(guild, logChannelType);
@ -329,7 +336,13 @@ public class WebhookExecutorService(
// parent_id should always have a value for threads
channelId = channel.ParentID.Value!.Value;
if (!channelCache.TryGet(channelId, out var parentChannel))
{
_logger.Verbose(
"Parent channel for thread {ChannelId} is not in cache, returning the default log channel",
channelId
);
return GetDefaultLogChannel(guild, logChannelType);
}
categoryId = parentChannel.ParentID.Value;
}
else
@ -343,8 +356,16 @@ public class WebhookExecutorService(
guild.IgnoredChannels.Contains(channelId.Value)
|| (categoryId != null && guild.IgnoredChannels.Contains(categoryId.Value.Value))
)
{
_logger.Verbose(
"Channel {ChannelId} or its parent {CategoryId} is ignored",
channelId,
categoryId
);
return null;
}
_logger.Verbose("Returning default log channel for {EventType}", logChannelType);
return GetDefaultLogChannel(guild, logChannelType);
}
@ -355,13 +376,29 @@ public class WebhookExecutorService(
ulong? userId = null
)
{
_logger.Verbose(
"Getting log channel for event {Event}. Channel ID: {ChannelId}, user ID: {UserId}",
logChannelType,
channelId,
userId
);
// Check if the user is ignored globally
if (userId != null && guild.Messages.IgnoredUsers.Contains(userId.Value))
{
_logger.Verbose("User {UserId} is ignored globally", userId);
return null;
}
// If the user isn't ignored and we didn't get a channel ID, return the default log channel
if (channelId == null)
{
_logger.Verbose(
"No channel ID given so returning default channel for {Event}",
logChannelType
);
return GetDefaultLogChannel(guild, logChannelType);
}
if (!channelCache.TryGet(channelId.Value, out var channel))
return null;
@ -377,7 +414,13 @@ public class WebhookExecutorService(
// parent_id should always have a value for threads
channelId = channel.ParentID.Value!.Value;
if (!channelCache.TryGet(channelId.Value, out var parentChannel))
{
_logger.Verbose(
"Parent channel for thread {ChannelId} is not in cache, returning the default log channel",
channelId
);
return GetDefaultLogChannel(guild, logChannelType);
}
categoryId = parentChannel.ParentID.Value;
}
else
@ -391,7 +434,14 @@ public class WebhookExecutorService(
guild.Messages.IgnoredChannels.Contains(channelId.Value.Value)
|| categoryId != null && guild.Messages.IgnoredChannels.Contains(categoryId.Value.Value)
)
{
_logger.Verbose(
"Channel {ChannelId} or its parent {CategoryId} is ignored",
channelId,
categoryId
);
return null;
}
if (userId != null)
{
@ -408,13 +458,27 @@ public class WebhookExecutorService(
: []
) ?? [];
if (channelIgnoredUsers.Concat(categoryIgnoredUsers).Contains(userId.Value))
{
_logger.Verbose(
"User {UserId} is ignored in {ChannelId} or its category {CategoryId}",
userId,
channelId,
categoryId
);
return null;
}
}
// These three events can be redirected to other channels. Redirects can be on a channel or category level.
// The events are only redirected if they're supposed to be logged in the first place.
if (GetDefaultLogChannel(guild, logChannelType) == 0)
{
_logger.Verbose(
"No default log channel for event {EventType}, ignoring event",
logChannelType
);
return null;
}
var categoryRedirect =
categoryId != null
@ -422,10 +486,29 @@ public class WebhookExecutorService(
: 0;
if (guild.Channels.Redirects.TryGetValue(channelId.Value.Value, out var channelRedirect))
{
_logger.Verbose(
"Messages from channel {ChannelId} should be redirected to {RedirectId}",
channelId,
channelRedirect
);
return channelRedirect;
return categoryRedirect != 0
? categoryRedirect
: GetDefaultLogChannel(guild, logChannelType);
}
if (categoryRedirect != 0)
{
_logger.Verbose(
"Messages from categoryId {CategoryId} should be redirected to {RedirectId}",
categoryId,
categoryRedirect
);
}
_logger.Verbose(
"No redirects or ignores for event {EventType}, returning default log channel",
logChannelType
);
return GetDefaultLogChannel(guild, logChannelType);
}
public static ulong GetDefaultLogChannel(Guild guild, LogChannelType logChannelType) =>