Skip to content

Commit

Permalink
feat: Sentry sampling, other changes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-shahnawaz authored Aug 2, 2022
1 parent 3e9cf95 commit d511327
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
27 changes: 27 additions & 0 deletions ASP.NET-Core.WebAPI/Helpers/ServiceExtensions/LoggingExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Serilog;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -29,6 +30,32 @@ internal static void AddLogging(this IServiceCollection serviceCollection, Envir
options.Dsn = environmentConfiguration.SENTRY_DSN;
options.Environment = environmentConfiguration.SENTRY_ENVIRONMENT;
options.Release = environmentConfiguration.SENTRY_RELEASE;
// See: https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/configuration/sampling [Accessed on 2022-06-06]
options.SampleRate = 0.5f;
options.TracesSampler = context =>
{
// If this is the continuation of a trace, just use that decision (rate controlled by the caller)
if (context.TransactionContext.IsParentSampled is not null)
{
return context.TransactionContext.IsParentSampled.Value
? 1.0
: 0.0;
}
// Otherwise, sample based on URL (exposed through custom sampling context)
return context.CustomSamplingContext.GetValueOrDefault("url") switch
{
// The health check endpoint is just noise - drop all transactions
"/health" => 0.0,
// Open API documentation endpoint
"/api-docs/*" => 0.0,
// Default sample rate
_ => 0.3
};
};
});
}
});
Expand Down
12 changes: 5 additions & 7 deletions ASP.NET-Core.WebAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void ConfigureServices(IServiceCollection services)
* then move provider logic in separate class (Single Responsibilty principle).
*/

services.AddOptions<EnvironmentConfiguration>(Configuration, out EnvironmentConfiguration environmentConfiguration);
services.AddOptions<ApplicationConfiguration>(Configuration, out ApplicationConfiguration applicationConfiguration);
services.AddOptions(Configuration, out EnvironmentConfiguration environmentConfiguration);
services.AddOptions(Configuration, out ApplicationConfiguration applicationConfiguration);
services.AddLogging(environmentConfiguration);
services.AddCors(environmentConfiguration);
services.AddRoutingConfigurations();
Expand All @@ -78,19 +78,17 @@ public void ConfigureServices(IServiceCollection services)
/// This method gets called by the runtime. Use this method to configure the HTTP request processing pipeline.
/// </summary>
/// <param name="app">An abstraction which provides mechanisms to configure the HTTP request processing pipeline.</param>
/// <param name="env">An abstraction which provides information about the web hosting environment within which this application is running.</param>
/// <param name="provider">An abstraction representing OpenAPI specification configuration</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
/// <param name="environmentConfiguration">Strongly typed environmental configuration</param>
/// <param name="applicationConfiguration">Strongly typed application configuration</param>
public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider provider, EnvironmentConfiguration environmentConfiguration, ApplicationConfiguration applicationConfiguration)
{
/*
While configuring ASP.NET Core pipeline please follow:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0#middleware-order
Accessed: 2021-06-01
*/

EnvironmentConfiguration environmentConfiguration = app.ApplicationServices.CreateScope().ServiceProvider.GetService<IOptions<EnvironmentConfiguration>>().Value;
ApplicationConfiguration applicationConfiguration = app.ApplicationServices.CreateScope().ServiceProvider.GetService<IOptions<ApplicationConfiguration>>().Value;

if (applicationConfiguration?.UseInMemoryDatabase ?? false)
{
InMemoryDbDataSeeder.SeedTestData(app);
Expand Down

0 comments on commit d511327

Please sign in to comment.