Smaller sentry payload, send machine name as user name

This commit is contained in:
Keivan Beigi 2017-01-05 17:20:35 -08:00
parent cd7e208efa
commit 0bdc137093
5 changed files with 113 additions and 14 deletions

View File

@ -0,0 +1,13 @@
using System;
using SharpRaven.Data;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class MachineNameUserFactory : ISentryUserFactory
{
public SentryUser Create()
{
return new SentryUser(Environment.MachineName);
}
}
}

View File

@ -11,23 +11,11 @@ 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},
@ -42,7 +30,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
public SentryTarget(string dsn)
{
_client = new RavenClient(new Dsn(dsn), new JsonPacketFactory(), new SentryRequestFactory(), new SentryUserFactory())
_client = new RavenClient(new Dsn(dsn), new SonarrJsonPacketFactory(), new SentryRequestFactory(), new MachineNameUserFactory())
{
Compression = true,
Environment = RuntimeInfo.IsProduction ? "production" : "development",
@ -103,7 +91,9 @@ namespace NzbDrone.Common.Instrumentation.Sentry
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 sentryMessage = new SentryMessage(logEvent.Message, logEvent.Parameters);
var sentryEvent = new SentryEvent(logEvent.Exception)
{
Level = LoggingLevelMap[logEvent.Level],

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using SharpRaven.Data;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class SonarrJsonPacketFactory : IJsonPacketFactory
{
private static string ShortenPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
var index = path.IndexOf("\\src\\", StringComparison.Ordinal);
if (index <= 0)
{
return path;
}
return path.Substring(index + "\\src".Length);
}
public JsonPacket Create(string project, SentryEvent @event)
{
var packet = new SonarrSentryPacket(project, @event);
try
{
foreach (var exception in packet.Exceptions)
{
foreach (var frame in exception.Stacktrace.Frames)
{
frame.Filename = ShortenPath(frame.Filename);
}
}
}
catch (Exception)
{
}
return packet;
}
[Obsolete]
public JsonPacket Create(string project, SentryMessage message, ErrorLevel level = ErrorLevel.Info, IDictionary<string, string> tags = null,
string[] fingerprint = null, object extra = null)
{
throw new NotImplementedException();
}
[Obsolete]
public JsonPacket Create(string project, Exception exception, SentryMessage message = null, ErrorLevel level = ErrorLevel.Error,
IDictionary<string, string> tags = null, string[] fingerprint = null, object extra = null)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,29 @@
using Newtonsoft.Json;
using SharpRaven.Data;
namespace NzbDrone.Common.Instrumentation.Sentry
{
public class SonarrSentryPacket : JsonPacket
{
private readonly JsonSerializerSettings _setting;
public SonarrSentryPacket(string project, SentryEvent @event) :
base(project, @event)
{
_setting = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore
};
}
public override string ToString(Formatting formatting)
{
return JsonConvert.SerializeObject(this, formatting, _setting);
}
public override string ToString()
{
return ToString(Formatting.None);
}
}
}

View File

@ -184,6 +184,9 @@
<Compile Include="Instrumentation\NzbDroneLogger.cs" />
<Compile Include="Instrumentation\Sentry\SentryDebounce.cs" />
<Compile Include="Instrumentation\Sentry\SentryTarget.cs" />
<Compile Include="Instrumentation\Sentry\MachineNameUserFactory.cs" />
<Compile Include="Instrumentation\Sentry\SonarrJsonPacketFactory.cs" />
<Compile Include="Instrumentation\Sentry\SonarrSentryPacket.cs" />
<Compile Include="Instrumentation\VersionLayoutRenderer.cs" />
<Compile Include="Extensions\LevenstheinExtensions.cs" />
<Compile Include="Messaging\IEvent.cs" />