Skip to content

Commit fd6ea13

Browse files
fix: put MongoDB to relationalProviders list
fix: wrong logic of healthcheck support multi-db
1 parent 6933152 commit fd6ea13

9 files changed

Lines changed: 79 additions & 89 deletions

File tree

src/Api/Api.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
32
<ItemGroup>
43
<ProjectReference Include="..\Application\Application.csproj" />
54
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
@@ -21,7 +20,10 @@
2120
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
2221
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
2322
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
24-
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.12.0-beta.1" />
23+
<PackageReference
24+
Include="OpenTelemetry.Instrumentation.EntityFrameworkCore"
25+
Version="1.12.0-beta.1"
26+
/>
2527
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.1" />
2628
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
2729
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
@@ -37,5 +39,4 @@
3739
<NoWarn>$(NoWarn);1591</NoWarn>
3840
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
3941
</PropertyGroup>
40-
4142
</Project>

src/Api/Extensions/HealthCheckExtension.cs

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Api.Settings;
2-
using Infrastructure.Constants;
32
using Infrastructure.Data;
43
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
54
using Microsoft.Extensions.Diagnostics.HealthChecks;
@@ -21,56 +20,43 @@ IConfiguration configuration
2120
HealthCheckSettings healthCheckSettings =
2221
configuration.GetSection(nameof(HealthCheckSettings)).Get<HealthCheckSettings>()
2322
?? new();
24-
services
25-
.AddHealthChecks()
26-
.AddNpgSql(
27-
provider =>
28-
{
29-
var databaseSetting = provider
30-
.GetRequiredService<IOptions<DatabaseSettings>>()
31-
.Value;
32-
CurrentProvider currentProvider = databaseSetting.Provider;
23+
IHealthChecksBuilder builder = services.AddHealthChecks();
3324

34-
if (
35-
DatabaseConfiguration.relationalProviders.Contains(
36-
currentProvider.ToString()
37-
)
38-
)
39-
{
40-
return currentProvider switch
41-
{
42-
CurrentProvider.PostgreSQL => databaseSetting
43-
.Relational!
44-
.PostgreSQL!
45-
.ConnectionString,
46-
_ => throw new NotSupportedException(
47-
$"Database provider {currentProvider} is not supported."
48-
),
49-
};
50-
}
51-
return currentProvider switch
25+
CurrentProvider? provider = configuration
26+
.GetSection($"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.Provider)}")
27+
.Get<CurrentProvider>();
28+
29+
switch (provider)
30+
{
31+
case CurrentProvider.PostgreSQL:
32+
builder.AddNpgSql(
33+
(sp) =>
5234
{
53-
CurrentProvider.PostgreSQL => databaseSetting
54-
.NonRelational!
55-
.MongoDB!
56-
.ConnectionString,
57-
_ => throw new NotSupportedException(
58-
$"Database provider {currentProvider} is not supported."
59-
),
60-
};
61-
},
62-
name: "postgres",
63-
failureStatus: HealthStatus.Unhealthy,
64-
tags: ["db", "postgres"],
65-
timeout: TimeSpan.FromSeconds(healthCheckSettings.TimeoutSeconds)
66-
);
35+
var databaseSetting = sp.GetRequiredService<
36+
IOptions<DatabaseSettings>
37+
>().Value;
38+
return databaseSetting.Relational!.PostgreSQL!.ConnectionString;
39+
},
40+
name: "postgres",
41+
failureStatus: HealthStatus.Unhealthy,
42+
tags: ["db", "postgres"],
43+
timeout: TimeSpan.FromSeconds(healthCheckSettings.TimeoutSeconds)
44+
);
45+
break;
46+
47+
case CurrentProvider.MongoDB:
48+
//
49+
break;
50+
default:
51+
throw new NotSupportedException($"Database provider {provider} is not supported.");
52+
}
6753
}
6854

69-
public static void UseHealthCheck(this WebApplication app, IConfiguration configuration)
55+
public static void UseHealthCheck(this WebApplication app)
7056
{
71-
HealthCheckSettings healthCheckSettings =
72-
configuration.GetSection(nameof(HealthCheckSettings)).Get<HealthCheckSettings>()
73-
?? new();
57+
HealthCheckSettings healthCheckSettings = app
58+
.Services.GetRequiredService<IOptions<HealthCheckSettings>>()
59+
.Value;
7460
app.MapHealthChecks(
7561
healthCheckSettings.Path,
7662
new HealthCheckOptions
@@ -84,13 +70,13 @@ public static void UseHealthCheck(this WebApplication app, IConfiguration config
8470
{
8571
status = report.Status.ToString(),
8672
totalDuration = report.TotalDuration.ToString(),
87-
entries = report.Entries.Select(x => new
73+
entries = report.Entries.Select(entry => new
8874
{
89-
key = x.Key,
90-
status = x.Value.Status.ToString(),
91-
description = x.Value.Description,
92-
duration = x.Value.Duration.ToString(),
93-
data = x.Value.Data,
75+
name = entry.Key,
76+
status = entry.Value.Status.ToString(),
77+
description = entry.Value.Description,
78+
duration = entry.Value.Duration.ToString(),
79+
data = entry.Value.Data,
9480
}),
9581
};
9682

src/Api/Extensions/OpenTelemetryExtensions.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,17 @@ IConfiguration configuration
7070
})
7171
.AddEntityFrameworkCoreInstrumentation(opt =>
7272
{
73-
opt.SetDbStatementForText = true;
74-
opt.SetDbStatementForStoredProcedure = true;
7573
opt.EnrichWithIDbCommand = (activity, command) =>
7674
{
7775
if (activity == null)
7876
{
7977
return;
8078
}
81-
// Extract SQL operation name: SELECT / INSERT / UPDATE / DELETE / EXEC...
82-
string operation =
83-
command.CommandType == CommandType.StoredProcedure
84-
? "EXEC"
85-
: command.CommandText.Split(
86-
' ',
87-
StringSplitOptions.RemoveEmptyEntries
88-
)[0];
89-
90-
activity.SetTag("db.system", command.Connection?.GetType().Name);
91-
activity.SetTag("db.name", command.Connection?.Database);
92-
activity.SetTag("db.operation", operation);
93-
activity.SetTag("db.command_type", command.CommandType.ToString());
79+
var dbName = command.Connection?.Database;
80+
if (!string.IsNullOrEmpty(dbName))
81+
{
82+
activity.SetTag("db.name", dbName);
83+
}
9484
};
9585
})
9686
.AddHttpClientInstrumentation();

src/Api/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@
5252
services.AddHttpContextAccessor();
5353
services.AddScoped<IRequestContextProvider, RequestContextProvider>();
5454

55-
// I set it Singleton because it's called inside many singleton services, but if u want, set it Scoped for the standard.
55+
// I set it Singleton because it's called inside many singleton services.
5656
services.AddSingleton<ICurrentUser, CurrentUser>();
57+
5758
services.AddCors(options =>
5859
options.AddPolicy(
5960
allowedCors.Name,
@@ -67,7 +68,6 @@
6768
}
6869
)
6970
);
70-
7171
services.AddCors(options =>
7272
options.AddPolicy(
7373
"allowedDev",
@@ -128,7 +128,7 @@
128128
});
129129
}
130130

