Added a one hour debounce of reporting the same errors to sentry

This commit is contained in:
Keivan Beigi 2017-01-05 14:01:58 -08:00
parent 76a7d4f866
commit 782efcfaf1
3 changed files with 45 additions and 3 deletions

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class SentryDebounce
{
private readonly Dictionary<string, DateTime> _dictionary;
private static readonly TimeSpan TTL = TimeSpan.FromHours(1);
public SentryDebounce()
{
_dictionary = new Dictionary<string, DateTime>();
}
public bool Allowed(IEnumerable<string> fingerPrint)
{
var key = string.Join("|", fingerPrint);
DateTime expiry;
_dictionary.TryGetValue(key, out expiry);
if (expiry >= DateTime.Now)
{
return false;
}
_dictionary[key] = DateTime.Now + TTL;
return true;
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using NLog;
@ -12,7 +11,6 @@ using SharpRaven.Data;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class SentryUserFactory : ISentryUserFactory
{
public SentryUser Create()
@ -21,6 +19,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
}
}
[Target("Sentry")]
public class SentryTarget : TargetWithLayout
{
@ -39,6 +38,8 @@ namespace NzbDrone.Common.Instrumentation.Sentry
{LogLevel.Warn, ErrorLevel.Warning},
};
private readonly SentryDebounce _debounce;
public SentryTarget(string dsn)
{
_client = new RavenClient(new Dsn(dsn), new JsonPacketFactory(), new SentryRequestFactory(), new SentryUserFactory())
@ -53,6 +54,8 @@ namespace NzbDrone.Common.Instrumentation.Sentry
_client.Tags.Add("culture", Thread.CurrentThread.CurrentCulture.Name);
_client.Tags.Add("branch", BuildInfo.Branch);
_client.Tags.Add("version", BuildInfo.Version.ToString());
_debounce = new SentryDebounce();
}
@ -94,6 +97,12 @@ namespace NzbDrone.Common.Instrumentation.Sentry
return;
}
var fingerPrint = GetFingerPrint(logEvent);
if (!_debounce.Allowed(fingerPrint))
{
return;
}
var extras = logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString());
_client.Logger = logEvent.LoggerName;
@ -105,7 +114,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
Extra = extras
};
var fingerPrint = GetFingerPrint(logEvent);
fingerPrint.ForEach(c => sentryEvent.Fingerprint.Add(c));
sentryEvent.Tags.Add("os_name", Environment.GetEnvironmentVariable("OS_NAME"));

View File

@ -183,6 +183,7 @@
<Compile Include="Instrumentation\NzbDroneFileTarget.cs" />
<Compile Include="Instrumentation\NzbDroneLogger.cs" />
<Compile Include="Instrumentation\Sentry\PopulateStackTraceRenderer.cs" />
<Compile Include="Instrumentation\Sentry\SentryDebounce.cs" />
<Compile Include="Instrumentation\Sentry\SentryTarget.cs" />
<Compile Include="Instrumentation\VersionLayoutRenderer.cs" />
<Compile Include="Extensions\LevenstheinExtensions.cs" />