diff --git a/.env.example b/.env.example index fda8fde..58346bf 100644 --- a/.env.example +++ b/.env.example @@ -29,8 +29,13 @@ ALARIK_ADMIN_USERNAME=heimdall ALARIK_ADMIN_PASSWORD=heimdall123 ALARIK_JWT=ChangeMeAtLeast32BytesLongKeyInProd! -# Sentry / GlitchTip (error tracking) -# SENTRY_DSN=https://key@glitchtip.tordais.cc/6 +# Sentry / GlitchTip (error tracking, performance, structured logs) +# Primary: Sentry__Dsn (ASP.NET Core config convention) +# Fallback: SENTRY_DSN (legacy, for backward compatibility) +# When unset/empty, the Sentry SDK is not initialized (graceful no-op). +Sentry__Dsn=https://db94b9fc2f36425f9eb4b9f6f347dc73@glitchtip.tordais.cc/6 +Sentry__TracesSampleRate=0.05 +Sentry__Release=heimdall@1.0.0 # Agent (set per agent instance) Agent__CentralUrl=http://heimdall:8080 diff --git a/src/Heimdall.Agent/Heimdall.Agent.csproj b/src/Heimdall.Agent/Heimdall.Agent.csproj index 712ad8d..6d3cdb4 100644 --- a/src/Heimdall.Agent/Heimdall.Agent.csproj +++ b/src/Heimdall.Agent/Heimdall.Agent.csproj @@ -19,6 +19,6 @@ - + diff --git a/src/Heimdall.Agent/Program.cs b/src/Heimdall.Agent/Program.cs index 191aa3a..1dc582b 100644 --- a/src/Heimdall.Agent/Program.cs +++ b/src/Heimdall.Agent/Program.cs @@ -1,3 +1,5 @@ +using Sentry.AspNetCore; + using Heimdall.Agent; using Heimdall.Agent.Services; using Microsoft.AspNetCore.Diagnostics.HealthChecks; @@ -14,7 +16,7 @@ var sentryDsn = builder.Configuration["Sentry:Dsn"] ?? Environment.GetEnvironmentVariable("SENTRY_DSN"); if (!string.IsNullOrEmpty(sentryDsn)) { - builder.WebHost.UseSentry(options => + builder.WebHost.UseSentry((SentryAspNetCoreOptions options) => { options.Dsn = sentryDsn; options.MinimumBreadcrumbLevel = LogLevel.Debug; diff --git a/src/Heimdall.Agent/appsettings.Development.json b/src/Heimdall.Agent/appsettings.Development.json index b2dcdb6..bce4d95 100644 --- a/src/Heimdall.Agent/appsettings.Development.json +++ b/src/Heimdall.Agent/appsettings.Development.json @@ -4,5 +4,13 @@ "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" } + }, + "Sentry": { + "Dsn": "https://db94b9fc2f36425f9eb4b9f6f347dc73@glitchtip.tordais.cc/6", + "Environment": "Development", + "TracesSampleRate": 1.0, + "SendDefaultPii": false, + "Debug": true, + "DiagnosticLevel": "debug" } } diff --git a/src/Heimdall.Application/Common/IErrorReporter.cs b/src/Heimdall.Application/Common/IErrorReporter.cs new file mode 100644 index 0000000..d984461 --- /dev/null +++ b/src/Heimdall.Application/Common/IErrorReporter.cs @@ -0,0 +1,29 @@ +namespace Heimdall.Application.Common; + +/// +/// Abstraction for error reporting (implemented by Sentry/GlitchTip in Infrastructure). +/// Application and Domain layers depend on this interface, never on Sentry directly. +/// +public interface IErrorReporter +{ + /// Capture an exception with optional tags. + void CaptureException(Exception exception, IDictionary? tags = null, object? extraData = null); + + /// Capture a message at a given level. + void CaptureMessage(string message, string level = "error"); + + /// Set a tag on the current scope (lasts for the duration of the scope). + void SetTag(string key, string value); + + /// Add a breadcrumb for context. + void AddBreadcrumb(string message, string? category = null, string? level = "info"); + + /// Log a structured log message at Info level. + void LogInfo(string message); + + /// Log a structured log message at Warning level. + void LogWarning(string message); + + /// Log a structured log message at Error level. + void LogError(string message); +} diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index a11143c..fe80eec 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -147,6 +147,9 @@ public static class DependencyInjection client.Timeout = TimeSpan.FromSeconds(10); }); + // Error reporting (Sentry/GlitchTip via IErrorReporter abstraction) + services.AddScoped(); + return services; } } diff --git a/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj b/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj index 5eb198e..691486f 100644 --- a/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj +++ b/src/Heimdall.Infrastructure/Heimdall.Infrastructure.csproj @@ -19,6 +19,7 @@ + diff --git a/src/Heimdall.Infrastructure/Services/SentryErrorReporter.cs b/src/Heimdall.Infrastructure/Services/SentryErrorReporter.cs new file mode 100644 index 0000000..2eb162e --- /dev/null +++ b/src/Heimdall.Infrastructure/Services/SentryErrorReporter.cs @@ -0,0 +1,79 @@ +using Heimdall.Application.Common; +using Sentry; + +namespace Heimdall.Infrastructure.Services; + +/// +/// Sentry/GlitchTip implementation of . +/// SentrySdk is initialized in Program.cs via UseSentry() — this class only +/// consumes the already-initialized SDK, so no init/close concerns here. +/// +internal sealed class SentryErrorReporter : IErrorReporter +{ + public void CaptureException(Exception exception, IDictionary? tags = null, object? extraData = null) + { + SentrySdk.CaptureException(exception, scope => + { + if (tags is not null) + { + foreach (var (key, value) in tags) + scope.SetTag(key, value); + } + + if (extraData is not null) + scope.SetExtra("data", extraData); + }); + } + + public void CaptureMessage(string message, string level = "error") + { + var sentryLevel = level.ToLowerInvariant() switch + { + "debug" => SentryLevel.Debug, + "info" => SentryLevel.Info, + "warning" => SentryLevel.Warning, + "fatal" => SentryLevel.Fatal, + _ => SentryLevel.Error, + }; + + SentrySdk.CaptureMessage(message, sentryLevel); + } + + public void SetTag(string key, string value) + { + SentrySdk.SetTag(key, value); + } + + public void AddBreadcrumb(string message, string? category = null, string? level = "info") + { + var bLevel = level?.ToLowerInvariant() switch + { + "debug" => BreadcrumbLevel.Debug, + "info" => BreadcrumbLevel.Info, + "warning" => BreadcrumbLevel.Warning, + "error" => BreadcrumbLevel.Error, + "critical" => BreadcrumbLevel.Error, + _ => BreadcrumbLevel.Info, + }; + + SentrySdk.AddBreadcrumb(message, category: category, level: bLevel); + } + + public void LogInfo(string message) + { + if (SentrySdk.IsEnabled) + SentrySdk.Logger.LogInfo(message); + } + + public void LogWarning(string message) + { + if (SentrySdk.IsEnabled) + SentrySdk.Logger.LogWarning(message); + } + + public void LogError(string message) + { + if (SentrySdk.IsEnabled) + SentrySdk.Logger.LogError(message); + } +} diff --git a/src/Heimdall.Web/Heimdall.Web.csproj b/src/Heimdall.Web/Heimdall.Web.csproj index 9969065..4076d5e 100644 --- a/src/Heimdall.Web/Heimdall.Web.csproj +++ b/src/Heimdall.Web/Heimdall.Web.csproj @@ -24,7 +24,7 @@ - + diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index a10531c..4b99e31 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -1,3 +1,5 @@ +using Sentry.AspNetCore; + using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; @@ -217,7 +219,7 @@ public partial class Program { ?? Environment.GetEnvironmentVariable("SENTRY_DSN"); if (!string.IsNullOrEmpty(sentryDsn)) { - builder.WebHost.UseSentry(options => + builder.WebHost.UseSentry((SentryAspNetCoreOptions options) => { options.Dsn = sentryDsn; @@ -238,7 +240,8 @@ public partial class Program { // GlitchTip counts every transaction as an event. // 0.05 = 5% sample = good signal-to-noise in production. options.TracesSampleRate = double.Parse( - builder.Configuration["Sentry:TracesSampleRate"] ?? "0.05"); + builder.Configuration["Sentry:TracesSampleRate"] ?? "0.05", + System.Globalization.CultureInfo.InvariantCulture); // ── Request body capture ──────────────────────────────── options.MaxRequestBodySize = Sentry.Extensibility.RequestSize.Small; @@ -271,11 +274,11 @@ public partial class Program { // Remove sensitive extras if (sentryEvent.Extra?.ContainsKey("password") == true) - sentryEvent.Extra.Remove("password"); + (sentryEvent.Extra as IDictionary)?.Remove("password"); if (sentryEvent.Extra?.ContainsKey("token") == true) - sentryEvent.Extra.Remove("token"); + (sentryEvent.Extra as IDictionary)?.Remove("token"); if (sentryEvent.Extra?.ContainsKey("secret") == true) - sentryEvent.Extra.Remove("secret"); + (sentryEvent.Extra as IDictionary)?.Remove("secret"); return sentryEvent; }); diff --git a/src/Heimdall.Web/appsettings.Development.json b/src/Heimdall.Web/appsettings.Development.json index 0c208ae..e2c27e0 100644 --- a/src/Heimdall.Web/appsettings.Development.json +++ b/src/Heimdall.Web/appsettings.Development.json @@ -4,5 +4,13 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "Sentry": { + "Dsn": "https://db94b9fc2f36425f9eb4b9f6f347dc73@glitchtip.tordais.cc/6", + "Environment": "Development", + "TracesSampleRate": 1.0, + "SendDefaultPii": true, + "Debug": true, + "DiagnosticLevel": "debug" } }