Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/BuildingBlocks/Core/Context/CorrelationIdContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FSH.Framework.Core.Context;

public class CorrelationIdContext : ICorrelationIdContext, ICorrelationIdInitializer
{
private string? _correlationId;

public string? CorrelationId => _correlationId;

public void SetCorrelationId(string correlationId)
{
_correlationId = correlationId;
}
}
6 changes: 6 additions & 0 deletions src/BuildingBlocks/Core/Context/ICorrelationIdContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FSH.Framework.Core.Context;

public interface ICorrelationIdContext
{
string? CorrelationId { get; }
}
6 changes: 6 additions & 0 deletions src/BuildingBlocks/Core/Context/ICorrelationIdInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace FSH.Framework.Core.Context;

public interface ICorrelationIdInitializer
{
void SetCorrelationId(string correlationId);
}
29 changes: 29 additions & 0 deletions src/BuildingBlocks/Jobs/CorrelationIdJobFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FSH.Framework.Shared.Multitenancy;
using Hangfire.Server;
using Serilog.Context;

namespace FSH.Framework.Jobs;

public class CorrelationIdJobFilter : IServerFilter
{
public void OnPerforming(PerformingContext context)
{
ArgumentNullException.ThrowIfNull(context);

var correlationId = context.GetJobParameter<string>("correlationId");
if (!string.IsNullOrEmpty(correlationId))
{
LogContext.PushProperty("correlation_id", correlationId);
}

var tenantInfo = context.GetJobParameter<AppTenantInfo>(MultitenancyConstants.Identifier);
if (tenantInfo is not null)
{
LogContext.PushProperty("tenant_id", tenantInfo.Id);
}
}

public void OnPerformed(PerformedContext context)
{
}
}
3 changes: 2 additions & 1 deletion src/BuildingBlocks/Jobs/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FSH.Framework.Core.Exceptions;
using FSH.Framework.Core.Exceptions;
using FSH.Framework.Jobs.Services;
using FSH.Framework.Shared.Persistence;
using Hangfire;
Expand Down Expand Up @@ -56,6 +56,7 @@ public static IServiceCollection AddHeroJobs(this IServiceCollection services)

config.UseFilter(new FshJobFilter(provider));
config.UseFilter(new LogJobFilter());
config.UseFilter(new CorrelationIdJobFilter());
config.UseFilter(new HangfireTelemetryFilter());
});

Expand Down
9 changes: 8 additions & 1 deletion src/BuildingBlocks/Jobs/FshJobActivator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Finbuckle.MultiTenant;
using Finbuckle.MultiTenant;
using Finbuckle.MultiTenant.Abstractions;
using FSH.Framework.Core.Common;
using FSH.Framework.Core.Context;
Expand Down Expand Up @@ -47,6 +47,13 @@ private void ReceiveParameters()
_scope.ServiceProvider.GetRequiredService<ICurrentUserInitializer>()
.SetCurrentUserId(userId);
}

string correlationId = _context.GetJobParameter<string>("correlationId");
if (!string.IsNullOrEmpty(correlationId))
{
_scope.ServiceProvider.GetRequiredService<ICorrelationIdInitializer>()
.SetCorrelationId(correlationId);
}
}

public override object Resolve(Type type) =>
Expand Down
8 changes: 7 additions & 1 deletion src/BuildingBlocks/Jobs/FshJobFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Finbuckle.MultiTenant.Abstractions;
using Finbuckle.MultiTenant.Abstractions;
using FSH.Framework.Core.Common;
using FSH.Framework.Shared.Identity.Claims;
using FSH.Framework.Shared.Multitenancy;
Expand Down Expand Up @@ -49,6 +49,12 @@ public void OnCreating(CreatingContext context)
{
context.SetJobParameter(QueryStringKeys.UserId, userId);
}

var correlationId = httpContext.TraceIdentifier;
if (!string.IsNullOrEmpty(correlationId))
{
context.SetJobParameter("correlationId", correlationId);
}
}

public void OnCreated(CreatedContext context)
Expand Down
3 changes: 2 additions & 1 deletion src/BuildingBlocks/Jobs/Jobs.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>FSH.Framework.Jobs</RootNamespace>
Expand All @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Serilog" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/BuildingBlocks/Web/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FSH.Framework.Caching;
using FSH.Framework.Caching;
using FSH.Framework.Jobs;
using FSH.Framework.Mailing;
using FSH.Framework.Persistence;
Expand All @@ -16,11 +16,13 @@
using FSH.Framework.Web.Security;
using FSH.Framework.Web.Versioning;
using Mediator;
using FSH.Framework.Web.Middlewares;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using FSH.Framework.Core.Context;

namespace FSH.Framework.Web;

Expand All @@ -34,6 +36,11 @@ public static IHostApplicationBuilder AddHeroPlatform(this IHostApplicationBuild
configure?.Invoke(options);

builder.Services.AddScoped<CurrentUserMiddleware>();
builder.Services.AddScoped<CorrelationIdMiddleware>();

builder.Services.AddScoped<CorrelationIdContext>();
builder.Services.AddScoped<ICorrelationIdContext>(sp => sp.GetRequiredService<CorrelationIdContext>());
builder.Services.AddScoped<ICorrelationIdInitializer>(sp => sp.GetRequiredService<CorrelationIdContext>());

builder.AddHeroLogging();
if (options.EnableOpenTelemetry)
Expand Down Expand Up @@ -98,6 +105,7 @@ public static WebApplication UseHeroPlatform(this WebApplication app, Action<Fsh
var openApiEnabled = options.UseOpenApi && IsOpenApiEnabled(app.Configuration);

app.UseExceptionHandler();
app.UseMiddleware<CorrelationIdMiddleware>();
app.UseHttpsRedirection();

app.UseHeroSecurityHeaders();
Expand Down
31 changes: 31 additions & 0 deletions src/BuildingBlocks/Web/Middlewares/CorrelationIdMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FSH.Framework.Core.Context;
using Microsoft.AspNetCore.Http;

namespace FSH.Framework.Web.Middlewares;

public class CorrelationIdMiddleware(ICorrelationIdInitializer correlationIdInitializer) : IMiddleware
{
private const string CorrelationIdHeader = "X-Correlation-Id";
private readonly ICorrelationIdInitializer _correlationIdInitializer = correlationIdInitializer;

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(next);

if (!context.Request.Headers.TryGetValue(CorrelationIdHeader, out var correlationId))
{
correlationId = context.TraceIdentifier;
}

_correlationIdInitializer.SetCorrelationId(correlationId!);

// Also add to response headers for predictability
if (!context.Response.Headers.ContainsKey(CorrelationIdHeader))
{
context.Response.Headers.Append(CorrelationIdHeader, correlationId);
}

await next(context);
}
}