From 0bdc1370937a35377925bb2a07136ab9b8533acf Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Thu, 5 Jan 2017 17:20:35 -0800 Subject: [PATCH] Smaller sentry payload, send machine name as user name --- .../Sentry/MachineNameUserFactory.cs | 13 ++++ .../Instrumentation/Sentry/SentryTarget.cs | 18 ++---- .../Sentry/SonarrJsonPacketFactory.cs | 64 +++++++++++++++++++ .../Sentry/SonarrSentryPacket.cs | 29 +++++++++ src/NzbDrone.Common/NzbDrone.Common.csproj | 3 + 5 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 src/NzbDrone.Common/Instrumentation/Sentry/MachineNameUserFactory.cs create mode 100644 src/NzbDrone.Common/Instrumentation/Sentry/SonarrJsonPacketFactory.cs create mode 100644 src/NzbDrone.Common/Instrumentation/Sentry/SonarrSentryPacket.cs diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/MachineNameUserFactory.cs b/src/NzbDrone.Common/Instrumentation/Sentry/MachineNameUserFactory.cs new file mode 100644 index 000000000..b9c92c880 --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/Sentry/MachineNameUserFactory.cs @@ -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); + } + } +} \ 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 a15ec5136..8e3f7b6f7 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs @@ -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; - /// - /// Map of NLog log levels to Raven/Sentry log levels - /// private static readonly IDictionary LoggingLevelMap = new Dictionary { {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], diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SonarrJsonPacketFactory.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SonarrJsonPacketFactory.cs new file mode 100644 index 000000000..fb639fb2f --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SonarrJsonPacketFactory.cs @@ -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 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 tags = null, string[] fingerprint = null, object extra = null) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SonarrSentryPacket.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SonarrSentryPacket.cs new file mode 100644 index 000000000..8dbfba818 --- /dev/null +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SonarrSentryPacket.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index 565e38036..4346a2610 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -184,6 +184,9 @@ + + +