Repository navigation
Add configuration-based automatic health monitoring registration #739
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee60ef0
Document automatic health monitoring registration configuration
StuartFerguson 710f75f
fix: resolve Codacy test double warnings
StuartFerguson 6fd871a
Merge branch 'main' into task/change_to_migrations
StuartFerguson a724879
fix: honor cancellation in registration test
StuartFerguson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
HealthMonitoring.Client/HealthMonitoringRegistrationExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace HealthMonitoring.Client; | ||
|
|
||
| public sealed class HealthMonitoringRegistrationConfiguration | ||
| { | ||
| public Uri MonitoringServerUrl { get; set; } = new("http://localhost:9620/"); | ||
| public ServiceRegistrationOptions Service { get; set; } = new(); | ||
| public List<DependencyMappingOptions> Dependencies { get; set; } = []; | ||
| } | ||
|
|
||
| public sealed class HealthMonitoringRegistrationHostedService( | ||
| IHealthMonitoringRegistrationClient client, | ||
| HealthMonitoringRegistrationConfiguration configuration) : IHostedService | ||
| { | ||
| public async Task StartAsync(CancellationToken cancellationToken) | ||
| { | ||
| await client.RegisterAndConfigureAsync( | ||
| configuration.Service, | ||
| configuration.Dependencies, | ||
| cancellationToken); | ||
| } | ||
|
|
||
| public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
| } | ||
|
|
||
| public static class HealthMonitoringRegistrationExtensions | ||
| { | ||
| public static IServiceCollection AddHealthMonitoringRegistration( | ||
| this IServiceCollection services, | ||
| IConfiguration configuration, | ||
| string sectionName = "HealthMonitoring") | ||
| { | ||
| ArgumentNullException.ThrowIfNull(services); | ||
| ArgumentNullException.ThrowIfNull(configuration); | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(sectionName); | ||
|
|
||
| var section = configuration.GetSection(sectionName); | ||
| if (!section.Exists()) | ||
| return services; | ||
|
|
||
| var registration = new HealthMonitoringRegistrationConfiguration(); | ||
| section.Bind(registration); | ||
| if (!registration.MonitoringServerUrl.IsAbsoluteUri) | ||
| throw new ArgumentException("The monitoring server URL must be absolute.", nameof(configuration)); | ||
|
|
||
| services.AddSingleton(registration); | ||
| services.AddHttpClient<IHealthMonitoringRegistrationClient, HealthMonitoringRegistrationClient>(client => | ||
| { | ||
| client.BaseAddress = registration.MonitoringServerUrl; | ||
| }); | ||
| services.AddHostedService<HealthMonitoringRegistrationHostedService>(); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
HealthMonitoring.Tests/Client/HealthMonitoringRegistrationExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using HealthMonitoring.Client; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace HealthMonitoring.Tests.Client; | ||
|
|
||
| public sealed class HealthMonitoringRegistrationExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void Configuration_registration_binds_service_settings_and_adds_startup_registration() | ||
| { | ||
| var configuration = new ConfigurationBuilder() | ||
| .AddInMemoryCollection(new Dictionary<string, string?> | ||
| { | ||
| ["HealthMonitoring:MonitoringServerUrl"] = "https://monitoring.example/", | ||
| ["HealthMonitoring:Service:ServiceId"] = "mobile-configuration", | ||
| ["HealthMonitoring:Service:Name"] = "Mobile Configuration", | ||
| ["HealthMonitoring:Service:HealthUrl"] = "https://mobile.example/healthui", | ||
| ["HealthMonitoring:Service:Description"] = "Mobile Configuration", | ||
| ["HealthMonitoring:Service:PollingInterval"] = "00:02:00", | ||
| ["HealthMonitoring:Dependencies:0:DependencyName"] = "Security Service", | ||
| ["HealthMonitoring:Dependencies:0:TargetServiceId"] = "security-service" | ||
| }) | ||
| .Build(); | ||
|
|
||
| var services = new ServiceCollection(); | ||
|
|
||
| services.AddHealthMonitoringRegistration(configuration); | ||
|
|
||
| using var provider = services.BuildServiceProvider(); | ||
| var options = provider.GetRequiredService<HealthMonitoringRegistrationConfiguration>(); | ||
|
|
||
| Assert.Equal(new Uri("https://monitoring.example/"), options.MonitoringServerUrl); | ||
| Assert.Equal("mobile-configuration", options.Service.ServiceId); | ||
| Assert.Equal(TimeSpan.FromMinutes(2), options.Service.PollingInterval); | ||
| Assert.Single(options.Dependencies); | ||
| Assert.Contains(provider.GetServices<IHostedService>(), service => service is HealthMonitoringRegistrationHostedService); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Missing_configuration_section_skips_automatic_registration() | ||
| { | ||
| var configuration = new ConfigurationBuilder().Build(); | ||
| var services = new ServiceCollection(); | ||
|
|
||
| services.AddHealthMonitoringRegistration(configuration); | ||
|
|
||
| using var provider = services.BuildServiceProvider(); | ||
|
|
||
| Assert.Empty(provider.GetServices<IHostedService>()); | ||
| Assert.Throws<InvalidOperationException>(() => | ||
| provider.GetRequiredService<IHealthMonitoringRegistrationClient>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Startup_registration_calls_register_and_configure() | ||
| { | ||
| var configuration = new HealthMonitoringRegistrationConfiguration | ||
| { | ||
| Service = new ServiceRegistrationOptions | ||
| { | ||
| ServiceId = "mobile-configuration", | ||
| Name = "Mobile Configuration", | ||
| HealthUrl = new Uri("https://mobile.example/healthui") | ||
| }, | ||
| Dependencies = [new DependencyMappingOptions("Security Service", "security-service")] | ||
| }; | ||
| var client = new RecordingRegistrationClient(); | ||
|
|
||
| await new HealthMonitoringRegistrationHostedService(client, configuration) | ||
| .StartAsync(CancellationToken.None); | ||
|
|
||
| Assert.Same(configuration.Service, client.Service); | ||
| Assert.Same(configuration.Dependencies, client.Dependencies); | ||
| } | ||
|
|
||
| private sealed class RecordingRegistrationClient : IHealthMonitoringRegistrationClient | ||
| { | ||
| public ServiceRegistrationOptions? Service { get; private set; } | ||
| public IEnumerable<DependencyMappingOptions>? Dependencies { get; private set; } | ||
|
|
||
| public Task<ServiceRegistrationResult> RegisterAsync(ServiceRegistrationOptions options, CancellationToken cancellationToken = default) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| throw new NotSupportedException($"RegisterAsync is not used for {options.ServiceId}."); | ||
| } | ||
|
|
||
| public Task SetDependencyMappingsAsync(string serviceId, IEnumerable<DependencyMappingOptions> mappings, CancellationToken cancellationToken = default) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| throw new NotSupportedException($"SetDependencyMappingsAsync is not used for {serviceId} with {mappings.Count()} mappings."); | ||
| } | ||
|
|
||
| public Task<ServiceRegistrationResult> RegisterAndConfigureAsync(ServiceRegistrationOptions options, IEnumerable<DependencyMappingOptions> mappings, CancellationToken cancellationToken = default) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| Service = options; | ||
| Dependencies = mappings; | ||
| return Task.FromResult(new ServiceRegistrationResult(options.ServiceId, "created", Guid.NewGuid())); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.