Added Sentry error reporting

This commit is contained in:
Keivan Beigi 2017-01-04 00:53:09 -08:00
parent 61579cfb7e
commit 053c730799
7 changed files with 140 additions and 8 deletions

View File

@ -82,6 +82,9 @@ namespace NzbDrone.Common.EnvironmentInfo
Name = Os.ToString();
FullName = Name;
}
Environment.SetEnvironmentVariable("OS_NAME", Name);
Environment.SetEnvironmentVariable("OS_VERSION", Version);
}
}

View File

@ -7,6 +7,7 @@ using NLog.Config;
using NLog.Targets;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Sentry;
namespace NzbDrone.Common.Instrumentation
{
@ -38,6 +39,8 @@ namespace NzbDrone.Common.Instrumentation
RegisterDebugger();
}
RegisterSentry(updateApp);
if (updateApp)
{
RegisterUpdateFile(appFolderInfo);
@ -69,6 +72,35 @@ namespace NzbDrone.Common.Instrumentation
LogManager.Configuration.LoggingRules.Add(loggingRule);
}
private static void RegisterSentry(bool updateClient)
{
string dsn;
if (updateClient)
{
dsn = RuntimeInfo.IsProduction
? "https://b85aa82c65b84b0e99e3b7c281438357:392b5bc007974147a922c5d841c47cf9@sentry.sonarr.tv/11"
: "https://6168f0946aba4e60ac23e469ac08eac5:bd59e8454ccc454ea27a90cff1f814ca@sentry.sonarr.tv/9";
}
else
{
dsn = RuntimeInfo.IsProduction
? "https://fed3f47e8bea4527831e96edfa02d495:8ed11e4878614d8e87b1e1b15d890dc9@sentry.sonarr.tv/8"
: "https://4ee3580e01d8407c96a7430fbc953512:5f2d07227a0b4fde99dea07041a3ff93@sentry.sonarr.tv/10";
}
var target = new SentryTarget(dsn)
{
Name = "sentryTarget",
Layout = "${message}"
};
var loggingRule = new LoggingRule("*", updateClient ? LogLevel.Trace : LogLevel.Error, target);
LogManager.Configuration.AddTarget("logentries", target);
LogManager.Configuration.LoggingRules.Add(loggingRule);
}
private static void RegisterDebugger()
{
DebuggerTarget target = new DebuggerTarget();

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NLog;
using NLog.Common;
using NLog.Targets;
using NzbDrone.Common.EnvironmentInfo;
using SharpRaven;
using SharpRaven.Data;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class SentryUserFactory : ISentryUserFactory
{
public SentryUser Create()
{
return new SentryUser((string)null);
}
}
[Target("Sentry")]
public class SentryTarget : TargetWithLayout
{
private readonly RavenClient _client;
/// <summary>
/// Map of NLog log levels to Raven/Sentry log levels
/// </summary>
private static readonly IDictionary<LogLevel, ErrorLevel> LoggingLevelMap = new Dictionary<LogLevel, ErrorLevel>
{
{LogLevel.Debug, ErrorLevel.Debug},
{LogLevel.Error, ErrorLevel.Error},
{LogLevel.Fatal, ErrorLevel.Fatal},
{LogLevel.Info, ErrorLevel.Info},
{LogLevel.Trace, ErrorLevel.Debug},
{LogLevel.Warn, ErrorLevel.Warning},
};
public SentryTarget(string dsn)
{
_client = new RavenClient(new Dsn(dsn), new JsonPacketFactory(), new SentryRequestFactory(), new SentryUserFactory())
{
Compression = true,
Environment = RuntimeInfo.IsProduction ? "production" : "development",
Release = BuildInfo.Version.ToString(),
};
_client.Tags.Add("osfamily", OsInfo.Os.ToString());
_client.Tags.Add("runtime", PlatformInfo.Platform.ToString().ToLower());
_client.Tags.Add("culture", Thread.CurrentThread.CurrentCulture.Name);
}
protected override void Write(LogEventInfo logEvent)
{
try
{
// don't report non-critical events without exceptions
if (logEvent.Exception == null && logEvent.Level < LogLevel.Warn)
{
return;
}
var extras = logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString());
_client.Logger = logEvent.LoggerName;
var sentryMessage = new SentryMessage(Layout.Render(logEvent));
var sentryEvent = new SentryEvent(logEvent.Exception)
{
Level = LoggingLevelMap[logEvent.Level],
Message = sentryMessage,
Extra = extras,
};
sentryEvent.Tags.Add("os_name", Environment.GetEnvironmentVariable("OS_NAME"));
sentryEvent.Tags.Add("os_version", Environment.GetEnvironmentVariable("OS_VERSION"));
sentryEvent.Tags.Add("runtime_version", Environment.GetEnvironmentVariable("RUNTIME_VERSION"));
_client.Capture(sentryEvent);
}
catch (Exception e)
{
InternalLogger.Error("Unable to send Sentry request: {0}", e.Message);
}
}
}
}

View File

@ -51,6 +51,10 @@
<HintPath>..\packages\DotNet4.SocksProxy.1.3.2.0\lib\net40\Org.Mentalis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SharpRaven, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SharpRaven.2.1.0\lib\net40\SharpRaven.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SocksWebProxy, Version=1.3.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DotNet4.SocksProxy.1.3.2.0\lib\net40\SocksWebProxy.dll</HintPath>
<Private>True</Private>
@ -178,6 +182,7 @@
<Compile Include="Instrumentation\LogEventExtensions.cs" />
<Compile Include="Instrumentation\NzbDroneFileTarget.cs" />
<Compile Include="Instrumentation\NzbDroneLogger.cs" />
<Compile Include="Instrumentation\Sentry\SentryTarget.cs" />
<Compile Include="Instrumentation\VersionLayoutRenderer.cs" />
<Compile Include="Extensions\LevenstheinExtensions.cs" />
<Compile Include="Messaging\IEvent.cs" />

View File

@ -4,4 +4,5 @@
<package id="ICSharpCode.SharpZipLib.Patched" version="0.86.5" targetFramework="net40" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />
<package id="NLog" version="4.4.1" targetFramework="net40" />
<package id="SharpRaven" version="2.1.0" targetFramework="net40" />
</packages>

View File

@ -30,6 +30,7 @@ namespace NzbDrone.Mono.EnvironmentInfo
if (versionMatch.Success)
{
runTimeVersion = new Version(versionMatch.Groups["version"].Value);
Environment.SetEnvironmentVariable("RUNTIME_VERSION", runTimeVersion.ToString());
}
}
}
@ -39,7 +40,6 @@ namespace NzbDrone.Mono.EnvironmentInfo
logger.Error(ex, "Unable to get mono version: " + ex.Message);
}
Version = runTimeVersion;
}
}

View File

@ -8,7 +8,9 @@ namespace NzbDrone.Windows.EnvironmentInfo
{
public DotNetPlatformInfo()
{
Version = GetFrameworkVersion();
var version = GetFrameworkVersion();
Environment.SetEnvironmentVariable("RUNTIME_VERSION", version.ToString());
Version = version;
}
public override Version Version { get; }
@ -27,27 +29,27 @@ namespace NzbDrone.Windows.EnvironmentInfo
if (releaseKey >= 394802)
{
return new Version(4,6,2);
return new Version(4, 6, 2);
}
if (releaseKey >= 394254)
{
return new Version(4,6,1);
return new Version(4, 6, 1);
}
if (releaseKey >= 393295)
{
return new Version(4,6);
return new Version(4, 6);
}
if (releaseKey >= 379893)
{
return new Version(4,5,2);
return new Version(4, 5, 2);
}
if (releaseKey >= 378675)
{
return new Version(4,5,1);
return new Version(4, 5, 1);
}
if (releaseKey >= 378389)
{
return new Version(4,5);
return new Version(4, 5);
}
return new Version(4, 0);