Skip to content

Commit

Permalink
feat(sentry): add sentry module
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Apr 21, 2023
1 parent edcbba7 commit c92841e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sitko.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitko.Core.Repository.Grpc"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitko.Core.Repository.Grpc.Tests", "tests\Sitko.Core.Repository.Grpc.Tests\Sitko.Core.Repository.Grpc.Tests.csproj", "{5D54C212-07F4-46B6-B86D-EC9D7641ED19}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sitko.Core.Sentry", "src\Sitko.Core.Sentry\Sitko.Core.Sentry.csproj", "{5D438687-8576-425A-A8A9-A80B893DCE0C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1483,6 +1485,18 @@ Global
{5D54C212-07F4-46B6-B86D-EC9D7641ED19}.Release|x64.Build.0 = Release|Any CPU
{5D54C212-07F4-46B6-B86D-EC9D7641ED19}.Release|x86.ActiveCfg = Release|Any CPU
{5D54C212-07F4-46B6-B86D-EC9D7641ED19}.Release|x86.Build.0 = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|x64.ActiveCfg = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|x64.Build.0 = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|x86.ActiveCfg = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Debug|x86.Build.0 = Debug|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|Any CPU.Build.0 = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|x64.ActiveCfg = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|x64.Build.0 = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|x86.ActiveCfg = Release|Any CPU
{5D438687-8576-425A-A8A9-A80B893DCE0C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6677865F-C349-4D25-9E19-3DEC43E92DD5} = {109B331E-71B9-483C-8FC1-13B10C92A7F1}
Expand Down Expand Up @@ -1588,5 +1602,6 @@ Global
{D310CECD-6BBF-4C7B-AC46-48BAD632B8B2} = {F626F7B7-70BB-4D3B-A803-68ADA8BA4234}
{910855FF-013F-4343-B4D1-27721CB65822} = {109B331E-71B9-483C-8FC1-13B10C92A7F1}
{5D54C212-07F4-46B6-B86D-EC9D7641ED19} = {10C46F0D-1B13-447E-AD22-F01DDB5A79FD}
{5D438687-8576-425A-A8A9-A80B893DCE0C} = {109B331E-71B9-483C-8FC1-13B10C92A7F1}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0"/>
<PackageVersion Include="Duende.AccessTokenManagement.OpenIdConnect" Version="1.1.0"/>
<PackageVersion Include="Sentry.AspNetCore" Version="3.30.0"/>
</ItemGroup>
<!--Email libraries-->
<ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Sitko.Core.Sentry/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Sitko.Core.App;

namespace Sitko.Core.Sentry;

public static class ApplicationExtensions
{
public static Application AddSentry(this Application application,
Action<IApplicationContext, SentryModuleOptions> configure, string? optionsKey = null) =>
application.AddModule<SentryModule, SentryModuleOptions>(configure, optionsKey);

public static Application AddSentry(this Application application,
Action<SentryModuleOptions>? configure = null, string? optionsKey = null) =>
application.AddModule<SentryModule, SentryModuleOptions>(configure, optionsKey);
}
27 changes: 27 additions & 0 deletions src/Sitko.Core.Sentry/SentryModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Sentry.AspNetCore;
using Sitko.Core.App;

namespace Sitko.Core.Sentry;

public class SentryModule : BaseApplicationModule<SentryModuleOptions>,
IHostBuilderModule<SentryModuleOptions>
{
public override string OptionsKey => "Sentry";

public void ConfigureHostBuilder(IApplicationContext context, IHostBuilder hostBuilder, SentryModuleOptions startupOptions) =>
hostBuilder.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseSentry(builder =>
{
startupOptions.ConfigureSentry?.Invoke(context, builder, startupOptions);
builder.AddSentryOptions(o =>
{
o.Dsn = startupOptions.Dsn;
o.Debug = startupOptions.EnableDebug;
o.TracesSampleRate = startupOptions.TracesSampleRate;
});
});
});
}
22 changes: 22 additions & 0 deletions src/Sitko.Core.Sentry/SentryModuleOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FluentValidation;
using Sentry.AspNetCore;
using Sitko.Core.App;

namespace Sitko.Core.Sentry;

public class SentryModuleOptions : BaseModuleOptions
{
public string Dsn { get; set; } = "";
public bool EnableDebug { get; set; }
public double TracesSampleRate { get; set; } = 1.0;
public Action<IApplicationContext, ISentryBuilder, SentryModuleOptions>? ConfigureSentry { get; set; }
}

public class SentryModuleOptionsValidator : AbstractValidator<SentryModuleOptions>
{
public SentryModuleOptionsValidator()
{
RuleFor(options => options.Dsn).NotEmpty().WithMessage("Provide Sentry DSN");
RuleFor(options => options.TracesSampleRate).GreaterThan(0).WithMessage("Traces Sample Rate should be greater than zero");
}
}
8 changes: 8 additions & 0 deletions src/Sitko.Core.Sentry/Sitko.Core.Sentry.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Sentry.AspNetCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sitko.Core.App.Web\Sitko.Core.App.Web.csproj" />
</ItemGroup>
</Project>

0 comments on commit c92841e

Please sign in to comment.