Use line number instead of message for sentry fingerprint

This commit is contained in:
Keivan Beigi 2017-01-05 14:00:25 -08:00
parent d61976251e
commit 76a7d4f866
4 changed files with 58 additions and 13 deletions

View File

@ -33,6 +33,8 @@ namespace NzbDrone.Common.Instrumentation
GlobalExceptionHandlers.Register();
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("populatestacktrace", typeof(PopulateStackTraceRenderer));
var appFolderInfo = new AppFolderInfo(startupContext);
if (Debugger.IsAttached)
@ -106,10 +108,11 @@ namespace NzbDrone.Common.Instrumentation
var target = new SentryTarget(dsn)
{
Name = "sentryTarget",
Layout = "${message}"
Layout = "${message}${populatestacktrace}"
};
var loggingRule = new LoggingRule("*", updateClient ? LogLevel.Trace : LogLevel.Error, target);
LogManager.Configuration.AddTarget("sentryTarget", target);
LogManager.Configuration.LoggingRules.Add(loggingRule);
}

View File

@ -0,0 +1,20 @@
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.
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using NLog;
@ -54,6 +55,35 @@ namespace NzbDrone.Common.Instrumentation.Sentry
_client.Tags.Add("version", BuildInfo.Version.ToString());
}
private List<string> GetFingerPrint(LogEventInfo logEvent)
{
var fingerPrint = new List<string>
{
logEvent.Level.Ordinal.ToString(),
};
var lineNumber = "";
if (logEvent.StackTrace != null)
{
var stackFrame = logEvent.StackTrace.GetFrame(logEvent.UserStackFrameNumber);
if (stackFrame != null)
{
lineNumber = $"#{stackFrame.GetFileLineNumber()}";
}
}
fingerPrint.Add(logEvent.LoggerName + lineNumber);
if (logEvent.Exception != null)
{
fingerPrint.Add(logEvent.Exception.GetType().Name);
}
return fingerPrint;
}
protected override void Write(LogEventInfo logEvent)
{
try
@ -64,7 +94,6 @@ namespace NzbDrone.Common.Instrumentation.Sentry
return;
}
var extras = logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString());
_client.Logger = logEvent.LoggerName;
@ -73,19 +102,11 @@ namespace NzbDrone.Common.Instrumentation.Sentry
{
Level = LoggingLevelMap[logEvent.Level],
Message = sentryMessage,
Extra = extras,
Fingerprint =
{
logEvent.Level.ToString(),
logEvent.LoggerName,
logEvent.Message
}
Extra = extras
};
if (logEvent.Exception != null)
{
sentryEvent.Fingerprint.Add(logEvent.Exception.GetType().FullName);
}
var fingerPrint = GetFingerPrint(logEvent);
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"));

View File

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