fix: better logging
- verbose logging for log channel resolving logic - don't LOG TOKENS TO THE CONSOLE OR SEQ - actually log verbose logs to seq - report open db connections to prometheus
This commit is contained in:
parent
7749c9d9e2
commit
5157105c35
7 changed files with 109 additions and 50 deletions
|
|
@ -20,14 +20,9 @@ using Npgsql;
|
|||
|
||||
namespace Catalogger.Backend.Database;
|
||||
|
||||
public class DatabaseConnection(Guid id, ILogger logger, NpgsqlConnection inner)
|
||||
: DbConnection,
|
||||
IDisposable
|
||||
public class DatabaseConnection(NpgsqlConnection inner) : DbConnection, IDisposable
|
||||
{
|
||||
public Guid ConnectionId => id;
|
||||
public NpgsqlConnection Inner => inner;
|
||||
private readonly ILogger _logger = logger.ForContext<DatabaseConnection>();
|
||||
private readonly DateTimeOffset _openTime = DateTimeOffset.UtcNow;
|
||||
|
||||
private bool _hasClosed;
|
||||
|
||||
|
|
@ -43,8 +38,6 @@ public class DatabaseConnection(Guid id, ILogger logger, NpgsqlConnection inner)
|
|||
}
|
||||
|
||||
DatabasePool.DecrementConnections();
|
||||
var openFor = DateTimeOffset.UtcNow - _openTime;
|
||||
_logger.Verbose("Closing connection {ConnId}, open for {OpenFor}", ConnectionId, openFor);
|
||||
_hasClosed = true;
|
||||
await inner.CloseAsync();
|
||||
}
|
||||
|
|
@ -52,11 +45,7 @@ public class DatabaseConnection(Guid id, ILogger logger, NpgsqlConnection inner)
|
|||
protected override async ValueTask<DbTransaction> BeginDbTransactionAsync(
|
||||
IsolationLevel isolationLevel,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
_logger.Verbose("Beginning transaction on connection {ConnId}", ConnectionId);
|
||||
return await inner.BeginTransactionAsync(isolationLevel, cancellationToken);
|
||||
}
|
||||
) => await inner.BeginTransactionAsync(isolationLevel, cancellationToken);
|
||||
|
||||
public new void Dispose()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -24,18 +24,13 @@ namespace Catalogger.Backend.Database;
|
|||
|
||||
public class DatabasePool
|
||||
{
|
||||
private readonly ILogger _rootLogger;
|
||||
private readonly ILogger _logger;
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
|
||||
private static int _openConnections;
|
||||
public static int OpenConnections => _openConnections;
|
||||
|
||||
public DatabasePool(Config config, ILogger logger, ILoggerFactory? loggerFactory)
|
||||
public DatabasePool(Config config, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
_rootLogger = logger;
|
||||
_logger = logger.ForContext<DatabasePool>();
|
||||
|
||||
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
||||
{
|
||||
Timeout = config.Database.Timeout ?? 5,
|
||||
|
|
@ -49,27 +44,10 @@ public class DatabasePool
|
|||
_dataSource = dataSourceBuilder.Build();
|
||||
}
|
||||
|
||||
public async Task<DatabaseConnection> AcquireAsync(CancellationToken ct = default)
|
||||
{
|
||||
return new DatabaseConnection(
|
||||
LogOpen(),
|
||||
_rootLogger,
|
||||
await _dataSource.OpenConnectionAsync(ct)
|
||||
);
|
||||
}
|
||||
public async Task<DatabaseConnection> AcquireAsync(CancellationToken ct = default) =>
|
||||
new(await _dataSource.OpenConnectionAsync(ct));
|
||||
|
||||
public DatabaseConnection Acquire()
|
||||
{
|
||||
return new DatabaseConnection(LogOpen(), _rootLogger, _dataSource.OpenConnection());
|
||||
}
|
||||
|
||||
private Guid LogOpen()
|
||||
{
|
||||
var connId = Guid.NewGuid();
|
||||
_logger.Verbose("Opening database connection {ConnId}", connId);
|
||||
IncrementConnections();
|
||||
return connId;
|
||||
}
|
||||
public DatabaseConnection Acquire() => new(_dataSource.OpenConnection());
|
||||
|
||||
public async Task ExecuteAsync(
|
||||
Func<DatabaseConnection, Task> func,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue