Add Sentry error reporting

This commit is contained in:
2026-08-26 17:08:50 +00:00
parent a92293eefa
commit 1b42150ed6
20 changed files with 86 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ public sealed class ServiceSettings
public SqlSettings Sql { get; init; } = new();
public SentrySettings Sentry { get; init; } = new();
public DiagnosticsSettings Diagnostics { get; init; } = new();
public static ServiceSettings Load(string path)
@@ -137,6 +139,11 @@ public sealed class SqlSettings
public int CommandTimeoutSeconds { get; init; } = 30;
}
public sealed class SentrySettings
{
public string? Dsn { get; init; }
}
public sealed class DiagnosticsSettings
{
public string LogDirectory { get; init; } = "daemon";

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Sentry;
using SmartB2B.Enova.Contracts;
using SmartB2B.Enova.Service.Rpc;
@@ -73,6 +74,11 @@ public sealed class PlaceOrderHandler
}
catch (EnovaOperationException exception)
{
SentrySdk.CaptureException(exception, scope =>
{
scope.SetTag("rpc.procedure", "eu.smartb2b.place_order");
scope.SetTag("wamp.error_uri", exception.ErrorUri);
});
throw WampErrorFactory.FromEnova(exception);
}

View File

@@ -5,6 +5,7 @@ using SmartB2B.Enova.Service.Orders;
using SmartB2B.Enova.Service.Rpc;
using SmartB2B.Enova.Service.Runtime;
using SmartB2B.Enova.Service.Sql;
using Sentry;
using WampSharp.V2.Rpc;
namespace SmartB2B.Enova.Service;
@@ -13,6 +14,7 @@ internal static class Program
{
public static async Task<int> Main(string[] args)
{
IDisposable? sentry = null;
var cancellation = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
@@ -25,6 +27,7 @@ internal static class Program
{
var commandLine = CommandLineOptions.Parse(args);
var settings = ServiceSettings.Load(commandLine.ConfigurationPath);
sentry = InitializeSentry(settings);
var runtime = settings.ResolveRuntimeSettings();
using var adapterLoader = new EnovaAdapterLoader(runtime.EnovaInstallationPath);
@@ -98,8 +101,34 @@ internal static class Program
catch (Exception exception)
{
Console.Error.WriteLine($"Błąd krytyczny: {exception}");
SentrySdk.CaptureException(exception);
await SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
return 1;
}
finally
{
sentry?.Dispose();
}
}
private static IDisposable? InitializeSentry(ServiceSettings settings)
{
if (string.IsNullOrWhiteSpace(settings.Sentry.Dsn))
{
Console.WriteLine("Sentry wyłączone (brak parametru sentry.dsn).");
return null;
}
var sentry = SentrySdk.Init(options =>
{
options.Dsn = settings.Sentry.Dsn;
options.IsGlobalModeEnabled = true;
options.Release = typeof(Program).Assembly.GetName().Version?.ToString();
});
SentrySdk.ConfigureScope(scope => scope.SetTag("portal", settings.Portal));
Console.WriteLine("Raportowanie błędów do Sentry włączone.");
return sentry;
}
}

View File

@@ -1,6 +1,7 @@
using WampSharp.Core.Serialization;
using WampSharp.V2.Core.Contracts;
using WampSharp.V2.Rpc;
using Sentry;
namespace SmartB2B.Enova.Service.Rpc;
@@ -99,6 +100,9 @@ public sealed class DelegateRpcOperation : IWampRpcOperation
catch (Exception exception)
{
Console.Error.WriteLine($"Nieobsłużony błąd procedury {Procedure}: {exception}");
SentrySdk.CaptureException(
exception,
scope => scope.SetTag("rpc.procedure", Procedure));
var details = formatter.Serialize(new Dictionary<string, object>());
var keywordArguments = formatter.Serialize(new Dictionary<string, object?>
{

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="../SmartB2B.Enova.Contracts/SmartB2B.Enova.Contracts.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Sentry" Version="6.9.0" />
<PackageReference Include="WampSharp.Default.Client" Version="23.8.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,5 +1,6 @@
using System.Data.Common;
using Newtonsoft.Json;
using Sentry;
using SmartB2B.Enova.Service.Rpc;
namespace SmartB2B.Enova.Service.Sql;
@@ -98,6 +99,11 @@ public sealed class SqlRawHandler
var code = exception.GetType().GetProperty("Number")?.GetValue(exception)
?? exception.ErrorCode;
Console.Error.WriteLine($"Błąd SQL {code}: {exception.Message}");
SentrySdk.CaptureException(exception, scope =>
{
scope.SetTag("rpc.procedure", "eu.smartb2b.sql_raw");
scope.SetExtra("sql.error_code", code);
});
throw WampErrorFactory.Create(
"eu.smartb2b.sql_error",
exception.Message,

View File

@@ -17,6 +17,9 @@
"connectionString": "Server=...;Database=...;User ID=...;Password=...;TrustServerCertificate=True",
"commandTimeoutSeconds": 30
},
"sentry": {
"dsn": "https://9ae7cbe5e29f4a85b2a748a78e55a868@glitchtip.glebocka.synology.me/10"
},
"diagnostics": {
"logDirectory": "daemon",
"logPrefix": "smartb2bsync-enova"