diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs new file mode 100644 index 000000000..c5f726bf8 --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; + +namespace NzbDrone.Common.Instrumentation.Sentry +{ + public class SentryDebounce + { + private readonly Dictionary _dictionary; + private static readonly TimeSpan TTL = TimeSpan.FromHours(1); + + public SentryDebounce() + { + _dictionary = new Dictionary(); + } + + public bool Allowed(IEnumerable 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; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs index e35e6cf89..07cc995e8 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs @@ -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")); diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index dcc5d5a2c..ddddb9333 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -183,6 +183,7 @@ +