Repository navigation
Expand file tree
/
Copy pathDiagnostics.cs
More file actions
147 lines (130 loc) · 6.05 KB
/
Copy pathDiagnostics.cs
File metadata and controls
147 lines (130 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.Extensions.Logging;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class Diagnostics
{
private readonly ILogger _logger;
private long _lastNumMessagesSent = 0;
private static Diagnostics _instance = null;
private static object _instanceLock = new object();
private Diagnostics()
{
ILoggerFactory loggerFactory = (ILoggerFactory)Program.AppHost.Services.GetService(typeof(ILoggerFactory));
_logger = loggerFactory.CreateLogger("Diagnostics");
}
public static Diagnostics Singleton
{
get
{
if (_instance == null)
{
lock (_instanceLock)
{
if (_instance == null)
{
_instance = new Diagnostics();
}
}
}
return _instance;
}
}
public DiagnosticsModel Info { get; set; } = new DiagnosticsModel();
private void Clear()
{
Info.PublisherStartTime = DateTime.UtcNow;
Info.ConnectedToBroker = false;
Info.ConnectedToAltBroker = false;
Info.NumberOfOpcSessionsConnected = 0;
Info.NumberOfOpcSubscriptionsConnected = 0;
Info.NumberOfOpcMonitoredItemsMonitored = 0;
Info.MonitoredItemsQueueCount = 0;
Info.EnqueueCount = 0;
Info.EnqueueFailureCount = 0;
Info.NumberOfEvents = 0;
Info.MissedSendIntervalCount = 0;
Info.TooLargeCount = 0;
Info.SentBytes = 0;
Info.SentMessages = 0;
Info.SentLastTime = DateTime.UtcNow;
Info.FailedMessages = 0;
Info.AverageNotificationsInBrokerMessage = 0;
Info.StoredMessagesCount = 0;
}
public async Task RunAsync(CancellationToken cancellationToken = default)
{
Clear();
if (Settings.Instance.DiagnosticsLoggingInterval == 0)
{
// diagnostics are disabled
return;
}
while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay((int)Settings.Instance.DiagnosticsLoggingInterval * 1000, cancellationToken).ConfigureAwait(false);
float messagesPerSecond = ((float)(Info.SentMessages - _lastNumMessagesSent)) / Settings.Instance.DiagnosticsLoggingInterval;
DiagnosticsSend("ConnectedToBroker", new DataValue(Info.ConnectedToBroker));
DiagnosticsSend("ConnectedToAltBroker", new DataValue(Info.ConnectedToAltBroker));
DiagnosticsSend("NumOpcSessions", new DataValue(Info.NumberOfOpcSessionsConnected));
DiagnosticsSend("NumOpcSubscriptions", new DataValue(Info.NumberOfOpcSubscriptionsConnected));
DiagnosticsSend("NumOpcMonitoredItems", new DataValue(Info.NumberOfOpcMonitoredItemsMonitored));
DiagnosticsSend("QueueCapacity", new DataValue((int)Settings.Instance.InternalQueueCapacity));
DiagnosticsSend("QueueCount", new DataValue(Info.MonitoredItemsQueueCount));
DiagnosticsSend("EnqueueFailures", new DataValue(Info.EnqueueFailureCount));
DiagnosticsSend("SentMessages", new DataValue(Info.SentMessages));
DiagnosticsSend("BrokerMessageSize", new DataValue(Info.SentBytes / (Info.SentMessages == 0 ? 1 : Info.SentMessages)));
DiagnosticsSend("BrokerMessageLatency", new DataValue(Info.AverageMessageLatency));
DiagnosticsSend("BrokerMessagesSecond", new DataValue(messagesPerSecond));
DiagnosticsSend("NumOpcMonitoredItemsSecond", new DataValue(messagesPerSecond * Info.AverageNotificationsInBrokerMessage));
DiagnosticsSend("BrokerMessageSendFailures", new DataValue(Info.FailedMessages));
DiagnosticsSend("StoredMessagesLeftToSend", new DataValue(Info.StoredMessagesCount));
using (Process currentProcess = Process.GetCurrentProcess())
{
DiagnosticsSend("CurrentWorkingSetMBs", new DataValue(currentProcess.WorkingSet64 / (1024 * 1024)));
}
_lastNumMessagesSent = Info.SentMessages;
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "writing diagnostics output error");
}
}
}
private void DiagnosticsSend(string displayName, DataValue value)
{
value.ServerTimestamp = DateTime.UtcNow;
if (Settings.Instance.SendDiagnosticsMessages)
{
try
{
MessageProcessorModel messageData = new()
{
ExpandedNodeId = "nsu=http://opcfoundation.org/UA/CloudPublisher/;s=" + displayName,
ApplicationUri = "urn:" + Settings.Instance.PublisherName,
MessageContext = new ServiceMessageContext(null),
Name = displayName,
Value = value
};
// enqueue the message
MessageProcessor.Enqueue(messageData);
}
catch (Exception ex)
{
_logger.LogError($"Message for diagnostics failed with {ex.Message}'.");
}
}
}
}
}