From 2e6b1826fcb688432960ccebd10dea0a6cdb1a51 Mon Sep 17 00:00:00 2001 From: Ske Date: Wed, 26 Aug 2020 22:29:24 +0200 Subject: [PATCH] Add experimental Elasticsearch logging output --- PluralKit.Bot/Bot.cs | 14 +++++++++++++ PluralKit.Core/CoreConfig.cs | 1 + PluralKit.Core/Modules.cs | 30 +++++++++++++++++++++++----- PluralKit.Core/PluralKit.Core.csproj | 1 + 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index b6b71e99..c2f02d20 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -21,6 +21,7 @@ using PluralKit.Core; using Sentry; using Serilog; +using Serilog.Context; using Serilog.Events; namespace PluralKit.Bot @@ -101,6 +102,19 @@ namespace PluralKit.Bot async Task HandleEventInner() { + // Mainly for testing ELK volume atm, no-op unless Elastic is configured + if (evt is MessageCreateEventArgs mc) + using (LogContext.PushProperty("Elastic", "yes?")) + _logger.Information("Received event {@Event}", new + { + Id = Guid.NewGuid(), + Type = mc.GetType().Name.Replace("EventArgs", ""), + MessageId = mc.Message.Id, + ChannelId = mc.Channel.Id, + GuildId = mc.Guild?.Id ?? 0, + UserId = mc.Author.Id, + }); + await using var serviceScope = _services.BeginLifetimeScope(); // Also, find a Sentry enricher for the event type (if one is present), and ask it to put some event data in the Sentry scope diff --git a/PluralKit.Core/CoreConfig.cs b/PluralKit.Core/CoreConfig.cs index 35d88e46..800547a4 100644 --- a/PluralKit.Core/CoreConfig.cs +++ b/PluralKit.Core/CoreConfig.cs @@ -9,6 +9,7 @@ namespace PluralKit.Core public string InfluxUrl { get; set; } public string InfluxDb { get; set; } public string LogDir { get; set; } + public string ElasticUrl { get; set; } public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Debug; public LogEventLevel FileLogLevel { get; set; } = LogEventLevel.Information; diff --git a/PluralKit.Core/Modules.cs b/PluralKit.Core/Modules.cs index 8e7db2dc..0a490b35 100644 --- a/PluralKit.Core/Modules.cs +++ b/PluralKit.Core/Modules.cs @@ -12,7 +12,9 @@ using Microsoft.Extensions.DependencyInjection; using NodaTime; using Serilog; +using Serilog.Events; using Serilog.Formatting.Compact; +using Serilog.Sinks.Elasticsearch; using Serilog.Sinks.SystemConsole.Themes; namespace PluralKit.Core @@ -95,7 +97,8 @@ namespace PluralKit.Core { var outputTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.ffffff}] {Level:u3} {Message:lj}{NewLine}{Exception}"; - return new LoggerConfiguration() + var logger = new LoggerConfiguration() + .Enrich.FromLogContext() .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb) .MinimumLevel.Is(config.ConsoleLogLevel) .WriteTo.Async(a => @@ -111,7 +114,7 @@ namespace PluralKit.Core restrictedToMinimumLevel: config.FileLogLevel, formatProvider: new UTCTimestampFormatProvider(), buffered: true); - + a.File( new RenderedCompactJsonFormatter(), (config.LogDir ?? "logs") + $"/pluralkit.{_component}.json", @@ -121,9 +124,26 @@ namespace PluralKit.Core buffered: true); }) // TODO: render as UTC in the console, too? or just in log files - .WriteTo.Async(a => - a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate, formatProvider: new UTCTimestampFormatProvider())) - .CreateLogger(); + .WriteTo.Async(a => + a.Console(theme: AnsiConsoleTheme.Code, outputTemplate: outputTemplate, + formatProvider: new UTCTimestampFormatProvider())); + + if (config.ElasticUrl != null) + { + var elasticConfig = new ElasticsearchSinkOptions(new Uri(config.ElasticUrl)) + { + AutoRegisterTemplate = true, + AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, + MinimumLogEventLevel = LogEventLevel.Verbose, + IndexFormat = "pluralkit-logs-{0:yyyy.MM.dd}" + }; + + logger.WriteTo + .Conditional(e => e.Properties.ContainsKey("Elastic"), + c => c.Elasticsearch(elasticConfig)); + } + + return logger.CreateLogger(); } } diff --git a/PluralKit.Core/PluralKit.Core.csproj b/PluralKit.Core/PluralKit.Core.csproj index d848d014..0fe8e7ea 100644 --- a/PluralKit.Core/PluralKit.Core.csproj +++ b/PluralKit.Core/PluralKit.Core.csproj @@ -33,6 +33,7 @@ +