Add monitoring on queries with sentry alert + Fix check position list in db for backtest

This commit is contained in:
2025-10-10 00:15:02 +07:00
parent ffb98fe359
commit e4c2f8b7a5
24 changed files with 3340 additions and 179 deletions

View File

@@ -89,8 +89,15 @@ builder.Services.AddHttpClient("GmxHealthCheck")
builder.Services.AddSingleton<Web3ProxyHealthCheck>(sp =>
new Web3ProxyHealthCheck(sp.GetRequiredService<IHttpClientFactory>(), web3ProxyUrl));
// Add SQL Loop Detection Service with Sentry integration
// Configure SQL monitoring settings
builder.Services.Configure<SqlMonitoringSettings>(builder.Configuration.GetSection("SqlMonitoring"));
// Register SQL monitoring services
builder.Services.AddSingleton<SentrySqlMonitoringService>();
// Add PostgreSQL DbContext with improved concurrency and connection management
builder.Services.AddDbContext<ManagingDbContext>(options =>
builder.Services.AddDbContext<ManagingDbContext>((serviceProvider, options) =>
{
options.UseNpgsql(postgreSqlConnectionString, npgsqlOptions =>
{
@@ -114,8 +121,22 @@ builder.Services.AddDbContext<ManagingDbContext>(options =>
// Enable service provider caching for better performance
options.EnableServiceProviderCaching();
// Enable connection resiliency for backtest and high-load scenarios
options.LogTo(msg => Console.WriteLine(msg), LogLevel.Warning); // Log warnings for connection issues
// Enable comprehensive SQL query logging for monitoring and debugging
var logger = serviceProvider.GetRequiredService<ILogger<ManagingDbContext>>();
var sentryMonitoringService = serviceProvider.GetRequiredService<SentrySqlMonitoringService>();
options.LogTo(msg =>
{
// Log SQL queries with enhanced formatting
if (msg.Contains("Executed DbCommand") || msg.Contains("Executing DbCommand"))
{
Console.WriteLine($"[EF-SQL] {msg}");
}
else if (msg.Contains("Warning") || msg.Contains("Error"))
{
Console.WriteLine($"[EF-WARNING] {msg}");
}
}, LogLevel.Information); // Log all SQL operations for monitoring
}, ServiceLifetime.Scoped); // Explicitly specify scoped lifetime for proper request isolation
// Add specific health checks for databases and other services