-
Notifications
You must be signed in to change notification settings - Fork 32
/
LogQueue.cs
506 lines (428 loc) · 17.4 KB
/
LogQueue.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using StackifyLib.Models;
using StackifyLib.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
#if NETFULL
using System.Runtime.Remoting.Messaging;
using StackifyLib.Web;
using System.Web;
#endif
namespace StackifyLib.Internal.Logs
{
internal class LogQueue
{
static DateTime _Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
private ConcurrentQueue<Models.LogMsg> _MessageBuffer = null;
private System.Threading.Timer _timer = null;
private TimeSpan _FlushInterval = TimeSpan.FromSeconds(1);
private bool _StopRequested = false;
private bool _UploadingNow = false;
private bool _QueueTooBig = true;
private bool _IsWebApp = false;
private LogClient _LogClient = null;
private bool _PauseUpload = false;
private bool _TimerStarted = false;
public LogQueue(LogClient logClient)
{
StackifyAPILogger.Log("Creating new LogQueue");
_LogClient = logClient;
_IsWebApp = false;// System.Web.Hosting.HostingEnvironment.IsHosted;
_MessageBuffer = new ConcurrentQueue<LogMsg>();
}
public bool CanQueue()
{
//maximum size at which we just drop the messages
return _QueueTooBig;
}
/// <summary>
/// Doing this so the Logger class in StackifyLib doesn't run a timer unless someone actually logs something
/// </summary>
public void EnsureTimer()
{
if (_timer == null)
{
_TimerStarted = true;
_timer = new System.Threading.Timer(OnTimer, null, _FlushInterval, _FlushInterval);
}
}
//private struct ThreadUsage
//{
// public string SrcMethod { get; set; }
// public string TransID { get; set; }
//}
//ConcurrentDictionary<string, ThreadUsage> _ThreadInfo = new ConcurrentDictionary<string, ThreadUsage>();
/// <summary>
/// Should call CanSend() before this. Did not also put that call in here to improve performance. Makes more sense to do it earlier so it can skip other steps up the chain.
/// </summary>
/// <param name="msg"></param>
public void QueueLogMessage(Models.LogMsg msg)
{
try
{
if (msg == null)
{
return;
}
if (!_TimerStarted)
{
EnsureTimer();
}
try
{
if (string.IsNullOrEmpty(msg.Th))
{
msg.Th = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
}
}
catch
{
// ignore
}
#if NETFULL
try
{
if (string.IsNullOrEmpty(msg.TransID))
{
var stackifyRequestID = CallContext.LogicalGetData("Stackify-RequestID");
if (stackifyRequestID != null)
{
msg.TransID = stackifyRequestID.ToString();
}
}
if (string.IsNullOrEmpty(msg.TransID))
{
//gets from Trace.CorrelationManager.ActivityId but doesnt assume it is guid since it technically doesn't have to be
//not calling the CorrelationManager method because it blows up if it isn't a guid
var correltionManagerId = CallContext.LogicalGetData("E2ETrace.ActivityID");
if (correltionManagerId != null && correltionManagerId is Guid && ((Guid)correltionManagerId) != Guid.Empty)
{
msg.TransID = correltionManagerId.ToString();
}
}
if (string.IsNullOrEmpty(msg.TransID))
{
if (_IsWebApp
&& System.Web.Hosting.HostingEnvironment.IsHosted
&& System.Web.HttpContext.Current != null
&& System.Web.HttpContext.Current.Handler != null)
{
msg.TransID = System.Web.HttpContext.Current.Request.GetHashCode().ToString();
}
}
}
catch (System.Web.HttpException ex)
{
StackifyAPILogger.Log("Request not available \r\n" + ex.ToString());
}
catch (Exception ex)
{
StackifyAPILogger.Log("Error figuring out TransID \r\n" + ex.ToString());
}
if (_IsWebApp
&& System.Web.Hosting.HostingEnvironment.IsHosted
&& System.Web.HttpContext.Current != null
&& System.Web.HttpContext.Current.Handler != null)
{
var context = System.Web.HttpContext.Current;
msg.UrlFull = context.Request.Url.ToString();
if (context.Items.Contains("Stackify.ReportingUrl"))
{
msg.UrlRoute = context.Items["Stackify.ReportingUrl"].ToString();
}
else
{
var resolver = new RouteResolver(new HttpContextWrapper(context));
var route = resolver.GetRoute();
if (!string.IsNullOrEmpty(route.Action))
{
msg.UrlRoute = route.ToString();
}
}
if (string.IsNullOrEmpty(msg.UrlRoute))
{
if (string.IsNullOrWhiteSpace(context.Request.AppRelativeCurrentExecutionFilePath) == false)
{
HelperFunctions.CleanPartialUrl(context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~'));
}
}
}
#else
// else if .Net Core
// get RequestID
if (string.IsNullOrEmpty(msg.TransID))
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var agentAssemblyQry = assemblies.Where(assembly => assembly.FullName.Contains("Stackify.Agent"));
if(agentAssemblyQry.Count() > 0)
{
var middleware = agentAssemblyQry.First();
var callContextType = middleware.GetType("Stackify.Agent.Threading.StackifyCallContext");
if (callContextType != null)
{
var traceCtxType = middleware.GetType("Stackify.Agent.Tracing.ITraceContext");
if(traceCtxType != null)
{
var traceContextProp = callContextType.GetProperty("TraceContext")?.GetValue(null);
if (traceContextProp != null)
{
var reqIdProp = traceCtxType.GetProperty("RequestId")?.GetValue(traceContextProp)?.ToString();
if(!string.IsNullOrEmpty(reqIdProp))
msg.TransID = reqIdProp;
}
}
}
}
}
#endif
_MessageBuffer.Enqueue(msg);
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #QueueLogMessage failed", ex);
}
}
private void OnTimer(Object stateInfo)
{
if (_PauseUpload)
{
return;
}
_timer.Change(-1, -1); //disable while it does this so it does fire multiple times
try
{
//remove messages in the queue that are old
if (!_LogClient.IsAuthorized() && _MessageBuffer.Count > 0)
{
var cutoff = (long)DateTime.UtcNow.AddMinutes(-5).Subtract(_Epoch).TotalMilliseconds;
while (true)
{
LogMsg msg;
if (_MessageBuffer.TryPeek(out msg) && msg.EpochMs < cutoff)
{
LogMsg msg2;
_MessageBuffer.TryDequeue(out msg2);
}
else
{
break;
}
}
if (_timer != null && !_StopRequested)
{
_timer.Change(_FlushInterval, _FlushInterval);
}
return;
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #OnTimer failed", ex);
}
try
{
int processedCount = FlushLoop();
//auto adjust how often we send based on how the rate of which data is being logged
if (processedCount >= 100)
{
if (_FlushInterval.TotalSeconds > 1)
{
_FlushInterval = TimeSpan.FromSeconds(_FlushInterval.TotalSeconds / 2);
StackifyAPILogger.Log(string.Format("#LogQueue Adjust log flush interval down to {0:0.00} seconds", _FlushInterval.TotalSeconds));
}
}
else if (processedCount < 10 && _FlushInterval != TimeSpan.FromSeconds(5))
{
double proposedSeconds = _FlushInterval.TotalSeconds * 1.25;
if (proposedSeconds < 1)
{
proposedSeconds = 1;
}
else if (proposedSeconds > 5)
{
proposedSeconds = 5;
}
if (_FlushInterval.TotalSeconds < proposedSeconds)
{
_FlushInterval = TimeSpan.FromSeconds(proposedSeconds);
StackifyAPILogger.Log(string.Format("#LogQueue Adjust log flush interval up to {0:0.00} seconds", _FlushInterval.TotalSeconds));
}
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #QueueLogMessage failed", ex);
}
if (_timer != null && !_StopRequested)
{
_timer.Change(_FlushInterval, _FlushInterval);
}
}
private int FlushLoop()
{
_UploadingNow = true;
int processedCount = 0;
try
{
int queueSize = _MessageBuffer.Count;
// StackifyLib.Utils.StackifyAPILogger.Log("FlushLoop - count: " + queueSize + " for " + _LogClient.LoggerName);
//CanSend() does an IdentifyApp so there is a chance this could take a while
if (queueSize > 0 && _LogClient.CanUpload())
{
_QueueTooBig = queueSize < Logger.MaxLogBufferSize;
bool keepGoing = false;
int flushTimes = 0;
//Keep flushing
do
{
int count = FlushOnce();
if (count >= 100)
{
keepGoing = true;
}
else
{
keepGoing = false;
}
flushTimes++;
processedCount += count;
} while (keepGoing && flushTimes < 25);
_QueueTooBig = _MessageBuffer.Count < Logger.MaxLogBufferSize;
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #FlushLoop failed", ex);
}
_UploadingNow = false;
return processedCount;
}
private int FlushOnce()
{
// StackifyLib.Utils.StackifyAPILogger.Log("Calling FlushOnceAsync");
int messageSize = 0;
var chunk = new List<LogMsg>();
//we only want to do this once at a time but the actual send is done async
long startMs = (long)DateTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;
try
{
while (true)
{
LogMsg msg;
if (_MessageBuffer.TryDequeue(out msg))
{
//do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen
if (msg.Msg != null && msg.Msg != null && msg.Msg.Contains("StackifyLib:"))
{
//skip!
continue;
}
chunk.Add(msg);
messageSize++;
//if we get something newer than when we started reading, break so it doesn't keep reading perpetually.
//Let it finish so the timer can run again and do a new batch
if (msg.EpochMs > startMs)
{
break;
}
//send this packet in a batch
if (messageSize > 100)
{
break;
}
}
else
{
break;
}
}
if (chunk.Any())
{
var response = _LogClient.SendLogsByGroups(chunk.ToArray());
if (response != null && response.Exception != null)
{
Utils.StackifyAPILogger.Log("Requeueing log messages due to error: " + response.Exception.ToString(), true);
if (response.IsClientError())
{
Utils.StackifyAPILogger.Log("#LogQueue Not requeueing log messages due to client error: " + response.StatusCode, true);
}
else
{
try
{
bool messagesSentTooManyTimes = EnqueueForRetransmission(chunk);
if (messagesSentTooManyTimes)
{
Utils.StackifyAPILogger.Log("#LogQueue Some messages not queued again due to too many failures uploading");
}
}
catch (Exception ex2)
{
Utils.StackifyAPILogger.Log("#LogQueue Error trying to requeue messages " + ex2.ToString());
}
}
}
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #FlushOnce failed", ex);
EnqueueForRetransmission(chunk);
}
return messageSize;
}
public void Stop()
{
Utils.StackifyAPILogger.Log("#LogQueue stop received");
_StopRequested = true;
if (!_UploadingNow)
{
FlushLoop();
}
else
{
DateTime stopWaiting = DateTime.UtcNow.AddSeconds(15);
//wait for it to finish up to 5 seconds
while (_UploadingNow && DateTime.UtcNow < stopWaiting)
{
System.Threading.Thread.Sleep(10);
}
}
Utils.StackifyAPILogger.Log("#LogQueue stop complete");
}
public void Pause(bool isPaused)
{
_PauseUpload = isPaused;
}
private bool EnqueueForRetransmission(List<LogMsg> chunk)
{
bool skippedMessage = false;
try
{
foreach (var item in chunk)
{
++item.UploadErrors;
// retry up to 5 times
if (item.UploadErrors < 5)
{
_MessageBuffer.Enqueue(item);
}
else
{
skippedMessage = true;
}
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#LogQueue #EnqueueForRetransmission failed", ex);
}
return skippedMessage;
}
}
}