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:
@@ -573,6 +573,11 @@ public class InboundScriptExecutor
|
||||
executionId: parentExecutionId,
|
||||
sourceNode: nodeIdentity?.NodeName);
|
||||
|
||||
// ScadaBridge#56: on a traced request, copy other central node-log lines tagged with its
|
||||
// ExecutionId into its ScriptLog rows (source "node"). Declared inside the try so it is
|
||||
// disposed before any catch block logs the request's outcome (already the inbound row).
|
||||
using var nodeLines = scriptLogger.CaptureNodeLines();
|
||||
|
||||
var context = new InboundScriptContext(
|
||||
parameters,
|
||||
route.WithDeadline(cts.Token)
|
||||
|
||||
@@ -3,6 +3,7 @@ using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Debugging;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.InboundAPI;
|
||||
|
||||
@@ -33,14 +34,22 @@ namespace ZB.MOM.WW.ScadaBridge.InboundAPI;
|
||||
/// A method script may log from several concurrent tasks, so the counters are guarded. Nothing in
|
||||
/// this class is allowed to throw into the script: a debug observer must never fail the request.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// Node lines (ScadaBridge#56): while <see cref="CaptureNodeLines"/> is open on a traced request,
|
||||
/// every other central node-log line tagged with the request's <c>ExecutionId</c> becomes a row with
|
||||
/// source <c>node</c>, under the same line cap. This logger's own node forward is suppressed, so a
|
||||
/// <c>Log.*</c> line is never copied twice.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed class InboundScriptLogger
|
||||
public sealed class InboundScriptLogger : IExecutionLogTarget
|
||||
{
|
||||
/// <summary>Per-line message cap (design D7); longer messages are truncated with an ellipsis.</summary>
|
||||
public const int MaxMessageChars = 2_000;
|
||||
|
||||
private const string SourceScript = "script";
|
||||
private const string SourceRuntime = "runtime";
|
||||
private const string SourceNode = "node";
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private readonly Action<AuditEvent>? _emit;
|
||||
@@ -54,6 +63,8 @@ public sealed class InboundScriptLogger
|
||||
private int _emitted;
|
||||
private int _dropped;
|
||||
private int _seq;
|
||||
private IDisposable? _capture;
|
||||
private bool _captureClosed;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the per-execution logger.
|
||||
@@ -120,6 +131,9 @@ public sealed class InboundScriptLogger
|
||||
{
|
||||
if (_emit is null) return;
|
||||
|
||||
// The request is over: stop copying node lines first, so none lands after the closing row.
|
||||
CloseCapture();
|
||||
|
||||
int dropped;
|
||||
int seq;
|
||||
lock (_gate)
|
||||
@@ -142,9 +156,13 @@ public sealed class InboundScriptLogger
|
||||
|
||||
try
|
||||
{
|
||||
_logger.Log(level, exception,
|
||||
"Script log [{LogLevel}] for inbound method {Method} (execution {ExecutionId}): {Message}",
|
||||
levelName, _methodName, _executionId, text);
|
||||
// Suppressed for the node-line sink (#56): this line becomes a "script" row below.
|
||||
using (ExecutionLogCapture.Suppress())
|
||||
{
|
||||
_logger.Log(level, exception,
|
||||
"Script log [{LogLevel}] for inbound method {Method} (execution {ExecutionId}): {Message}",
|
||||
levelName, _methodName, _executionId, text);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -171,7 +189,72 @@ public sealed class InboundScriptLogger
|
||||
Emit(levelName, SourceScript, seq, text, exception);
|
||||
}
|
||||
|
||||
private void Emit(string level, string source, int seq, string message, Exception? exception)
|
||||
/// <summary>
|
||||
/// ScadaBridge#56: starts copying the central node-log lines tagged with this request's
|
||||
/// <c>ExecutionId</c> into its rows (source <c>node</c>). A no-op on an untraced request or one
|
||||
/// with no execution id.
|
||||
/// </summary>
|
||||
/// <para>Internal on purpose: it is host plumbing, and a public member of the <c>Log</c> global
|
||||
/// would be callable from script code.</para>
|
||||
/// <returns>A disposable that stops the copy; <see cref="FlushDropped"/> also stops it.</returns>
|
||||
internal IDisposable CaptureNodeLines()
|
||||
{
|
||||
if (_emit is null || _executionId is not { } id)
|
||||
{
|
||||
return NoCapture.Instance;
|
||||
}
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
if (_captureClosed || _capture is not null)
|
||||
{
|
||||
return NoCapture.Instance;
|
||||
}
|
||||
|
||||
_capture = ExecutionLogCapture.Register(id, this);
|
||||
return _capture;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>ScadaBridge#56: one tagged node-log line, from the per-execution sink.</summary>
|
||||
/// <param name="line">The captured line.</param>
|
||||
void IExecutionLogTarget.Capture(CapturedLogLine line)
|
||||
{
|
||||
if (_emit is null) return;
|
||||
|
||||
int seq;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_captureClosed) return;
|
||||
if (_emitted >= _maxLines)
|
||||
{
|
||||
_dropped++;
|
||||
return;
|
||||
}
|
||||
|
||||
_emitted++;
|
||||
seq = ++_seq;
|
||||
}
|
||||
|
||||
Emit(line.Level, SourceNode, seq, Truncate(line.Message), line.Exception, line.Category, line.OccurredAtUtc);
|
||||
}
|
||||
|
||||
private void CloseCapture()
|
||||
{
|
||||
IDisposable? capture;
|
||||
lock (_gate)
|
||||
{
|
||||
_captureClosed = true;
|
||||
capture = _capture;
|
||||
_capture = null;
|
||||
}
|
||||
|
||||
capture?.Dispose();
|
||||
}
|
||||
|
||||
private void Emit(
|
||||
string level, string source, int seq, string message, Exception? exception,
|
||||
string? category = null, DateTime? occurredAtUtc = null)
|
||||
{
|
||||
AuditEvent evt;
|
||||
try
|
||||
@@ -180,6 +263,7 @@ public sealed class InboundScriptLogger
|
||||
AuditChannel.ScriptTrace,
|
||||
AuditKind.ScriptLog,
|
||||
AuditStatus.Delivered,
|
||||
occurredAtUtc: occurredAtUtc,
|
||||
actor: _actor,
|
||||
target: _methodName,
|
||||
sourceNode: _sourceNode,
|
||||
@@ -191,7 +275,7 @@ public sealed class InboundScriptLogger
|
||||
errorMessage: exception?.Message,
|
||||
errorDetail: exception?.ToString(),
|
||||
requestSummary: message,
|
||||
extra: new ScriptTraceExtra { LogLevel = level, LogSource = source, Seq = seq }.ToJson());
|
||||
extra: new ScriptTraceExtra { LogLevel = level, LogSource = source, LogCategory = category, Seq = seq }.ToJson());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -225,4 +309,13 @@ public sealed class InboundScriptLogger
|
||||
var keep = char.IsHighSurrogate(message[MaxMessageChars - 1]) ? MaxMessageChars - 1 : MaxMessageChars;
|
||||
return string.Concat(message.AsSpan(0, keep), "…");
|
||||
}
|
||||
|
||||
private sealed class NoCapture : IDisposable
|
||||
{
|
||||
public static readonly NoCapture Instance = new();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user