Skip to content

Commit bd270bc

Browse files
Merge pull request #96 from StuartFerguson/task/#30_healthchecks
Health checks added
2 parents ac4d597 + 57f648d commit bd270bc

File tree

3 files changed

+69
-61
lines changed

3 files changed

+69
-61
lines changed

TransactionProcessor.Tests/General/BootstrapperTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private IConfigurationRoot SetupMemoryConfiguration()
5252
configuration.Add("AppSettings:ClientSecret", "clientSecret");
5353
configuration.Add("AppSettings:EstateManagementApi", "http://localhost");
5454
configuration.Add("AppSettings:SecurityService", "http://localhost");
55+
configuration.Add("SecurityConfiguration:Authority", "http://localhost");
5556

5657
builder.AddInMemoryCollection(configuration);
5758

TransactionProcessor/Startup.cs

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ namespace TransactionProcessor
2626
using Common;
2727
using EstateManagement.Client;
2828
using EventStore.Client;
29+
using HealthChecks.UI.Client;
2930
using MediatR;
3031
using MessagingService.BusinessLogic.EventHandling;
3132
using Microsoft.AspNetCore.Authentication.JwtBearer;
33+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3234
using Microsoft.AspNetCore.Mvc.ApiExplorer;
3335
using Microsoft.AspNetCore.Mvc.Versioning;
36+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3437
using Microsoft.Extensions.Options;
3538
using Microsoft.IdentityModel.Logging;
3639
using Models;
@@ -69,6 +72,10 @@ public Startup(IWebHostEnvironment webHostEnvironment)
6972
// This method gets called by the runtime. Use this method to add services to the container.
7073
public void ConfigureServices(IServiceCollection services)
7174
{
75+
ConfigurationReader.Initialise(Startup.Configuration);
76+
77+
Startup.ConfigureEventStoreSettings();
78+
7279
this.ConfigureMiddlewareServices(services);
7380

7481
services.AddTransient<IMediator, Mediator>();
@@ -107,51 +114,8 @@ public void ConfigureServices(IServiceCollection services)
107114
}
108115
else
109116
{
110-
services.AddEventStoreClient((settings) =>
111-
{
112-
settings.CreateHttpMessageHandler = () => new SocketsHttpHandler
113-
{
114-
SslOptions =
115-
{
116-
RemoteCertificateValidationCallback = (sender,
117-
certificate,
118-
chain,
119-
errors) => true,
120-
}
121-
};
122-
settings.ConnectionName = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionName");
123-
settings.ConnectivitySettings = new EventStoreClientConnectivitySettings
124-
{
125-
Address =
126-
new Uri(Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionString")),
127-
};
128-
settings.DefaultCredentials = new UserCredentials(Startup.Configuration.GetValue<String>("EventStoreSettings:UserName"),
129-
Startup.Configuration.GetValue<String>("EventStoreSettings:Password"));
130-
});
131-
132-
133-
134-
services.AddEventStoreProjectionManagerClient((settings) =>
135-
{
136-
settings.CreateHttpMessageHandler = () => new SocketsHttpHandler
137-
{
138-
SslOptions =
139-
{
140-
RemoteCertificateValidationCallback = (sender,
141-
certificate,
142-
chain,
143-
errors) => true,
144-
}
145-
};
146-
settings.ConnectionName = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionName");
147-
settings.ConnectivitySettings = new EventStoreClientConnectivitySettings
148-
{
149-
Address =
150-
new Uri(Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionString"))
151-
};
152-
settings.DefaultCredentials = new UserCredentials(Startup.Configuration.GetValue<String>("EventStoreSettings:UserName"),
153-
Startup.Configuration.GetValue<String>("EventStoreSettings:Password"));
154-
});
117+
services.AddEventStoreClient(Startup.ConfigureEventStoreSettings);
118+
services.AddEventStoreProjectionManagerClient(Startup.ConfigureEventStoreSettings);
155119
}
156120

157121
services.AddTransient<IEventStoreContext, EventStoreContext>();
@@ -206,10 +170,55 @@ public void ConfigureServices(IServiceCollection services)
206170
services.AddSingleton<IFeeCalculationManager, FeeCalculationManager>();
207171
}
208172

209-
173+
private static EventStoreClientSettings EventStoreClientSettings;
174+
175+
private static void ConfigureEventStoreSettings(EventStoreClientSettings settings = null)
176+
{
177+
if (settings == null)
178+
{
179+
settings = new EventStoreClientSettings();
180+
}
181+
182+
settings.CreateHttpMessageHandler = () => new SocketsHttpHandler
183+
{
184+
SslOptions =
185+
{
186+
RemoteCertificateValidationCallback = (sender,
187+
certificate,
188+
chain,
189+
errors) => true,
190+
}
191+
};
192+
settings.ConnectionName = Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionName");
193+
settings.ConnectivitySettings = new EventStoreClientConnectivitySettings
194+
{
195+
Address = new Uri(Startup.Configuration.GetValue<String>("EventStoreSettings:ConnectionString")),
196+
};
197+
198+
settings.DefaultCredentials = new UserCredentials(Startup.Configuration.GetValue<String>("EventStoreSettings:UserName"),
199+
Startup.Configuration.GetValue<String>("EventStoreSettings:Password"));
200+
Startup.EventStoreClientSettings = settings;
201+
}
210202

211203
private void ConfigureMiddlewareServices(IServiceCollection services)
212204
{
205+
services.AddHealthChecks()
206+
.AddEventStore(Startup.EventStoreClientSettings,
207+
userCredentials: Startup.EventStoreClientSettings.DefaultCredentials,
208+
name: "Eventstore",
209+
failureStatus: HealthStatus.Unhealthy,
210+
tags: new string[] { "db", "eventstore" })
211+
.AddUrlGroup(new Uri($"{ConfigurationReader.GetValue("SecurityConfiguration", "Authority")}/.well-known/openid-configuration"),
212+
name: "Security Service",
213+
httpMethod: HttpMethod.Get,
214+
failureStatus: HealthStatus.Unhealthy,
215+
tags: new string[] { "security", "authorisation" })
216+
.AddUrlGroup(new Uri($"{ConfigurationReader.GetValue("AppSettings", "EstateManagementApi")}/.well-known/openid-configuration"),
217+
name: "Estate Management Service",
218+
httpMethod: HttpMethod.Get,
219+
failureStatus: HealthStatus.Unhealthy,
220+
tags: new string[] { "application", "estatemanagement" });
221+
213222
services.AddApiVersioning(
214223
options =>
215224
{
@@ -299,8 +308,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
299308

300309
Logger.Initialise(logger);
301310

302-
ConfigurationReader.Initialise(Startup.Configuration);
303-
304311
app.AddRequestLogging();
305312
app.AddResponseLogging();
306313
app.AddExceptionHandler();
@@ -313,6 +320,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
313320
app.UseEndpoints(endpoints =>
314321
{
315322
endpoints.MapControllers();
323+
endpoints.MapHealthChecks("health", new HealthCheckOptions()
324+
{
325+
Predicate = _ => true,
326+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
327+
});
316328
});
317329

318330
app.UseSwagger();
@@ -326,17 +338,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
326338
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
327339
}
328340
});
329-
330-
//if (String.Compare(ConfigurationReader.GetValue("EventStoreSettings", "START_PROJECTIONS"),
331-
// Boolean.TrueString,
332-
// StringComparison.InvariantCultureIgnoreCase) == 0)
333-
//{
334-
// app.PreWarm(true).Wait();
335-
//}
336-
//else
337-
//{
338-
// app.PreWarm();
339-
//}
340341
}
341342
}
342343
}

TransactionProcessor/TransactionProcessor.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
9+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="3.1.1" />
10+
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="3.1.3" />
11+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.1.2" />
12+
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="3.1.2" />
13+
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="3.1.2" />
14+
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
1015
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
1116
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
1217
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
@@ -19,7 +24,8 @@
1924
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
2025
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
2126
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.4" />
22-
<PackageReference Include="Shared" Version="0.0.15.7" />
27+
<PackageReference Include="Shared" Version="1.0.1" />
28+
<PackageReference Include="Shared.EventStore" Version="1.0.1" />
2329
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
2430
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
2531
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="5.1.2" />

0 commit comments

Comments
 (0)