diff --git a/src/Helpers/MetricsHelper.cs b/src/Helpers/MetricsHelper.cs
index 83941e5c..26c6b4ed 100644
--- a/src/Helpers/MetricsHelper.cs
+++ b/src/Helpers/MetricsHelper.cs
@@ -153,32 +153,13 @@ public static void AddMonitoredItemCount(int delta = 1)
///
/// Add a published count.
///
- public static void AddPublishedCount(string sessionId, string subscriptionId, NotificationMessage notificationMessage, ILogger logger)
+ public static void AddPublishedCount(string sessionId, string subscriptionId, int dataChanges, int events)
{
if (!IsEnabled)
{
return;
}
- int events = 0;
- int dataChanges = 0;
- int diagnostics = 0;
- notificationMessage.NotificationData.ForEach(x => {
- if (x.Body is DataChangeNotification changeNotification)
- {
- dataChanges += changeNotification.MonitoredItems.Count;
- diagnostics += changeNotification.DiagnosticInfos.Count;
- }
- else if (x.Body is EventNotificationList eventNotification)
- {
- events += eventNotification.Events.Count;
- }
- else
- {
- logger.LogDebug("Unknown notification type: {NotificationType}", x.Body.GetType().Name);
- }
- });
-
if (dataChanges > 0)
{
var dataPointsDimensions = MergeWithBaseDimensions(
diff --git a/src/PlcServer.cs b/src/PlcServer.cs
index ccc031bb..31ddf348 100644
--- a/src/PlcServer.cs
+++ b/src/PlcServer.cs
@@ -265,7 +265,27 @@ public override ResponseHeader Publish(
if (PublishMetricsEnabled)
{
- MetricsHelper.AddPublishedCount(context.SessionId.ToString(), subscriptionId.ToString(), notificationMessage, _logger);
+ int events = 0;
+ int dataChanges = 0;
+ int diagnostics = 0;
+
+ notificationMessage.NotificationData.ForEach(x => {
+ if (x.Body is DataChangeNotification changeNotification)
+ {
+ dataChanges += changeNotification.MonitoredItems.Count;
+ diagnostics += changeNotification.DiagnosticInfos.Count;
+ }
+ else if (x.Body is EventNotificationList eventNotification)
+ {
+ events += eventNotification.Events.Count;
+ }
+ else
+ {
+ LogUnknownNotification(x.Body.GetType().Name);
+ }
+ });
+
+ MetricsHelper.AddPublishedCount(context.SessionId.ToString(), subscriptionId.ToString(), dataChanges, events);
}
LogSuccessWithSessionIdAndSubscriptionId(
@@ -644,4 +664,9 @@ partial void LogPeriodicInfo(
Level = LogLevel.Error,
Message = "{message}")]
partial void LogErrorMessage(string message);
+
+ [LoggerMessage(
+ Level = LogLevel.Debug,
+ Message = "Unknown notification type: {NotificationType}")]
+ partial void LogUnknownNotification(string notificationType);
}
diff --git a/tests/MetricsTests.cs b/tests/MetricsTests.cs
index 9eb3f04e..4bb58e65 100644
--- a/tests/MetricsTests.cs
+++ b/tests/MetricsTests.cs
@@ -19,6 +19,7 @@ public MetricsTests()
{
_metrics = new Dictionary();
_meterListener = new MeterListener();
+
_meterListener.InstrumentPublished = (instrument, listener) => {
if (instrument.Meter.Name == MetricsHelper.Meter.Name)
{
@@ -79,6 +80,16 @@ public void TestAddMonitoredItemCount()
counter.Should().Be(1);
}
+ [Test]
+ public void TestAddPublishedCount()
+ {
+ var sessionId = Guid.NewGuid().ToString();
+ var subscriptionId = Guid.NewGuid().ToString();
+ MetricsHelper.AddPublishedCount(sessionId, subscriptionId, 1, 0);
+ _metrics.TryGetValue("opc_plc_published_count_with_type", out var counter).Should().BeTrue();
+ counter.Should().Be(1);
+ }
+
[Test]
public void TestRecordTotalErrors()
{