Skip to content

Commit

Permalink
Disable publish metrics during high load
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscantero committed Jul 30, 2024
1 parent b87ce16 commit 50280de
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/PlcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace OpcPlc;
using System.Threading;

public partial class PlcServer : StandardServer
{
{
private const uint PlcShutdownWaitSeconds = 10;

public PlcNodeManager PlcNodeManager { get; set; }

public AlarmConditionServerNodeManager AlarmNodeManager { get; set; }
Expand All @@ -37,7 +39,9 @@ public partial class PlcServer : StandardServer
public readonly TimeService TimeService;
private readonly ImmutableList<IPluginNodes> _pluginNodes;
private readonly ILogger _logger;
private readonly Timer _periodicLoggingTimer;
private readonly Timer _periodicLoggingTimer;

private bool _disablePublishMetrics;

public PlcServer(OpcPlcConfiguration config, PlcSimulation plcSimulation, TimeService timeService, ImmutableList<IPluginNodes> pluginNodes, ILogger logger)
{
Expand All @@ -55,14 +59,20 @@ public PlcServer(OpcPlcConfiguration config, PlcSimulation plcSimulation, TimeSe
ThreadPool.GetAvailableThreads(out int availWorkerThreads, out int availCompletionPortThreads);
int sessionCount = ServerInternal.SessionManager.GetSessions().Count;
IList<Subscription> subscriptions = ServerInternal.SubscriptionManager.GetSubscriptions();
int monitoredItemsCount = subscriptions.Sum(s => s.MonitoredItemCount);
LogPeriodicInfo(
ServerInternal.SessionManager.GetSessions().Count,
ServerInternal.SubscriptionManager.GetSubscriptions().Count,
ServerInternal.SubscriptionManager.GetSubscriptions().Sum(s => s.MonitoredItemCount),
sessionCount,
subscriptions.Count,
monitoredItemsCount,
curProc.WorkingSet64 / 1024 / 1024,
availWorkerThreads,
availCompletionPortThreads,
curProc.Threads.Count);
_disablePublishMetrics = sessionCount > 40 || monitoredItemsCount > 500;
}
catch
{
Expand Down Expand Up @@ -209,7 +219,10 @@ public override ResponseHeader Publish(

var responseHeader = base.Publish(requestHeader, subscriptionAcknowledgements, out subscriptionId, out availableSequenceNumbers, out moreNotifications, out notificationMessage, out results, out diagnosticInfos);

MetricsHelper.AddPublishedCount(context.SessionId.ToString(), subscriptionId.ToString(), notificationMessage, _logger);
if (!_disablePublishMetrics)
{
MetricsHelper.AddPublishedCount(context.SessionId.ToString(), subscriptionId.ToString(), notificationMessage, _logger);
}

LogSuccessWithSessionIdAndSubscriptionId(
nameof(Publish),
Expand Down Expand Up @@ -513,8 +526,6 @@ protected override void OnServerStopping()
base.OnServerStopping();
}

private const uint PlcShutdownWaitSeconds = 10;

[LoggerMessage(
Level = LogLevel.Information,
Message = "\n\t# Open sessions: {Sessions}\n" +
Expand Down

0 comments on commit 50280de

Please sign in to comment.