Some checks failed
v2-ci / build (push) Failing after 42s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (push) Has been skipped
Phase7Composer now carries UnsAreaProjection + UnsLineProjection lists so the applier can materialise the full UNS topology in the OPC UA address space. New IOpcUaAddressSpaceSink.EnsureFolder(folderNodeId, parentNodeId, displayName) seam (no-op default, recorded in tests, forwarded by DeferredAddressSpaceSink, implemented by SdkAddressSpaceSink). The SDK- side OtOpcUaNodeManager gains an EnsureFolder API that creates FolderState nodes with proper parent linkage; RebuildAddressSpace now clears folders too so re-applies don't accumulate stale topology. Phase7Applier.MaterialiseHierarchy walks composition.UnsAreas → composition.UnsLines → composition.EquipmentNodes, calling EnsureFolder with the correct parent at each level. Idempotent — calling twice with the same composition is a no-op. OpcUaPublishActor.HandleRebuild invokes it after Phase7Applier.Apply so OPC UA clients browsing the server now see Area/Line/Equipment as proper folders rather than flat tag ids. DeploymentArtifact.ParseComposition reads UnsAreas + UnsLines from the JSON snapshot the ControlPlane emits, populating the new fields when present. Phase7Composer.Compose now accepts UnsAreas + UnsLines; a 3-arg overload preserves the old signature for legacy callers + existing tests. The Phase7CompositionResult convenience ctor likewise keeps the planner tests working without UNS data. 3 new hierarchy tests (pure unit + boot-verify against a real OtOpcUaSdkServer); OpcUaServer suite is 48/48 green (was 45, +3), Runtime 74/74 unchanged. Closes #85.
179 lines
6.8 KiB
C#
179 lines
6.8 KiB
C#
using System.Diagnostics;
|
|
using System.Diagnostics.Metrics;
|
|
using Akka.Actor;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Engines;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Observability;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Observability;
|
|
|
|
/// <summary>
|
|
/// F13d — verifies the instrumentation sites actually emit on the central
|
|
/// <see cref="OtOpcUaTelemetry"/> meter + activity source. Each test attaches a one-shot
|
|
/// listener, exercises the instrumented path, then asserts the recorded measurement matches.
|
|
/// </summary>
|
|
public sealed class OtOpcUaTelemetryHookTests : RuntimeActorTestBase
|
|
{
|
|
[Fact]
|
|
public void VirtualTagActor_evaluation_emits_otopcua_virtualtag_eval_counter()
|
|
{
|
|
using var recorder = new MeterRecorder("otopcua.virtualtag.eval");
|
|
var parent = CreateTestProbe();
|
|
var evaluator = new ConstEval(42);
|
|
|
|
var actor = parent.ChildActorOf(VirtualTagActor.Props("vt-tel-1", "expr", evaluator: evaluator));
|
|
actor.Tell(new VirtualTagActor.DependencyValueChanged("a", 1, DateTime.UtcNow));
|
|
parent.ExpectMsg<VirtualTagActor.EvaluationResult>();
|
|
|
|
recorder.Total.ShouldBeGreaterThanOrEqualTo(1);
|
|
recorder.WithTag("outcome", "ok").ShouldBeGreaterThanOrEqualTo(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void OpcUaPublishActor_AttributeValueUpdate_emits_sink_write_counter()
|
|
{
|
|
using var recorder = new MeterRecorder("otopcua.opcua.sink.write");
|
|
var sink = new RecordingSink();
|
|
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
|
sink: sink,
|
|
serviceLevel: NullServiceLevelPublisher.Instance,
|
|
subscribeRedundancyTopic: false,
|
|
localNode: Commons.Types.NodeId.Parse("test-node")));
|
|
|
|
actor.Tell(new OpcUaPublishActor.AttributeValueUpdate(
|
|
NodeId: "ns=2;s=tag-1",
|
|
Value: 42,
|
|
Quality: OpcUaQuality.Good,
|
|
TimestampUtc: DateTime.UtcNow));
|
|
|
|
AwaitAssertion(() =>
|
|
{
|
|
recorder.Total.ShouldBeGreaterThanOrEqualTo(1);
|
|
recorder.WithTag("kind", "value").ShouldBeGreaterThanOrEqualTo(1);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void RebuildAddressSpace_starts_an_address_space_rebuild_span()
|
|
{
|
|
using var spanRecorder = new ActivityRecorder("otopcua.opcua.address_space_rebuild");
|
|
var sink = new RecordingSink();
|
|
var actor = Sys.ActorOf(OpcUaPublishActor.PropsForTests(
|
|
sink: sink,
|
|
serviceLevel: NullServiceLevelPublisher.Instance,
|
|
subscribeRedundancyTopic: false,
|
|
localNode: Commons.Types.NodeId.Parse("test-node")));
|
|
|
|
actor.Tell(new OpcUaPublishActor.RebuildAddressSpace(Commons.Types.CorrelationId.NewId()));
|
|
|
|
AwaitAssertion(() => spanRecorder.Activities.ShouldContain(a => a.OperationName == "otopcua.opcua.address_space_rebuild"));
|
|
}
|
|
|
|
private void AwaitAssertion(Action assertion)
|
|
{
|
|
var deadline = DateTime.UtcNow.AddSeconds(2);
|
|
Exception? last = null;
|
|
while (DateTime.UtcNow < deadline)
|
|
{
|
|
try { assertion(); return; }
|
|
catch (Exception ex) { last = ex; Thread.Sleep(25); }
|
|
}
|
|
if (last is not null) throw last;
|
|
}
|
|
|
|
/// <summary>Listens to a single instrument by name and tallies the values + tags.</summary>
|
|
private sealed class MeterRecorder : IDisposable
|
|
{
|
|
private readonly string _name;
|
|
private readonly MeterListener _listener;
|
|
private long _total;
|
|
private readonly List<KeyValuePair<string, object?>[]> _tagSets = new();
|
|
private readonly object _gate = new();
|
|
|
|
public MeterRecorder(string instrumentName)
|
|
{
|
|
_name = instrumentName;
|
|
_listener = new MeterListener
|
|
{
|
|
InstrumentPublished = (instrument, listener) =>
|
|
{
|
|
if (instrument.Meter.Name == OtOpcUaTelemetry.MeterName && instrument.Name == _name)
|
|
listener.EnableMeasurementEvents(instrument);
|
|
}
|
|
};
|
|
_listener.SetMeasurementEventCallback<long>((_, value, tags, _) =>
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_total += value;
|
|
_tagSets.Add(tags.ToArray());
|
|
}
|
|
});
|
|
_listener.Start();
|
|
}
|
|
|
|
public long Total { get { lock (_gate) return _total; } }
|
|
|
|
public int WithTag(string key, string value)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _tagSets.Count(set => set.Any(t => t.Key == key && Equals(t.Value, value)));
|
|
}
|
|
}
|
|
|
|
public void Dispose() => _listener.Dispose();
|
|
}
|
|
|
|
/// <summary>Listens to a single ActivitySource by name and stores started Activities.</summary>
|
|
private sealed class ActivityRecorder : IDisposable
|
|
{
|
|
private readonly string _operationName;
|
|
private readonly ActivityListener _listener;
|
|
private readonly List<Activity> _activities = new();
|
|
private readonly object _gate = new();
|
|
|
|
public ActivityRecorder(string operationName)
|
|
{
|
|
_operationName = operationName;
|
|
_listener = new ActivityListener
|
|
{
|
|
ShouldListenTo = source => source.Name == OtOpcUaTelemetry.ActivitySourceName,
|
|
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
|
|
ActivityStarted = activity =>
|
|
{
|
|
if (activity.OperationName == _operationName)
|
|
{
|
|
lock (_gate) _activities.Add(activity);
|
|
}
|
|
}
|
|
};
|
|
ActivitySource.AddActivityListener(_listener);
|
|
}
|
|
|
|
public IReadOnlyList<Activity> Activities { get { lock (_gate) return _activities.ToArray(); } }
|
|
|
|
public void Dispose() => _listener.Dispose();
|
|
}
|
|
|
|
private sealed class ConstEval(object? value) : IVirtualTagEvaluator
|
|
{
|
|
public VirtualTagEvalResult Evaluate(string virtualTagId, string expression, IReadOnlyDictionary<string, object?> dependencies)
|
|
=> VirtualTagEvalResult.Ok(value);
|
|
}
|
|
|
|
private sealed class RecordingSink : IOpcUaAddressSpaceSink
|
|
{
|
|
public int Writes { get; private set; }
|
|
public void WriteValue(string nodeId, object? value, OpcUaQuality quality, DateTime sourceTimestampUtc) => Writes++;
|
|
public void WriteAlarmState(string alarmNodeId, bool active, bool acknowledged, DateTime occurredUtc) => Writes++;
|
|
public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName) { }
|
|
public void RebuildAddressSpace() { /* recorded via span */ }
|
|
}
|
|
}
|