-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on more health checks functions (#681)
* Working on more health checks functions * Additionals changes * Changes are changed * Changes are made * additional white space fixes * more white space fixes * white space fixes * whitespace fixes * one more * add checks to ensure we only run the test when we have the setting set * use other strings for queries * Address concerns from Gilian * address concern Gilian * Did some code cleanup. --------- Co-authored-by: Mike van Mourik <[email protected]> Co-authored-by: Gilian <[email protected]>
- Loading branch information
1 parent
4644748
commit 545eca0
Showing
5 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,7 @@ | ||
namespace GeeksCoreLibrary.Core.Models; | ||
|
||
public class HealthChecksSettings | ||
{ | ||
public int MaximumDatabaseConnections { get; set; } | ||
public int MaximumConnectionsInTime { get; set; } | ||
} |
70 changes: 70 additions & 0 deletions
70
GeeksCoreLibrary/Modules/HealthChecks/Services/DatabaseHealthService.cs
This file contains 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,70 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GeeksCoreLibrary.Core.Models; | ||
using GeeksCoreLibrary.Modules.Databases.Interfaces; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace GeeksCoreLibrary.Modules.HealthChecks.Services; | ||
public class DatabaseHealthService : IHealthCheck | ||
{ | ||
private readonly IDatabaseConnection databaseConnection; | ||
private readonly HealthChecksSettings healthChecksSettings; | ||
|
||
public DatabaseHealthService(IDatabaseConnection databaseConnection, IOptions<HealthChecksSettings> healthChecksSettings) | ||
{ | ||
this.databaseConnection = databaseConnection; | ||
this.healthChecksSettings = healthChecksSettings.Value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
// A new query to check the max active connections from the DatabaseHealthCheck. | ||
var query = "SELECT COUNT(*) AS active_connections FROM information_schema.PROCESSLIST;"; | ||
var datatable = await databaseConnection.GetAsync(query); | ||
|
||
var healthCheckConnections = healthChecksSettings.MaximumDatabaseConnections; | ||
var healthCheckConnectionsTime = healthChecksSettings.MaximumConnectionsInTime; | ||
|
||
// If no value is set, we are skipping this test. | ||
if (healthCheckConnections > 0) | ||
{ | ||
// Retrieve the count of active connections. | ||
var activeConnections = Convert.ToInt32(datatable.Rows[0]["active_connections"]); | ||
|
||
// Check if the number of active connections exceeds the limit. | ||
if (activeConnections > healthCheckConnections) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Too many database connections. There are currently {activeConnections} connections active, which is more than the threshold {healthCheckConnections} that is set in the appSettings."); | ||
} | ||
} | ||
|
||
// If no value is set, we are skipping this test. | ||
if (healthCheckConnectionsTime > 0) | ||
{ | ||
// Query to check the max open connections in time from the DatabaseHealthCheck. | ||
query = "SELECT ID, USER, HOST, TIME AS connection_time_in_sec FROM information_schema.PROCESSLIST WHERE USER != 'repluser' AND HOST != 'localhost' ORDER BY connection_time_in_sec DESC"; | ||
datatable = await databaseConnection.GetAsync(query); | ||
if (datatable.Rows.Count == 0) | ||
{ | ||
// If the query returns no results, it means that there are no open connections. | ||
// But we can connect to the database, otherwise we would get an exception. So this means the database is healthy. | ||
return HealthCheckResult.Healthy(); | ||
} | ||
|
||
// Retrieve the count of open connections in time. | ||
var connectionTime = Convert.ToInt32(datatable.Rows[0]["connection_time_in_sec"]); | ||
|
||
// Check if the time from open connections exceeds the limit. | ||
if (connectionTime > healthCheckConnectionsTime) | ||
{ | ||
return HealthCheckResult.Unhealthy($"There is at least one opened connection that has been open for more than {healthCheckConnectionsTime} seconds. There most likely is an issue with this connection."); | ||
} | ||
} | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
} |