Skip to content

Commit df7dc3d

Browse files
net0wellclaude
andcommitted
fix: comandos SQL da biblioteca registrados em Debug (spec 005)
Os hosts gravavam ~4 linhas de log por segundo, quase todas do EF Core registrando em Information a busca do relay no outbox, que roda a cada 250 ms (D-58). Em poucos minutos o console do Aspire passava de 2000 registros. - O PsrDbContext registra os comandos executados em Debug; comandos com falha continuam como erro. Vale para qualquer aplicação que use o pacote, não só os hosts (D-187). - Nos hosts, Microsoft.AspNetCore vai para Warning: os traces já cobrem as requisições, e Serilog:MinimumLevel:Override muda isso. - Teste: os comandos executados chegam ao logger em Debug. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b6c7f79 commit df7dc3d

6 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ RunHandle run = await queue.EnqueuePipelineAsync("progression-calc", new { a1 =
166166
- **External systems:** the public `StartPipelineRun` command (exchange `psr.public`) starts pipelines. The `run.*` and
167167
`step.*` events (exchange `psr.events`) report progress and outputs. The contracts are in `schemas/messages`.
168168
- **Observability:** `ActivitySource` and `Meter` named `PythonScriptRunner`; the `traceparent` follows the run all the
169-
way to the worker.
169+
way to the worker. The relay polls the outbox every 250 ms, so the library logs its SQL commands at `Debug`
170+
(`Microsoft.EntityFrameworkCore.Database.Command`).
170171

171172
`hosts/PythonScriptRunner.WorkerHost` is a ready-made worker. It provides `/health/live`, `/health/ready`, JSON logs and
172173
OTLP, and is configured with environment variables:
@@ -330,6 +331,7 @@ docker run -d --read-only \
330331
| `Oidc__Authority`, `Oidc__ClientId`, `Oidc__ClientSecret` | dashboard | optional: turns on OpenID Connect |
331332
| `Dashboard__PathPrefix` | dashboard | `/psr` |
332333
| `OTEL_EXPORTER_OTLP_ENDPOINT` | both | `http://otel-collector:4317` |
334+
| `Serilog__MinimumLevel__Override__<category>` | both | `Information` for `Microsoft.AspNetCore` brings back the request logs (default `Warning`) |
333335

334336
### Volumes and read-only file system
335337

‎hosts/PythonScriptRunner.ServiceDefaults/ServiceDefaultsExtensions.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OpenTelemetry.Resources;
88
using OpenTelemetry.Trace;
99
using Serilog;
10+
using Serilog.Events;
1011
using Serilog.Formatting.Compact;
1112

1213
namespace Microsoft.Extensions.Hosting;
@@ -31,7 +32,10 @@ public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder, strin
3132
where TBuilder : IHostApplicationBuilder
3233
{
3334
ArgumentNullException.ThrowIfNull(builder);
35+
// ASP.NET Core logs every request at Information; the traces already cover them (D-187). The configuration
36+
// (Serilog:MinimumLevel:Override:Microsoft.AspNetCore) can bring them back.
3437
builder.Services.AddSerilog((services, logger) => logger
38+
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
3539
.ReadFrom.Configuration(builder.Configuration)
3640
.Enrich.FromLogContext()
3741
.Enrich.WithProperty("service", serviceName)

‎src/PythonScriptRunner.EntityFrameworkCore/PsrDbContextOptions.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.EntityFrameworkCore.Diagnostics;
2+
using Microsoft.Extensions.Logging;
13
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
24

35
namespace PythonScriptRunner.EntityFrameworkCore;
@@ -7,4 +9,12 @@ internal static class PsrDbContextOptions
79
{
810
public static void ConfigureNpgsql(NpgsqlDbContextOptionsBuilder npgsql) =>
911
npgsql.MigrationsHistoryTable(PsrDbContext.MigrationsHistoryTable, PsrDbContext.Schema);
12+
13+
/// <summary>
14+
/// The outbox relay polls every 250 ms and the worker every few seconds (D-58), so each executed command is logged at
15+
/// <c>Debug</c> instead of <c>Information</c>: otherwise the host's log fills with the same queries (D-187). Failed
16+
/// commands are still logged as errors.
17+
/// </summary>
18+
public static void ConfigureWarnings(WarningsConfigurationBuilder warnings) =>
19+
warnings.Log((RelationalEventId.CommandExecuted, LogLevel.Debug));
1020
}

‎src/PythonScriptRunner.EntityFrameworkCore/PsrEntityFrameworkCoreExtensions.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public static IPythonScriptRunnerBuilder AddEntityFrameworkCore(this IPythonScri
3333
var services = builder.Services;
3434
services.AddSingleton(options);
3535
services.TryAddSingleton<PsrDataSource>();
36-
services.AddPooledDbContextFactory<PsrDbContext>((provider, db) =>
37-
db.UseNpgsql(provider.GetRequiredService<PsrDataSource>().DataSource, PsrDbContextOptions.ConfigureNpgsql));
36+
services.AddPooledDbContextFactory<PsrDbContext>((provider, db) => db
37+
.UseNpgsql(provider.GetRequiredService<PsrDataSource>().DataSource, PsrDbContextOptions.ConfigureNpgsql)
38+
.ConfigureWarnings(PsrDbContextOptions.ConfigureWarnings));
3839

3940
services.TryAddSingleton<EfCatalog>();
4041
services.Replace(ServiceDescriptor.Singleton<ICatalog>(provider => provider.GetRequiredService<EfCatalog>()));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Diagnostics;
3+
using Microsoft.Extensions.Logging;
4+
using PythonScriptRunner.EntityFrameworkCore.Tests.Infrastructure;
5+
6+
namespace PythonScriptRunner.EntityFrameworkCore.Tests;
7+
8+
public sealed class LoggingTests(PostgresFixture postgres, PythonFixture python)
9+
{
10+
/// <summary>
11+
/// The relay and the worker poll the database several times per second (D-58); with the executed commands at
12+
/// Information, the hosts logged ~4 lines per second of the same query (D-187).
13+
/// </summary>
14+
[Fact]
15+
public async Task ExecutedSqlCommands_AreLoggedAtDebug()
16+
{
17+
await using var host = await PsrTestHost.CreateAsync(postgres, python, services: s => s
18+
.AddLogging(logging => logging.SetMinimumLevel(LogLevel.Debug))
19+
.AddFakeLogging());
20+
21+
(await host.Admin.GetStageAsync("inexistente", TestContext.Current.CancellationToken)).ShouldBeNull();
22+
23+
var executed = host.Services.GetFakeLogCollector().GetSnapshot()
24+
.Where(r => r.Category == DbLoggerCategory.Database.Command.Name && r.Id.Id == RelationalEventId.CommandExecuted.Id)
25+
.ToList();
26+
executed.ShouldNotBeEmpty();
27+
executed.ShouldAllBe(r => r.Level == LogLevel.Debug);
28+
}
29+
}

‎tests/PythonScriptRunner.EntityFrameworkCore.Tests/PythonScriptRunner.EntityFrameworkCore.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.Extensions.Configuration" />
2222
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
23+
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" />
2324
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
2425
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
2526
<PackageReference Include="Shouldly" />

0 commit comments

Comments
 (0)