From cd7e208efab0a09c79523a00b622259dbc3c9464 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Thu, 5 Jan 2017 15:42:17 -0800 Subject: [PATCH] Revert "Use line number instead of message for sentry fingerprint" This reverts commit 5f339c0a92e3255890fb63dfa3e650852a81d60f. # Conflicts: # src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs --- .../Instrumentation/NzbDroneLogger.cs | 5 +-- .../Sentry/PopulateStackTraceRenderer.cs | 20 ----------- .../Instrumentation/Sentry/SentryDebounce.cs | 17 ++++----- .../Instrumentation/Sentry/SentryTarget.cs | 36 +++++++++++-------- src/NzbDrone.Common/NzbDrone.Common.csproj | 1 - 5 files changed, 31 insertions(+), 48 deletions(-) delete mode 100644 src/NzbDrone.Common/Instrumentation/Sentry/PopulateStackTraceRenderer.cs diff --git a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs index 836938de5..7a05776ae 100644 --- a/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs +++ b/src/NzbDrone.Common/Instrumentation/NzbDroneLogger.cs @@ -33,8 +33,6 @@ namespace NzbDrone.Common.Instrumentation GlobalExceptionHandlers.Register(); - ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("populatestacktrace", typeof(PopulateStackTraceRenderer)); - var appFolderInfo = new AppFolderInfo(startupContext); if (Debugger.IsAttached) @@ -108,11 +106,10 @@ namespace NzbDrone.Common.Instrumentation var target = new SentryTarget(dsn) { Name = "sentryTarget", - Layout = "${message}${populatestacktrace}" + Layout = "${message}" }; var loggingRule = new LoggingRule("*", updateClient ? LogLevel.Trace : LogLevel.Error, target); - LogManager.Configuration.AddTarget("sentryTarget", target); LogManager.Configuration.LoggingRules.Add(loggingRule); } diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/PopulateStackTraceRenderer.cs b/src/NzbDrone.Common/Instrumentation/Sentry/PopulateStackTraceRenderer.cs deleted file mode 100644 index bc21f11e3..000000000 --- a/src/NzbDrone.Common/Instrumentation/Sentry/PopulateStackTraceRenderer.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Text; -using NLog; -using NLog.Config; -using NLog.Internal; -using NLog.LayoutRenderers; - -namespace NzbDrone.Common.Instrumentation.Sentry -{ - [ThreadAgnostic] - [LayoutRenderer("populatestacktrace")] - public class PopulateStackTraceRenderer : LayoutRenderer, IUsesStackTrace - { - StackTraceUsage IUsesStackTrace.StackTraceUsage => StackTraceUsage.WithSource; - - protected override void Append(StringBuilder builder, LogEventInfo logEvent) - { - // This is only used to populate the stacktrace. doesn't actually render anything. - } - } -} diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs index c5f726bf8..5517b2dc2 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryDebounce.cs @@ -1,31 +1,32 @@ using System; using System.Collections.Generic; +using NzbDrone.Common.Cache; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.Instrumentation.Sentry { public class SentryDebounce { - private readonly Dictionary _dictionary; - private static readonly TimeSpan TTL = TimeSpan.FromHours(1); + private readonly TimeSpan _ttl; + private readonly Cached _cache; public SentryDebounce() { - _dictionary = new Dictionary(); + _cache = new Cached(); + _ttl = RuntimeInfo.IsProduction ? TimeSpan.FromHours(1) : TimeSpan.FromSeconds(10); } public bool Allowed(IEnumerable fingerPrint) { var key = string.Join("|", fingerPrint); + var exists = _cache.Find(key); - DateTime expiry; - _dictionary.TryGetValue(key, out expiry); - - if (expiry >= DateTime.Now) + if (exists) { return false; } - _dictionary[key] = DateTime.Now + TTL; + _cache.Set(key, true, _ttl); return true; } } diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs index 23237c6c8..a15ec5136 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs @@ -58,35 +58,32 @@ namespace NzbDrone.Common.Instrumentation.Sentry _debounce = new SentryDebounce(); } - - private List GetFingerPrint(LogEventInfo logEvent) + private static List GetFingerPrint(LogEventInfo logEvent) { var fingerPrint = new List { logEvent.Level.Ordinal.ToString(), + logEvent.LoggerName }; - var lineNumber = ""; + var ex = logEvent.Exception; - if (logEvent.StackTrace != null) + if (ex != null) { - var stackFrame = logEvent.StackTrace.GetFrame(logEvent.UserStackFrameNumber); - if (stackFrame != null) + var exception = ex.GetType().Name; + + if (ex.InnerException != null) { - lineNumber = $"#{stackFrame.GetFileLineNumber()}"; + exception += ex.InnerException.GetType().Name; } - } - fingerPrint.Add(logEvent.LoggerName + lineNumber); - - if (logEvent.Exception != null) - { - fingerPrint.Add(logEvent.Exception.GetType().Name); + fingerPrint.Add(exception); } return fingerPrint; } + protected override void Write(LogEventInfo logEvent) { try @@ -111,11 +108,20 @@ namespace NzbDrone.Common.Instrumentation.Sentry { Level = LoggingLevelMap[logEvent.Level], Message = sentryMessage, - Extra = extras + Extra = extras, + Fingerprint = + { + logEvent.Level.ToString(), + logEvent.LoggerName, + logEvent.Message + } }; + if (logEvent.Exception != null) + { + sentryEvent.Fingerprint.Add(logEvent.Exception.GetType().FullName); + } - fingerPrint.ForEach(c => sentryEvent.Fingerprint.Add(c)); sentryEvent.Tags.Add("os_name", Environment.GetEnvironmentVariable("OS_NAME")); sentryEvent.Tags.Add("os_version", Environment.GetEnvironmentVariable("OS_VERSION")); diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index ddddb9333..565e38036 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -182,7 +182,6 @@ -