Skip to content

Commit

Permalink
Add health checks, close #37
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 2, 2021
1 parent a8d9e31 commit c82864a
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 9 deletions.
16 changes: 15 additions & 1 deletion Tzkt.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand All @@ -24,6 +24,20 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((host, appConfig) =>
{
appConfig.Sources.Clear();
appConfig.AddJsonFile("appsettings.json", true);
appConfig.AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", true);
appConfig.AddEnvironmentVariables("TZKT_");
appConfig.AddEnvironmentVariables("TZKT_API_");
appConfig.AddCommandLine(args);
})
.ConfigureLogging(logConfig =>
{
logConfig.ClearProviders();
logConfig.AddConsole();
});
}

Expand Down
14 changes: 14 additions & 0 deletions Tzkt.Api/Services/Health/DumbHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Tzkt.Api.Services
{
class DumbHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = default)
{
return Task.FromResult(HealthCheckResult.Healthy());
}
}
}
18 changes: 18 additions & 0 deletions Tzkt.Api/Services/Health/HealthChecksConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.Configuration;

namespace Tzkt.Api.Services
{
class HealthChecksConfig
{
public bool Enabled { get; set; } = false;
public string Endpoint { get; set; } = "/health";
}

static class HealthChecksConfigExt
{
public static HealthChecksConfig GetHealthChecksConfig(this IConfiguration config)
{
return config.GetSection("HealthChecks")?.Get<HealthChecksConfig>() ?? new();
}
}
}
20 changes: 20 additions & 0 deletions Tzkt.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public void ConfigureServices(IServiceCollection services)
}
#endregion

#region health checks
var healthChecks = Configuration.GetHealthChecksConfig();
if (healthChecks.Enabled)
{
services.AddHealthChecks()
.AddCheck<DumbHealthCheck>(nameof(DumbHealthCheck));
}
#endregion

#region dapper
SqlMapper.AddTypeHandler(new AccountMetadataTypeHandler());
SqlMapper.AddTypeHandler(new JsonElementTypeHandler());
Expand Down Expand Up @@ -142,10 +151,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();

#region web socket
if (Configuration.GetWebsocketConfig().Enabled)
{
endpoints.MapHub<DefaultHub>("/v1/events");
}
#endregion

#region health checks
var healthChecks = Configuration.GetHealthChecksConfig();
if (healthChecks.Enabled)
{
endpoints.MapHealthChecks(healthChecks.Endpoint);
}
#endregion
});
}
}
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Api/Websocket/WebsocketConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public static WebsocketConfig GetWebsocketConfig(this IConfiguration config)
return config.GetSection("Websocket")?.Get<WebsocketConfig>() ?? new WebsocketConfig();
}
}

}
10 changes: 10 additions & 0 deletions Tzkt.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{
"HealthChecks": {
"Enabled": false
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"FormatterName": "simple",
"FormatterOptions": {
"TimestampFormat": "HH:mm:ss.fff ",
"UseUtcTimestamp": false
}
}
}
}
11 changes: 11 additions & 0 deletions Tzkt.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
"Enabled": false,
"UpdatePeriod": 30
},
"HealthChecks": {
"Enabled": true,
"Endpoint": "/health"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Console": {
"FormatterName": "systemd", // simple | systemd | json
"FormatterOptions": {
//"TimestampFormat": "HH:mm:ss ",
"UseUtcTimestamp": true
}
}
},
"AllowedHosts": "*"
Expand Down
34 changes: 27 additions & 7 deletions Tzkt.Sync/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

Expand All @@ -22,13 +22,19 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, config) =>
.ConfigureAppConfiguration((host, appConfig) =>
{
config.AddEnvironmentVariables("TZKT_");
appConfig.Sources.Clear();
appConfig.AddJsonFile("appsettings.json", true);
appConfig.AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", true);
appConfig.AddEnvironmentVariables("TZKT_");
appConfig.AddEnvironmentVariables("TZKT_SYNC_");
appConfig.AddCommandLine(args);
})
.ConfigureHostConfiguration(configHost =>
.ConfigureLogging(logConfig =>
{
configHost.AddEnvironmentVariables(prefix: "TZKT_");
logConfig.ClearProviders();
logConfig.AddConsole();
})
.ConfigureServices((hostContext, services) =>
{
Expand All @@ -38,10 +44,24 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddCaches();
services.AddTezosNode();
services.AddTezosProtocols();

services.AddQuotes(hostContext.Configuration);

services.AddHostedService<Observer>();

#region healh checks
var healthChecks = hostContext.Configuration.GetHealthChecksConfig();
if (healthChecks.Enabled)
{
services.AddHealthChecks()
.AddCheck<DumbHealthCheck>(nameof(DumbHealthCheck));

services.AddSingleton<IHealthCheckPublisher, HealthCheckPublisher>();
services.Configure<HealthCheckPublisherOptions>(config =>
{
config.Delay = TimeSpan.FromSeconds(healthChecks.Delay);
config.Period = TimeSpan.FromSeconds(healthChecks.Period);
});
}
#endregion
});
}

Expand Down
14 changes: 14 additions & 0 deletions Tzkt.Sync/Services/Health/DumbHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Tzkt.Sync.Services
{
class DumbHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken ct = default)
{
return Task.FromResult(HealthCheckResult.Healthy());
}
}
}
32 changes: 32 additions & 0 deletions Tzkt.Sync/Services/Health/HealthCheckPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Tzkt.Sync.Services
{
class HealthCheckPublisher : IHealthCheckPublisher
{
readonly string FilePath;

public HealthCheckPublisher(IConfiguration config)
{
FilePath = config.GetHealthChecksConfig().FilePath
?? Path.GetTempFileName();
}

public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
{
if (report.Status == HealthStatus.Healthy)
{
using var _ = File.Create(FilePath);
}
else if (File.Exists(FilePath))
{
File.Delete(FilePath);
}
return Task.CompletedTask;
}
}
}
20 changes: 20 additions & 0 deletions Tzkt.Sync/Services/Health/HealthChecksConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Configuration;

namespace Tzkt.Sync.Services
{
class HealthChecksConfig
{
public bool Enabled { get; set; } = false;
public int Delay { get; set; } = 10;
public int Period { get; set; } = 10;
public string FilePath { get; set; } = "sync.health";
}

static class HealthChecksConfigExt
{
public static HealthChecksConfig GetHealthChecksConfig(this IConfiguration config)
{
return config.GetSection("HealthChecks")?.Get<HealthChecksConfig>() ?? new();
}
}
}
1 change: 1 addition & 0 deletions Tzkt.Sync/Tzkt.Sync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="5.0.8" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Netezos" Version="2.3.13" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
Expand Down
10 changes: 10 additions & 0 deletions Tzkt.Sync/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{
"HealthChecks": {
"Enabled": false
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"Console": {
"FormatterName": "simple",
"FormatterOptions": {
"TimestampFormat": "HH:mm:ss.fff ",
"UseUtcTimestamp": false
}
}
}
}
13 changes: 13 additions & 0 deletions Tzkt.Sync/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@
"ConnectionStrings": {
"DefaultConnection": "host=db;port=5432;database=tzkt_db;username=tzkt;password=qwerty;"
},
"HealthChecks": {
"Enabled": true,
"Delay": 10,
"Period": 10,
"FilePath": "sync.health"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Console": {
"FormatterName": "systemd", // simple | systemd | json
"FormatterOptions": {
//"TimestampFormat": "HH:mm:ss ",
"UseUtcTimestamp": true
}
}
}
}

0 comments on commit c82864a

Please sign in to comment.