From c4cd4aace55a0fe806530c6d9fd69be04b6a9115 Mon Sep 17 00:00:00 2001 From: AffectedArc07 <25063394+AffectedArc07@users.noreply.github.com> Date: Tue, 8 Jun 2021 13:34:08 +0100 Subject: [PATCH] Elasticsearch log ingest support --- build/Version.props | 2 +- .../ElasticsearchConfiguration.cs | 53 +++++++++++++++++++ src/Tgstation.Server.Host/Core/Application.cs | 1 + .../Extensions/ServiceCollectionExtensions.cs | 30 ++++++++++- .../Setup/SetupApplication.cs | 3 +- .../Tgstation.Server.Host.csproj | 2 + 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/Tgstation.Server.Host/Configuration/ElasticsearchConfiguration.cs diff --git a/build/Version.props b/build/Version.props index 5ea4c486cb..ed5e4717c5 100644 --- a/build/Version.props +++ b/build/Version.props @@ -4,7 +4,7 @@ 4.12.0 - 3.0.0 + 3.0.1 9.0.1 9.0.0 10.0.0 diff --git a/src/Tgstation.Server.Host/Configuration/ElasticsearchConfiguration.cs b/src/Tgstation.Server.Host/Configuration/ElasticsearchConfiguration.cs new file mode 100644 index 0000000000..18894682c1 --- /dev/null +++ b/src/Tgstation.Server.Host/Configuration/ElasticsearchConfiguration.cs @@ -0,0 +1,53 @@ +namespace Tgstation.Server.Host.Configuration + { + /// + /// Configuration options pertaining to elasticsearch log storage. + /// + sealed class ElasticsearchConfiguration + { + /// + /// The key for the the resides in. + /// + public const string Section = "Elasticsearch"; + + /// + /// Default value of . + /// + const bool DefaultEnable = false; + + /// + /// Default value of . + /// + const string DefaultHost = "http://127.0.0.1:9200"; // localhost + + /// + /// Default value of . + /// + const string DefaultUsername = "my_username"; + + /// + /// Default value of . + /// + const string DefaultPassword = "my_password"; + + /// + /// Do we want to enable elasticsearch or not?. + /// + public bool Enable { get; set; } = DefaultEnable; + + /// + /// The host of the elasticsearch endpoint. + /// + public string Host { get; set; } = DefaultHost; + + /// + /// Username for elasticsearch. + /// + public string Username { get; set; } = DefaultUsername; + + /// + /// Password for elasticsearch. + /// + public string Password { get; set; } = DefaultPassword; + } +} diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index d9b4928689..43e0212e91 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -151,6 +151,7 @@ namespace Tgstation.Server.Host.Core config.MinimumLevel.Override("System.Net.Http.HttpClient", microsoftEventLevel.Value); } }, + Configuration, sinkConfig => { if (postSetupServices.FileLoggingConfiguration.Disable) diff --git a/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs b/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs index d383cdccba..ba38928a55 100644 --- a/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs +++ b/src/Tgstation.Server.Host/Extensions/ServiceCollectionExtensions.cs @@ -2,12 +2,14 @@ using System.Diagnostics; using System.Globalization; +using Elastic.CommonSchema.Serilog; + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Configuration; - +using Serilog.Sinks.Elasticsearch; using Tgstation.Server.Host.Configuration; namespace Tgstation.Server.Host.Extensions @@ -57,11 +59,13 @@ namespace Tgstation.Server.Host.Extensions /// /// The to configure. /// Additional configuration for a given . + /// The service configuration for a given . /// Additional configuration for a given . /// The updated . public static IServiceCollection SetupLogging( this IServiceCollection serviceCollection, Action configurationAction, + IConfiguration tgsConfiguration, Action sinkConfigurationAction = null) => serviceCollection.AddLogging(builder => { @@ -85,6 +89,30 @@ namespace Tgstation.Server.Host.Extensions sinkConfigurationAction?.Invoke(sinkConfiguration); }); + string elasticsearchEndpoint = tgsConfiguration.GetSection("Elasticsearch").GetSection("Host").Value; + string elasticsearchUser = tgsConfiguration.GetSection("Elasticsearch").GetSection("Username").Value; + string elasticsearchPassword = tgsConfiguration.GetSection("Elasticsearch").GetSection("Password").Value; + string raw_bool = tgsConfiguration.GetSection("Elasticsearch").GetSection("Enable").Value; + bool elasticsearchEnabled = false; + + if (!string.IsNullOrEmpty(raw_bool)) + elasticsearchEnabled = bool.Parse(raw_bool); + + if (elasticsearchEnabled) + { + if (elasticsearchEndpoint == null) + throw new InvalidOperationException("Elasticsearch endpoint is null!"); + + configuration.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticsearchEndpoint)) + { + ModifyConnectionSettings = x => (!string.IsNullOrEmpty(elasticsearchUser) && !string.IsNullOrEmpty(elasticsearchPassword)) ? x.BasicAuthentication(elasticsearchUser, elasticsearchPassword) : null, + CustomFormatter = new EcsTextFormatter(), + AutoRegisterTemplate = true, + AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, + IndexFormat = "tgs4-logs", + }); + } + builder.AddSerilog(configuration.CreateLogger(), true); if (Debugger.IsAttached) diff --git a/src/Tgstation.Server.Host/Setup/SetupApplication.cs b/src/Tgstation.Server.Host/Setup/SetupApplication.cs index fc45fb2ce6..9b940fd128 100644 --- a/src/Tgstation.Server.Host/Setup/SetupApplication.cs +++ b/src/Tgstation.Server.Host/Setup/SetupApplication.cs @@ -52,7 +52,7 @@ namespace Tgstation.Server.Host.Setup if (services == null) throw new ArgumentNullException(nameof(services)); - services.SetupLogging(config => config.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)); + services.SetupLogging(config => config.MinimumLevel.Override("Microsoft", LogEventLevel.Warning), Configuration); services.AddSingleton(IOManager); services.AddSingleton(AssemblyInformationProvider); @@ -66,6 +66,7 @@ namespace Tgstation.Server.Host.Setup services.UseStandardConfig(Configuration); services.UseStandardConfig(Configuration); services.UseStandardConfig(Configuration); + services.UseStandardConfig(Configuration); ConfigureHostedService(services); } diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index 5789898241..084ee35ed9 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -67,6 +67,7 @@ + @@ -91,6 +92,7 @@ + all