private void ReadThreadProc()
{
Logger.Trace("ReadThreadProc() begin");
int numMetricsDropped = 0;
while (_runReadThread)
{
try
{
double collectionStart = Util.GetNow();
foreach (ICollectdPlugin plugin in _plugins)
{
var readPlugin = plugin as ICollectdReadPlugin;
if (readPlugin == null)
{
// skip if plugin is not a readplugin, it might be a writeplugin
continue;
}
double start = Util.GetNow();
IList<CollectableValue> collectedValues = readPlugin.Read();
double end = Util.GetNow();
Logger.Info("Read {1} items in {2:0.00}s [{0}]", readPlugin.GetType().Name, collectedValues.Count, end - start);
if (collectedValues == null || !collectedValues.Any())
continue;
lock (_queueLock)
{
foreach (CollectableValue metric in collectedValues)
{
_collectedValueQueue.Enqueue(metric);
while (_collectedValueQueue.Count >= MaxQueueSize)
{
// When queue size grows above the Max limit,
// old entries are removed
_collectedValueQueue.Dequeue();
if ((++numMetricsDropped%1000) == 0)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Exceeded max queue length");
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_EXCEEDED_MAX_QUEUE_LENGTH);
Logger.Log(logEvent);
Logger.Warn("Number of metrics dropped : {0}", numMetricsDropped);
}
}
}
}
}
double collectionEnd = Util.GetNow();
double elapsed = collectionEnd - collectionStart;
double revisedInterval = (_interval - elapsed) * 1000;
if (revisedInterval / _interval < 0.1)
{
Logger.Warn("Read thread took {0} seconds out of {1} second cycle", elapsed, _interval);
}
if (revisedInterval > 0)
Thread.Sleep((int)revisedInterval);
else
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Read thread exceeded cycle time");
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_READ_EXCEEDED_CYCLE_TIME);
Logger.Log(logEvent);
}
}
catch (ThreadInterruptedException)
{
Logger.Info("Read thread interrupted");
}
catch (Exception exp)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Exception in ReadThreadProc()");
logEvent.Exception = exp;
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_UNHANDLED_EXCEPTION);
Logger.Log(logEvent);
Thread.Sleep(_interval * 1000);
}
}
Logger.Trace("ReadThreadProc() return");
}