131-
app.UseHealthCheck(configuration);
131+
app.UseHealthCheck();
132132
if (isDevelopment)
133133
{
134134
app.UseCors("allowedDev");

src/Api/appsettings.example.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"SecuritySettings": {
6161
"JwtSettings": {
6262
"Default": {
63+
"Name": "Default",
6364
"SecretKey": "3YDPg1E0pXAC9kD1iMtbciNj24gpDAxngqv8r6LRczWGx",
6465
"AccessTokenExpirationInMinutes": 1440,
6566
"RefreshTokenExpirationInDays": 7
@@ -113,7 +114,8 @@
113114
"IsEnabled": false,
114115
"DefaultIndex": "default_index",
115116
"Password": "your_password",
116-
"Username": "your_username"
117+
"Username": "your_username",
118+
"PrefixIndex": "TheTemplate"
117119
},
118120
"DatabaseSettings": {
119121
"Provider": "PostgreSQL",

src/Infrastructure/Constants/DatabaseConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class DatabaseConfiguration
77
"PostgreSQL",
88
"MySQL",
99
"SQLServer",
10-
"MongoDB",
10+
"Oracle",
1111
];
1212
}

src/Infrastructure/Data/DatabaseExtension.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Application.Common.Interfaces.UnitOfWorks;
2-
using Infrastructure.common.validator;
32
using Infrastructure.Data.Interceptors;
43
using Microsoft.EntityFrameworkCore;
5-
using Microsoft.Extensions.Configuration;
64
using Microsoft.Extensions.DependencyInjection;
75
using Microsoft.Extensions.Options;
86
using Npgsql;
@@ -11,19 +9,11 @@ namespace Infrastructure.Data;
119

1210
public static class DatabaseExtension
1311
{
14-
public static IServiceCollection AddRelationalDatabase(
12+
public static IServiceCollection AddEfCoreRelationalDatabase(
1513
this IServiceCollection services,
16-
IConfiguration configuration
14+
CurrentProvider provider
1715
)
1816
{
19-
services.AddOptionsWithFluentValidation<DatabaseSettings>(
20-
configuration.GetSection(nameof(DatabaseSettings))
21-
);
22-
23-
CurrentProvider? provider = configuration
24-
.GetSection($"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.Provider)}")
25-
.Get<CurrentProvider>();
26-
2717
if (provider == CurrentProvider.PostgreSQL)
2818
{
2919
services.AddSingleton(sp =>

src/Infrastructure/Data/DatabaseSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public enum CurrentProvider
1212
PostgreSQL = 1,
1313
MySQL = 2,
1414
SQLServer = 3,
15-
MongoDB = 4,
15+
Oracle = 4,
16+
MongoDB = 5,
1617
}
1718

1819
public class RelationalProvider

src/Infrastructure/DependencyInjection.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Reflection;
22
using FluentValidation;
3+
using Infrastructure.common.validator;
4+
using Infrastructure.Constants;
35
using Infrastructure.Data;
46
using Infrastructure.Data.Repositories;
57
using Infrastructure.Data.Seeders;
@@ -30,8 +32,26 @@ IConfiguration configuration
3032
ValidatorOptions.Global.DefaultRuleLevelCascadeMode = CascadeMode.Stop;
3133
ValidatorOptions.Global.DefaultClassLevelCascadeMode = CascadeMode.Stop;
3234
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
35+
services.AddOptionsWithFluentValidation<DatabaseSettings>(
36+
configuration.GetSection(nameof(DatabaseSettings))
37+
);
3338

34-
services.AddRelationalDatabase(configuration);
39+
CurrentProvider? provider = configuration
40+
.GetSection($"{nameof(DatabaseSettings)}:{nameof(DatabaseSettings.Provider)}")
41+
.Get<CurrentProvider>();
42+
43+
if (
44+
provider != null
45+
&& DatabaseConfiguration.relationalProviders.Contains(provider.ToString()!)
46+
)
47+
{
48+
// EFCore register
49+
services.AddEfCoreRelationalDatabase(provider.Value);
50+
}
51+
else
52+
{
53+
// non-relational db register
54+
}
3555

3656
// queue register
3757
services.AddQueue(configuration);

0 commit comments

Comments
 (0)