feat(logging): carry ExecutionId across actor hops onto other actors' log lines (ScadaBridge#56)
LogContext is an AsyncLocal and does not cross an Akka message hop, so the run's id now rides the message: Commons.Observability.IExecutionCorrelated (explicit interface member, no wire change) on SetStaticAttributeCommand, AttributeValueChanged (not for an inferred writer), ScriptCallRequest and the four RouteTo*Requests, plus a new additive SourceExecutionId on GetAttributeRequest, WaitForAttributeRequest, WriteAttributeBatchRequest, WriteTagRequest and WriteTagBatchRequest. ScriptRuntimeContext stamps its id on the requests it sends, the Instance Actor hands it on to the DCL, and the Instance, Script, Alarm, Deployment Manager and both DCL actors re-push it per message in AroundReceive (ExecutionLogContext.PushFor: one type test when the message names no execution). The inbound executor pushes the request's ExecutionId for the method execution. Tests (TestKit): Instance Actor line carries the message's id, no leak from the sender's ambient context, scope ends with the message, per-write ids inside a batched SetAttributes, ids forwarded on DCL writes, ScriptRuntimeContext stamps; DCL adapter lines on single/batch writes carry the id; inbound executor lines carry the request id; Commons mapping + no serialized member. Each mutation- proved. Still open on #56: a queryable per-execution sink and a Flow Debugger section. The DCL actors' own Akka _log lines bypass Serilog (filed #120). Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using Akka.Actor;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -262,6 +263,21 @@ public class AlarmActor : ReceiveActor
|
||||
Receive<ExpressionEvalFailed>(HandleExpressionEvalFailed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#56: while handling a message sent on behalf of an execution
|
||||
/// (<see cref="IExecutionCorrelated"/>), every line this actor logs - and every line logged by
|
||||
/// a continuation it starts - carries that execution's <c>ExecutionId</c>. A message that names
|
||||
/// no execution costs one type test.
|
||||
/// </summary>
|
||||
/// <param name="receive">The behaviour that handles the message.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Whether the message was handled.</returns>
|
||||
protected override bool AroundReceive(Receive receive, object message)
|
||||
{
|
||||
using (ExecutionLogContext.PushFor(message))
|
||||
return base.AroundReceive(receive, message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreStart()
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using Akka.Actor;
|
||||
using Akka.Cluster;
|
||||
@@ -571,6 +572,21 @@ public class DeploymentManagerActor : ReceiveActor, IWithTimers
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#56: while handling a message sent on behalf of an execution
|
||||
/// (<see cref="IExecutionCorrelated"/>), every line this actor logs - and every line logged by
|
||||
/// a continuation it starts - carries that execution's <c>ExecutionId</c>. A message that names
|
||||
/// no execution costs one type test.
|
||||
/// </summary>
|
||||
/// <param name="receive">The behaviour that handles the message.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Whether the message was handled.</returns>
|
||||
protected override bool AroundReceive(Receive receive, object message)
|
||||
{
|
||||
using (ExecutionLogContext.PushFor(message))
|
||||
return base.AroundReceive(receive, message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreStart()
|
||||
{
|
||||
@@ -3509,7 +3525,9 @@ public class DeploymentManagerActor : ReceiveActor, IWithTimers
|
||||
var inner = new WaitForAttributeRequest(
|
||||
request.CorrelationId, request.InstanceUniqueName, request.AttributeName,
|
||||
request.TargetValueEncoded, null, request.Timeout, DateTimeOffset.UtcNow,
|
||||
request.RequireGoodQuality);
|
||||
request.RequireGoodQuality,
|
||||
// ScadaBridge#56: the Instance Actor logs under the inbound request's id.
|
||||
SourceExecutionId: request.ParentExecutionId);
|
||||
|
||||
// Ask bounded by the WAIT timeout + slack — NOT a fixed 30s (the wait legitimately blocks up to request.Timeout).
|
||||
instanceActor.Ask<WaitForAttributeResponse>(inner, request.Timeout + TimeSpan.FromSeconds(5))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using Akka.Actor;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -450,6 +451,21 @@ public class InstanceActor : ReceiveActor
|
||||
Receive<FlushStaticOverrides>(_ => FlushDirtyStaticOverrides());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#56: while handling a message sent on behalf of an execution
|
||||
/// (<see cref="IExecutionCorrelated"/>), every line this actor logs - and every line logged by
|
||||
/// a continuation it starts - carries that execution's <c>ExecutionId</c>. A message that names
|
||||
/// no execution costs one type test.
|
||||
/// </summary>
|
||||
/// <param name="receive">The behaviour that handles the message.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Whether the message was handled.</returns>
|
||||
protected override bool AroundReceive(Receive receive, object message)
|
||||
{
|
||||
using (ExecutionLogContext.PushFor(message))
|
||||
return base.AroundReceive(receive, message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreStart()
|
||||
{
|
||||
@@ -608,7 +624,11 @@ public class InstanceActor : ReceiveActor
|
||||
var caller = Sender;
|
||||
var outcomes = new Task<SetStaticAttributeResponse>[request.Writes.Count];
|
||||
for (var i = 0; i < outcomes.Length; i++)
|
||||
outcomes[i] = ApplySetAttribute(request.Writes[i]);
|
||||
{
|
||||
// ScadaBridge#56: the batch envelope names no execution; each write names its own.
|
||||
using (ExecutionLogContext.PushFor(request.Writes[i]))
|
||||
outcomes[i] = ApplySetAttribute(request.Writes[i]);
|
||||
}
|
||||
|
||||
if (outcomes.All(t => t.IsCompleted))
|
||||
{
|
||||
@@ -897,7 +917,9 @@ public class InstanceActor : ReceiveActor
|
||||
resolved.Attribute.BoundDataConnectionName!,
|
||||
resolved.Attribute.DataSourceReference!,
|
||||
writeValue,
|
||||
DateTimeOffset.UtcNow);
|
||||
DateTimeOffset.UtcNow,
|
||||
// ScadaBridge#56: the DCL (and its adapter) log under the writing execution's id.
|
||||
SourceExecutionId: command.SourceExecutionId);
|
||||
|
||||
// Flow Debugger trace arm (design §5.3): only a TRACED write with a known writer arms;
|
||||
// the next device update is marked as inferred in HandleTagValueUpdate. Untraced writes
|
||||
@@ -1036,7 +1058,9 @@ public class InstanceActor : ReceiveActor
|
||||
}
|
||||
|
||||
var dclReq = new WriteTagBatchRequest(
|
||||
cid, connName!, values, triggerPath, triggerVal, DateTimeOffset.UtcNow);
|
||||
cid, connName!, values, triggerPath, triggerVal, DateTimeOffset.UtcNow,
|
||||
// ScadaBridge#56: the DCL (and its adapter) log under the writing execution's id.
|
||||
SourceExecutionId: request.SourceExecutionId);
|
||||
|
||||
// Ask the DCL and pipe the batch outcome back to the original caller. The DCL
|
||||
// bounds its own write with WriteTimeout; this Ask is bounded a bit longer so a
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
||||
using Akka.Actor;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -228,6 +229,21 @@ public class ScriptActor : ReceiveActor, IWithTimers
|
||||
Receive<ExpressionEvalFailed>(HandleExpressionEvalFailed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#56: while handling a message sent on behalf of an execution
|
||||
/// (<see cref="IExecutionCorrelated"/>), every line this actor logs - and every line logged by
|
||||
/// a continuation it starts - carries that execution's <c>ExecutionId</c>. A message that names
|
||||
/// no execution costs one type test.
|
||||
/// </summary>
|
||||
/// <param name="receive">The behaviour that handles the message.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Whether the message was handled.</returns>
|
||||
protected override bool AroundReceive(Receive receive, object message)
|
||||
{
|
||||
using (ExecutionLogContext.PushFor(message))
|
||||
return base.AroundReceive(receive, message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreStart()
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
|
||||
@@ -10,17 +11,15 @@ namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
|
||||
/// <c>Enrich.FromLogContext()</c> (<c>LoggerConfigurationFactory</c>).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <b>Bounded by design.</b> <c>LogContext</c> is an <c>AsyncLocal</c>: it follows the run's own
|
||||
/// call chain but NOT an Akka message hop. A line logged by the Instance Actor, DCL or alarm
|
||||
/// actors while handling a message sent BY the run runs on that actor's own context and does not
|
||||
/// carry the id. Closing that needs the id on the message envelope plus a per-message push in each
|
||||
/// actor, and a queryable sink (an execution-id column on the replicated <c>site_events</c> table);
|
||||
/// both are recorded as the remaining work on the issue.
|
||||
/// <c>LogContext</c> is an <c>AsyncLocal</c>: it follows the run's own call chain but NOT an Akka
|
||||
/// message hop. The messages the run sends carry its id instead (<see cref="IExecutionCorrelated"/>)
|
||||
/// and the Instance, Script, Alarm, Deployment Manager and DCL actors re-push it per message
|
||||
/// (<see cref="ExecutionLogContext.PushFor"/>).
|
||||
/// </remarks>
|
||||
internal static class ScriptRunLogScope
|
||||
{
|
||||
/// <summary>Property name carrying the run's <c>ExecutionId</c>.</summary>
|
||||
internal const string ExecutionIdProperty = "ExecutionId";
|
||||
internal const string ExecutionIdProperty = ExecutionLogContext.ExecutionIdProperty;
|
||||
|
||||
/// <summary>Property name carrying the instance's unique name.</summary>
|
||||
internal const string InstanceNameProperty = "InstanceName";
|
||||
|
||||
@@ -719,7 +719,9 @@ public class ScriptRuntimeContext
|
||||
{
|
||||
var correlationId = Guid.NewGuid().ToString();
|
||||
var request = new GetAttributeRequest(
|
||||
correlationId, _instanceName, attributeName, DateTimeOffset.UtcNow);
|
||||
correlationId, _instanceName, attributeName, DateTimeOffset.UtcNow,
|
||||
// ScadaBridge#56: the Instance Actor logs under this run's id while serving it.
|
||||
SourceExecutionId: _executionId);
|
||||
|
||||
var response = await _instanceActor.Ask<GetAttributeResponse>(request, _askTimeout).ConfigureAwait(false);
|
||||
|
||||
@@ -823,7 +825,9 @@ public class ScriptRuntimeContext
|
||||
var cid = Guid.NewGuid().ToString();
|
||||
var req = new WaitForAttributeRequest(
|
||||
cid, _instanceName, name, targetValueEncoded, predicate, timeout, DateTimeOffset.UtcNow,
|
||||
requireGoodQuality);
|
||||
requireGoodQuality,
|
||||
// ScadaBridge#56: the Instance Actor logs under this run's id while serving it.
|
||||
SourceExecutionId: _executionId);
|
||||
|
||||
var startedUtc = DateTime.UtcNow;
|
||||
var sw = _isTraced ? Stopwatch.StartNew() : null;
|
||||
@@ -1126,7 +1130,9 @@ public class ScriptRuntimeContext
|
||||
{
|
||||
var cid = Guid.NewGuid().ToString();
|
||||
var batchReq = new WriteAttributeBatchRequest(
|
||||
cid, _instanceName, encodedValues, triggerAttr, triggerEncoded, DateTimeOffset.UtcNow);
|
||||
cid, _instanceName, encodedValues, triggerAttr, triggerEncoded, DateTimeOffset.UtcNow,
|
||||
// ScadaBridge#56: rides on to the DCL batch write so both actors log under this run's id.
|
||||
SourceExecutionId: _executionId);
|
||||
|
||||
// 35s: the InstanceActor's DCL Ask is internally bounded at 30s, so allow a small
|
||||
// margin so the DCL's own typed failure/timeout reply is the one we observe rather
|
||||
|
||||
Reference in New Issue
Block a user