Skip to content

Commit d328525

Browse files
author
mwatson
committed
Issue with appenders telling metrics to stop but they shouldn't be stopped. Caused queue to build up and use too much memory
1 parent c589272 commit d328525

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

Src/StackifyLib.log4net.v1_2_10/StackifyAppender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected override void OnClose()
4646
try
4747
{
4848
_logClient.Close();
49-
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
49+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue("log4net v1.2 OnClose");
5050

5151
}
5252
catch

Src/StackifyLib.log4net/StackifyAppender.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ protected override void OnClose()
3838
{
3939
try
4040
{
41+
StackifyAPILogger.Log("log4net appender closing");
42+
4143
_logClient.Close();
42-
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
4344

45+
//This is to force the metrics queue to flush as well
46+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue("log4net OnClose");
4447
}
4548
catch
4649
{

Src/StackifyLib.nLog/StackifyTarget.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected override void CloseTarget()
4343
{
4444
Utils.StackifyAPILogger.Log("NLog target closing");
4545
_logClient.Close();
46-
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue();
46+
StackifyLib.Internal.Metrics.MetricClient.StopMetricsQueue("NLog CloseTarget");
4747
}
4848
catch (Exception ex)
4949
{
@@ -53,6 +53,8 @@ protected override void CloseTarget()
5353

5454
protected override void InitializeTarget()
5555
{
56+
Utils.StackifyAPILogger.Log("NLog InitializeTarget");
57+
5658
_logClient = new LogClient("StackifyLib.net-nlog", apiKey, uri);
5759
if (!String.IsNullOrEmpty(globalContextKeys))
5860
{

Src/StackifyLib/Internal/Metrics/MetricClient.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ public static void QueueMetric(Metric metric)
149149
{
150150
try
151151
{
152-
_MetricQueue.Enqueue(metric);
152+
//set a sanity cap
153+
if (_MetricQueue.Count < 100000)
154+
{
155+
_MetricQueue.Enqueue(metric);
156+
}
153157
}
154158
catch(Exception ex)
155159
{
@@ -205,10 +209,12 @@ private static void Aggregate(MetricAggregate aggregate)
205209
private static void ReadAllQueuedMetrics()
206210
{
207211
DateTime maxDate = DateTime.UtcNow; //read only up until now so it doesn't get stuck in an endless loop
208-
//Loop through add sum up the totals of the counts and values by aggregate key then pass it all in at once to update the aggregate dictionary so it is done in one pass
212+
//Loop through add sum up the totals of the counts and values by aggregate key then pass it all in at once to update the aggregate dictionary so it is done in one pass
209213

210214
//key is the aggregate key which is the metric name, type and rounded minute of the occurrence
211215

216+
StackifyAPILogger.Log("ReadAllQueuedMetrics " + maxDate);
217+
212218
var batches = new Dictionary<string, MetricAggregate>();
213219

214220
long processed = 0;
@@ -293,6 +299,7 @@ private static void UploadMetricsCheck(object state)
293299

294300
DateTime currentMinute = DateTime.UtcNow.Floor(TimeSpan.FromMinutes(1));
295301

302+
StackifyAPILogger.Log("Calling UploadMetrics " + currentMinute);
296303
allSuccess = UploadMetrics(currentMinute);
297304

298305
PurgeOldMetrics(purgeOlderThan);
@@ -302,24 +309,43 @@ private static void UploadMetricsCheck(object state)
302309
seconds = .1;
303310
}
304311
}
312+
else
313+
{
314+
StackifyAPILogger.Log("Metrics processing canceled because stop was requested");
315+
}
305316

306317

307318

308319
_Timer.Change(TimeSpan.FromSeconds(seconds), TimeSpan.FromSeconds(seconds));
309320

310321
}
311322

312-
public static void StopMetricsQueue()
323+
324+
public static void StopMetricsQueue(string reason = "Unknown")
313325
{
326+
StackifyAPILogger.Log("StopMetricsQueue called by " + reason, true);
327+
314328
//don't let t his method run more than once
315329
if (_StopRequested)
316330
return;
317331

318332
_StopRequested = true;
319333

320-
DateTime currentMinute = DateTime.UtcNow.AddMinutes(2).Floor(TimeSpan.FromMinutes(1));
334+
try
335+
{
336+
DateTime currentMinute = DateTime.UtcNow.AddMinutes(2).Floor(TimeSpan.FromMinutes(1));
337+
338+
UploadMetrics(currentMinute);
339+
340+
}
341+
catch (Exception ex)
342+
{
343+
StackifyAPILogger.Log("StopMetricsQueue error" + ex.ToString(), true);
344+
}
345+
321346

322-
UploadMetrics(currentMinute);
347+
_StopRequested = false;
348+
StackifyAPILogger.Log("StopMetricsQueue completed" + reason, true);
323349
}
324350

325351
public static bool UploadMetrics(DateTime currentMinute)

Src/StackifyLib/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static void Shutdown()
7777
_LogClient.Close();
7878

7979
//flush any remaining metrics as well
80-
MetricClient.StopMetricsQueue();
80+
MetricClient.StopMetricsQueue("Logger Shutdown called");
8181
}
8282

8383
/// <summary>

0 commit comments

Comments
 (0)