Skip to content

Commit

Permalink
Move back publish metrics calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscantero committed Jul 31, 2024
1 parent 3db2589 commit 0069d80
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
21 changes: 1 addition & 20 deletions src/Helpers/MetricsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,13 @@ public static void AddMonitoredItemCount(int delta = 1)
/// <summary>
/// Add a published count.
/// </summary>
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(
Expand Down
27 changes: 26 additions & 1 deletion src/PlcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
11 changes: 11 additions & 0 deletions tests/MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public MetricsTests()
{
_metrics = new Dictionary<string, object>();
_meterListener = new MeterListener();

_meterListener.InstrumentPublished = (instrument, listener) => {
if (instrument.Meter.Name == MetricsHelper.Meter.Name)
{
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 0069d80

Please sign in to comment.