feat(logging): bridge Akka logging to Serilog and copy traced runs' node-log lines into the Flow Debugger (ScadaBridge#120, #56)
#120: Akka ILoggingAdapter lines used to bypass Serilog and go to stdout only. - BuildHocon sets akka.loggers = [Host.Actors.SerilogAkkaLogger] and keeps akka.loglevel = INFO, so Akka Debug chatter is still filtered at the adapter. The INFO+ lines that used to reach stdout now go through the Serilog pipeline instead, with no extra volume. - Lines reach the file sinks with the node enrichers. SourceContext is the logging class's full name, AkkaSource is the actor path, Akka's own timestamp is kept, and format arguments become structured properties. - The Akka logger actor writes on its own thread, after the logging actor's LogContext is gone. To keep the ExecutionId, the two DCL actors log through ExecutionAwareLogging.GetLogger(Context), which records ExecutionLogContext.Current (a new AsyncLocal mirror of the pushed id) in an ExecutionTaggedLogMessage. The logger actor pushes that id again while it writes the line. #56 remainder (coordinator decision: no replicated schema change): - The host pipeline gains ExecutionLogCaptureSink. - While a run is traced, its step logger registers its ExecutionId: the site ScriptLogger from the launcher (plus each inline CallShared child id), and the central InboundScriptLogger from the executor. - Each tagged line becomes a ScriptLog row with LogSource "node" and the new ScriptTraceExtra.LogCategory. - The rows count against the existing MaxLogLinesPerRun cap, are clipped at 2 000 chars, ride the low-priority collector lane and pass the same redactor. Untraced runs cost one volatile read. - Lines are never copied twice. The step loggers' own forwards run inside ExecutionLogCapture.Suppress(), and the sink suppresses itself while it calls a target. The registration is declared inside the launcher's and executor's try, so it ends before the catch blocks log the outcome, and FlushDropped closes it. - FlowGraphBuilder lists node lines in FlowGraph.NodeLogs and never folds them onto a step. - The Flow Debugger shows them in a Node log card (FlowNodeLogPanel): grouped per execution under its run step, with an All | Warn + filter. Live and Replay both read the same rows, so site scope and role rules apply unchanged. - CLI --show-logs prints them as a trailing block. Tests: Commons ExecutionLogCaptureTests, ExecutionLogContext.Current, FlowGraphBuilder node lines. SiteRuntime NodeLogCaptureTests covers cross-actor capture via the launcher, untraced runs capturing nothing, no double capture of script and runtime lines or of the launcher failure line, the shared cap, collector redaction, shared-script child ids and FlushDropped closing capture. InboundNodeLogCaptureTests covers the central path. DCL ExecutionAwareLoggingTests covers the DataConnectionActor write-path Debug line being tagged and an uncorrelated line not being tagged. Host SerilogAkkaLoggerTests covers mapping, the ExecutionId crossing the logger hop, HOCON, ActorSystem boot and the Akka line reaching a traced run's step log end to end. Host LoggerConfigurationTests checks that the sink is wired. bUnit FlowNodeLogPanelTests and a CLI projection test cover the UI and CLI. Every new assertion was mutation-proven. Docs: step-logs design section 12, Component-SiteRuntime, CentralUI, InboundAPI, Host, CLI README, CLAUDE.md. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Serilog;
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Debugging;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.InboundAPI.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#56 (central half): while a method request is traced, other central node-log lines
|
||||
/// tagged with its <c>ExecutionId</c> become <c>node</c> rows of the request; the script's own
|
||||
/// <c>Log.*</c> line and the executor's outcome line are not copied, and an untraced request copies
|
||||
/// nothing.
|
||||
/// </summary>
|
||||
public class InboundNodeLogCaptureTests
|
||||
{
|
||||
private readonly Serilog.ILogger _node = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Sink(new ExecutionLogCaptureSink())
|
||||
.CreateLogger();
|
||||
|
||||
private readonly InboundScriptExecutor _executor;
|
||||
private readonly ApiMethod _method = new("node", "x") { Id = 1, TimeoutSeconds = 10 };
|
||||
|
||||
/// <summary>Builds an executor whose own logger writes to the node pipeline, like the host's.</summary>
|
||||
public InboundNodeLogCaptureTests()
|
||||
=> _executor = new InboundScriptExecutor(new SerilogMel(_node), Substitute.For<IServiceProvider>());
|
||||
|
||||
private sealed class SerilogMel(Serilog.ILogger inner) : ILogger<InboundScriptExecutor>
|
||||
{
|
||||
private readonly Serilog.ILogger _inner = inner.ForContext("SourceContext", typeof(InboundScriptExecutor).FullName);
|
||||
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Information;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel)) return;
|
||||
var level = logLevel switch
|
||||
{
|
||||
LogLevel.Information => Serilog.Events.LogEventLevel.Information,
|
||||
LogLevel.Warning => Serilog.Events.LogEventLevel.Warning,
|
||||
_ => Serilog.Events.LogEventLevel.Error,
|
||||
};
|
||||
_inner.Write(level, exception, "{Message:l}", formatter(state, exception));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<List<(AuditDetails Details, ScriptTraceExtra Extra)>> RunAsync(
|
||||
Func<InboundScriptContext, Task<object?>> handler, bool trace)
|
||||
{
|
||||
_executor.RegisterHandler("node", handler);
|
||||
var sink = new List<AuditEvent>();
|
||||
var route = new RouteHelper(Substitute.For<IInstanceLocator>(), Substitute.For<IInstanceRouter>())
|
||||
.WithTraceSink(e => { lock (sink) sink.Add(e); });
|
||||
await _executor.ExecuteAsync(_method, new Dictionary<string, object?>(), route, TimeSpan.FromSeconds(10),
|
||||
parentExecutionId: Guid.NewGuid(), trace: trace);
|
||||
lock (sink)
|
||||
{
|
||||
return sink
|
||||
.Select(e => AuditDetailsCodec.Deserialize(e.DetailsJson))
|
||||
.Where(d => d.Kind == nameof(AuditKind.ScriptLog))
|
||||
.Select(d => (d, ScriptTraceExtra.TryParse(d.Extra)!))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A callee's line on the request's flow becomes a node row with its category.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task TracedRequest_CalleeLine_IsANodeRow()
|
||||
{
|
||||
var rows = await RunAsync(async _ =>
|
||||
{
|
||||
await Task.Yield();
|
||||
_node.ForContext("SourceContext", "Acme.InboundDatabaseHelper").Warning("query retried {Count}", 2);
|
||||
return null;
|
||||
}, trace: true);
|
||||
|
||||
var row = Assert.Single(rows);
|
||||
Assert.Equal("node", row.Extra.LogSource);
|
||||
Assert.Equal("warn", row.Extra.LogLevel);
|
||||
Assert.Equal("Acme.InboundDatabaseHelper", row.Extra.LogCategory);
|
||||
Assert.Equal("query retried 2", row.Details.RequestSummary);
|
||||
}
|
||||
|
||||
/// <summary>An untraced request registers nothing, so the same line is not captured.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task UntracedRequest_CapturesNothing()
|
||||
{
|
||||
var rows = await RunAsync(_ =>
|
||||
{
|
||||
_node.Warning("query retried");
|
||||
return Task.FromResult<object?>(null);
|
||||
}, trace: false);
|
||||
|
||||
Assert.Empty(rows);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The script's own Log line is one script row (its node forward is suppressed), and the
|
||||
/// executor's failure line is the inbound row's error, so it is not copied.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ScriptLine_AndExecutorFailureLine_AreNotCopied()
|
||||
{
|
||||
var rows = await RunAsync(ctx =>
|
||||
{
|
||||
ctx.Log.Info("from the script");
|
||||
throw new InvalidOperationException("boom");
|
||||
}, trace: true);
|
||||
|
||||
var row = Assert.Single(rows);
|
||||
Assert.Equal("script", row.Extra.LogSource);
|
||||
Assert.Equal("from the script", row.Details.RequestSummary);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user