Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor changes to the plugin interface #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/EventStore.Plugins/IPlugableComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ public interface IPlugableComponent {
/// </summary>
/// <param name="services">The IServiceCollection to use for configuration.</param>
/// <param name="configuration">The IConfiguration to use for configuration.</param>
/// <returns>The configured IServiceCollection.</returns>
IServiceCollection ConfigureServices(IServiceCollection services, IConfiguration configuration);
void ConfigureServices(IServiceCollection services, IConfiguration configuration);

/// <summary>
/// Configures the application using the provided WebHostBuilderContext and IApplicationBuilder.
/// </summary>
/// <param name="builder">The IApplicationBuilder to use for configuration.</param>
/// <returns>The configured IApplicationBuilder.</returns>
IApplicationBuilder Configure(IApplicationBuilder builder);
/// <param name="configuration">The IConfiguration to use for configuration.</param>
void ConfigureApplication(IApplicationBuilder builder, IConfiguration configuration);
}
21 changes: 14 additions & 7 deletions src/EventStore.Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILoggerFactory>().CreateLogger(GetType());
Expand All @@ -109,18 +107,22 @@ IApplicationBuilder IPlugableComponent.Configure(IApplicationBuilder app) {
Version, IsEnabledResult.EnableInstructions
);

return app;
return;
}

logger.LogInformation("{Version} plugin enabled.", Version);

ConfigureApplication(app, Configuration);

PublishDiagnostics(new() { ["enabled"] = Enabled });

return app;
}

/// <summary>
/// Publishes diagnostics data.
/// Used for ESDB telemetry
/// </summary>
/// <param name="eventName">The name of the event to publish.</param>
/// <param name="eventData">The data to publish.</param>
protected internal void PublishDiagnostics(string eventName, Dictionary<string, object?> eventData) {
DiagnosticListener.Write(
nameof(PluginDiagnosticsData),
Expand All @@ -133,6 +135,11 @@ protected internal void PublishDiagnostics(string eventName, Dictionary<string,
);
}

/// <summary>
/// Publishes diagnostics data.
/// Used for ESDB telemetry
/// </summary>
/// <param name="eventData">The data to publish.</param>
protected internal void PublishDiagnostics(Dictionary<string, object?> eventData) =>
PublishDiagnostics(nameof(PluginDiagnosticsData), eventData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(
app.Services.GetRequiredService<IMeterFactory>(),
Expand Down
6 changes: 3 additions & 3 deletions test/EventStore.Plugins.Tests/PluginBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginLicenseException>().Which
Expand All @@ -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<PluginLicenseException>().Which
Expand All @@ -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<Exception>();
Expand Down
Loading