mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-05 20:42:19 -07:00
Update Sentry SDK add features
Co-authored-by: Stefan Jandl <reg@bitfox.at> (cherry picked from commit 6377c688fc7b35749d608bf62796446bb5bcb11b)
This commit is contained in:
parent
e6388cab94
commit
0e19c03e9a
5 changed files with 74 additions and 13 deletions
|
@ -99,6 +99,35 @@
|
||||||
<RootNamespace Condition="'$(LidarrProject)'=='true'">$(MSBuildProjectName.Replace('Lidarr','NzbDrone'))</RootNamespace>
|
<RootNamespace Condition="'$(LidarrProject)'=='true'">$(MSBuildProjectName.Replace('Lidarr','NzbDrone'))</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(TestProject)'!='true'">
|
||||||
|
<!-- Annotates .NET assemblies with repository information including SHA -->
|
||||||
|
<!-- Sentry uses this to link directly to GitHub at the exact version/file/line -->
|
||||||
|
<!-- This is built-in on .NET 8 and can be removed once the project is updated -->
|
||||||
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Sentry specific configuration: Only in Release mode -->
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
||||||
|
<!-- https://docs.sentry.io/platforms/dotnet/configuration/msbuild/ -->
|
||||||
|
<!-- OrgSlug, ProjectSlug and AuthToken are required.
|
||||||
|
They can be set below, via argument to 'msbuild -p:' or environment variable -->
|
||||||
|
<SentryOrg></SentryOrg>
|
||||||
|
<SentryProject></SentryProject>
|
||||||
|
<SentryUrl></SentryUrl> <!-- If empty, assumed to be sentry.io -->
|
||||||
|
<SentryAuthToken></SentryAuthToken> <!-- Use env var instead: SENTRY_AUTH_TOKEN -->
|
||||||
|
|
||||||
|
<!-- Upload PDBs to Sentry, enabling stack traces with line numbers and file paths
|
||||||
|
without the need to deploy the application with PDBs -->
|
||||||
|
<SentryUploadSymbols>true</SentryUploadSymbols>
|
||||||
|
|
||||||
|
<!-- Source Link settings -->
|
||||||
|
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#publishrepositoryurl -->
|
||||||
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
|
<!-- Embeds all source code in the respective PDB. This can make it a bit bigger but since it'll be uploaded
|
||||||
|
to Sentry and not distributed to run on the server, it helps debug crashes while making releases smaller -->
|
||||||
|
<EmbedAllSources>true</EmbedAllSources>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<!-- Standard testing packages -->
|
<!-- Standard testing packages -->
|
||||||
<ItemGroup Condition="'$(TestProject)'=='true'">
|
<ItemGroup Condition="'$(TestProject)'=='true'">
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Common.Instrumentation.Sentry;
|
using NzbDrone.Common.Instrumentation.Sentry;
|
||||||
using NzbDrone.Test.Common;
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
|
@ -27,7 +28,7 @@ namespace NzbDrone.Common.Test.InstrumentationTests
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
_subject = new SentryTarget("https://aaaaaaaaaaaaaaaaaaaaaaaaaa@sentry.io/111111");
|
_subject = new SentryTarget("https://aaaaaaaaaaaaaaaaaaaaaaaaaa@sentry.io/111111", Mocker.GetMock<IAppFolderInfo>().Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
private LogEventInfo GivenLogEvent(LogLevel level, Exception ex, string message)
|
private LogEventInfo GivenLogEvent(LogLevel level, Exception ex, string message)
|
||||||
|
|
|
@ -44,7 +44,7 @@ namespace NzbDrone.Common.Instrumentation
|
||||||
RegisterDebugger();
|
RegisterDebugger();
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisterSentry(updateApp);
|
RegisterSentry(updateApp, appFolderInfo);
|
||||||
|
|
||||||
if (updateApp)
|
if (updateApp)
|
||||||
{
|
{
|
||||||
|
@ -65,7 +65,7 @@ namespace NzbDrone.Common.Instrumentation
|
||||||
LogManager.ReconfigExistingLoggers();
|
LogManager.ReconfigExistingLoggers();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterSentry(bool updateClient)
|
private static void RegisterSentry(bool updateClient, IAppFolderInfo appFolderInfo)
|
||||||
{
|
{
|
||||||
string dsn;
|
string dsn;
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ namespace NzbDrone.Common.Instrumentation
|
||||||
: "https://0522924d625c497f86fc2a1b22aaf21d@sentry.servarr.com/16";
|
: "https://0522924d625c497f86fc2a1b22aaf21d@sentry.servarr.com/16";
|
||||||
}
|
}
|
||||||
|
|
||||||
var target = new SentryTarget(dsn)
|
var target = new SentryTarget(dsn, appFolderInfo)
|
||||||
{
|
{
|
||||||
Name = "sentryTarget",
|
Name = "sentryTarget",
|
||||||
Layout = "${message}"
|
Layout = "${message}"
|
||||||
|
|
|
@ -9,6 +9,7 @@ using NLog;
|
||||||
using NLog.Common;
|
using NLog.Common;
|
||||||
using NLog.Targets;
|
using NLog.Targets;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
using Sentry;
|
using Sentry;
|
||||||
|
|
||||||
namespace NzbDrone.Common.Instrumentation.Sentry
|
namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
|
@ -99,7 +100,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
public bool FilterEvents { get; set; }
|
public bool FilterEvents { get; set; }
|
||||||
public bool SentryEnabled { get; set; }
|
public bool SentryEnabled { get; set; }
|
||||||
|
|
||||||
public SentryTarget(string dsn)
|
public SentryTarget(string dsn, IAppFolderInfo appFolderInfo)
|
||||||
{
|
{
|
||||||
_sdk = SentrySdk.Init(o =>
|
_sdk = SentrySdk.Init(o =>
|
||||||
{
|
{
|
||||||
|
@ -107,9 +108,33 @@ namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
o.AttachStacktrace = true;
|
o.AttachStacktrace = true;
|
||||||
o.MaxBreadcrumbs = 200;
|
o.MaxBreadcrumbs = 200;
|
||||||
o.Release = $"{BuildInfo.AppName}@{BuildInfo.Release}";
|
o.Release = $"{BuildInfo.AppName}@{BuildInfo.Release}";
|
||||||
o.BeforeSend = x => SentryCleanser.CleanseEvent(x);
|
o.SetBeforeSend(x => SentryCleanser.CleanseEvent(x));
|
||||||
o.BeforeBreadcrumb = x => SentryCleanser.CleanseBreadcrumb(x);
|
o.SetBeforeBreadcrumb(x => SentryCleanser.CleanseBreadcrumb(x));
|
||||||
o.Environment = BuildInfo.Branch;
|
o.Environment = BuildInfo.Branch;
|
||||||
|
|
||||||
|
// Crash free run statistics (sends a ping for healthy and for crashes sessions)
|
||||||
|
o.AutoSessionTracking = true;
|
||||||
|
|
||||||
|
// Caches files in the event device is offline
|
||||||
|
// Sentry creates a 'sentry' sub directory, no need to concat here
|
||||||
|
o.CacheDirectoryPath = appFolderInfo.GetAppDataPath();
|
||||||
|
|
||||||
|
// default environment is production
|
||||||
|
if (!RuntimeInfo.IsProduction)
|
||||||
|
{
|
||||||
|
if (RuntimeInfo.IsDevelopment)
|
||||||
|
{
|
||||||
|
o.Environment = "development";
|
||||||
|
}
|
||||||
|
else if (RuntimeInfo.IsTesting)
|
||||||
|
{
|
||||||
|
o.Environment = "testing";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
o.Environment = "other";
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
InitializeScope();
|
InitializeScope();
|
||||||
|
@ -127,7 +152,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
{
|
{
|
||||||
SentrySdk.ConfigureScope(scope =>
|
SentrySdk.ConfigureScope(scope =>
|
||||||
{
|
{
|
||||||
scope.User = new User
|
scope.User = new SentryUser
|
||||||
{
|
{
|
||||||
Id = HashUtil.AnonymousToken()
|
Id = HashUtil.AnonymousToken()
|
||||||
};
|
};
|
||||||
|
@ -169,9 +194,7 @@ namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
|
|
||||||
private void OnError(Exception ex)
|
private void OnError(Exception ex)
|
||||||
{
|
{
|
||||||
var webException = ex as WebException;
|
if (ex is WebException webException)
|
||||||
|
|
||||||
if (webException != null)
|
|
||||||
{
|
{
|
||||||
var response = webException.Response as HttpWebResponse;
|
var response = webException.Response as HttpWebResponse;
|
||||||
var statusCode = response?.StatusCode;
|
var statusCode = response?.StatusCode;
|
||||||
|
@ -290,13 +313,21 @@ namespace NzbDrone.Common.Instrumentation.Sentry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var level = LoggingLevelMap[logEvent.Level];
|
||||||
var sentryEvent = new SentryEvent(logEvent.Exception)
|
var sentryEvent = new SentryEvent(logEvent.Exception)
|
||||||
{
|
{
|
||||||
Level = LoggingLevelMap[logEvent.Level],
|
Level = level,
|
||||||
Logger = logEvent.LoggerName,
|
Logger = logEvent.LoggerName,
|
||||||
Message = logEvent.FormattedMessage
|
Message = logEvent.FormattedMessage
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (level is SentryLevel.Fatal && logEvent.Exception is not null)
|
||||||
|
{
|
||||||
|
// Usages of 'fatal' here indicates the process will crash. In Sentry this is represented with
|
||||||
|
// the 'unhandled' exception flag
|
||||||
|
logEvent.Exception.SetSentryMechanism("Logger.Fatal", "Logger.Fatal was called", false);
|
||||||
|
}
|
||||||
|
|
||||||
sentryEvent.SetExtras(extras);
|
sentryEvent.SetExtras(extras);
|
||||||
sentryEvent.SetFingerprint(fingerPrint);
|
sentryEvent.SetFingerprint(fingerPrint);
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<PackageReference Include="NLog" Version="5.3.4" />
|
<PackageReference Include="NLog" Version="5.3.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" />
|
||||||
<PackageReference Include="NLog.Layouts.ClefJsonLayout" Version="1.0.2" />
|
<PackageReference Include="NLog.Layouts.ClefJsonLayout" Version="1.0.2" />
|
||||||
<PackageReference Include="Sentry" Version="3.25.0" />
|
<PackageReference Include="Sentry" Version="4.0.2" />
|
||||||
<PackageReference Include="SharpZipLib" Version="1.4.2" />
|
<PackageReference Include="SharpZipLib" Version="1.4.2" />
|
||||||
<PackageReference Include="System.IO.Abstractions" Version="17.0.24" />
|
<PackageReference Include="System.IO.Abstractions" Version="17.0.24" />
|
||||||
<PackageReference Include="System.Text.Json" Version="6.0.10" />
|
<PackageReference Include="System.Text.Json" Version="6.0.10" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue