From 4c653cf9fbd33aa35ddabcc40e1a9898e3edb689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Silveira?= Date: Wed, 22 May 2024 17:02:37 +0200 Subject: [PATCH] minor changes to the plugin interface --- src/EventStore.Plugins/IPlugableComponent.cs | 7 +++---- src/EventStore.Plugins/Plugin.cs | 21 ++++++++++++------- .../Diagnostics/PluginMetricsTests.cs | 2 +- .../PluginBaseTests.cs | 6 +++--- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/EventStore.Plugins/IPlugableComponent.cs b/src/EventStore.Plugins/IPlugableComponent.cs index baf9347..6c34b51 100644 --- a/src/EventStore.Plugins/IPlugableComponent.cs +++ b/src/EventStore.Plugins/IPlugableComponent.cs @@ -43,13 +43,12 @@ public interface IPlugableComponent { /// /// The IServiceCollection to use for configuration. /// The IConfiguration to use for configuration. - /// The configured IServiceCollection. - IServiceCollection ConfigureServices(IServiceCollection services, IConfiguration configuration); + void ConfigureServices(IServiceCollection services, IConfiguration configuration); /// /// Configures the application using the provided WebHostBuilderContext and IApplicationBuilder. /// /// The IApplicationBuilder to use for configuration. - /// The configured IApplicationBuilder. - IApplicationBuilder Configure(IApplicationBuilder builder); + /// The IConfiguration to use for configuration. + void ConfigureApplication(IApplicationBuilder builder, IConfiguration configuration); } \ No newline at end of file diff --git a/src/EventStore.Plugins/Plugin.cs b/src/EventStore.Plugins/Plugin.cs index 0b2aabe..6d3a9af 100644 --- a/src/EventStore.Plugins/Plugin.cs +++ b/src/EventStore.Plugins/Plugin.cs @@ -84,17 +84,15 @@ public virtual void ConfigureApplication(IApplicationBuilder app, IConfiguration public virtual (bool Enabled, string EnableInstructions) IsEnabled(IConfiguration configuration) => (true, ""); - IServiceCollection IPlugableComponent.ConfigureServices(IServiceCollection services, IConfiguration configuration) { + void IPlugableComponent.ConfigureServices(IServiceCollection services, IConfiguration configuration) { Configuration = configuration; IsEnabledResult = IsEnabled(configuration); if (Enabled) ConfigureServices(services, configuration); - - return services; } - IApplicationBuilder IPlugableComponent.Configure(IApplicationBuilder app) { + void IPlugableComponent.ConfigureApplication(IApplicationBuilder app, IConfiguration configuration) { PublishDiagnostics(new() { ["enabled"] = Enabled }); var logger = app.ApplicationServices.GetRequiredService().CreateLogger(GetType()); @@ -109,7 +107,7 @@ IApplicationBuilder IPlugableComponent.Configure(IApplicationBuilder app) { Version, IsEnabledResult.EnableInstructions ); - return app; + return; } logger.LogInformation("{Version} plugin enabled.", Version); @@ -117,10 +115,14 @@ IApplicationBuilder IPlugableComponent.Configure(IApplicationBuilder app) { ConfigureApplication(app, Configuration); PublishDiagnostics(new() { ["enabled"] = Enabled }); - - return app; } + /// + /// Publishes diagnostics data. + /// Used for ESDB telemetry + /// + /// The name of the event to publish. + /// The data to publish. protected internal void PublishDiagnostics(string eventName, Dictionary eventData) { DiagnosticListener.Write( nameof(PluginDiagnosticsData), @@ -133,6 +135,11 @@ protected internal void PublishDiagnostics(string eventName, Dictionary + /// Publishes diagnostics data. + /// Used for ESDB telemetry + /// + /// The data to publish. protected internal void PublishDiagnostics(Dictionary eventData) => PublishDiagnostics(nameof(PluginDiagnosticsData), eventData); diff --git a/test/EventStore.Plugins.Tests/Diagnostics/PluginMetricsTests.cs b/test/EventStore.Plugins.Tests/Diagnostics/PluginMetricsTests.cs index 0cc0ac2..2d51cd2 100644 --- a/test/EventStore.Plugins.Tests/Diagnostics/PluginMetricsTests.cs +++ b/test/EventStore.Plugins.Tests/Diagnostics/PluginMetricsTests.cs @@ -18,7 +18,7 @@ public void can_receive_metrics_from_plugin() { using var app = builder.Build(); - plugin.Configure(app); + plugin.ConfigureApplication(app, builder.Configuration); using var collector = new MetricCollector( app.Services.GetRequiredService(), diff --git a/test/EventStore.Plugins.Tests/PluginBaseTests.cs b/test/EventStore.Plugins.Tests/PluginBaseTests.cs index 375d179..6ff092d 100644 --- a/test/EventStore.Plugins.Tests/PluginBaseTests.cs +++ b/test/EventStore.Plugins.Tests/PluginBaseTests.cs @@ -50,7 +50,7 @@ public void comercial_plugin_is_disabled_when_licence_is_missing() { using var app = builder.Build(); - Action configure = () => plugin.Configure(app); + var configure = () => plugin.ConfigureApplication(app, EmptyConfiguration); // Act & Assert configure.Should().Throw().Which @@ -75,7 +75,7 @@ public void comercial_plugin_is_disabled_when_licence_is_invalid() { using var app = builder.Build(); - Action configure = () => plugin.Configure(app); + var configure = () => plugin.ConfigureApplication(app, EmptyConfiguration); // Act & Assert configure.Should().Throw().Which @@ -99,7 +99,7 @@ public void comercial_plugin_is_enabled_when_licence_is_present() { using var app = builder.Build(); - Action configure = () => plugin.Configure(app); + var configure = () => plugin.ConfigureApplication(app, builder.Configuration); // Act & Assert configure.Should().NotThrow();