Skip to content

Commit b428ae5

Browse files
feature(#6): this commit implements correlation support to improve tracing and observability
1 parent 19bec38 commit b428ae5

5 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Comanda.Subscriptions.WebApi.Constants;
2+
3+
public static class Headers
4+
{
5+
public const string Credential = "X-Credential";
6+
public const string Location = "Location";
7+
public const string Link = "Link";
8+
public const string Pagination = "X-Pagination";
9+
public const string Authorization = "Authorization";
10+
public const string Idempotency = "Idempotency-Key";
11+
public const string Correlation = "Correlation";
12+
}

Boundaries/Comanda.Subscriptions/Source/Comanda.Subscriptions.WebApi/Extensions/HttpPipelineExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public static void UseHttpPipeline(this IApplicationBuilder app)
1313
app.UseAuthentication();
1414
app.UseAuthorization();
1515

16+
app.UseCorrelationMiddleware();
1617
app.UseEndpoints(endpoints =>
1718
{
1819
endpoints.MapControllers();
1920
});
2021
}
21-
}
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Comanda.Subscriptions.WebApi.Middlewares;
2+
3+
public sealed class CorrelationMiddleware(RequestDelegate next)
4+
{
5+
public async Task InvokeAsync(HttpContext context)
6+
{
7+
var correlationId = context.Request.Headers[Headers.Correlation].FirstOrDefault();
8+
9+
if (string.IsNullOrWhiteSpace(correlationId))
10+
correlationId = context.TraceIdentifier;
11+
12+
context.Items[Headers.Correlation] = correlationId;
13+
context.Response.Headers[Headers.Correlation] = correlationId;
14+
15+
/* enriches the logging scope with the current correlation identifier, ensuring all logs within this request pipeline share the same identifier */
16+
/* more details: https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/ */
17+
18+
using (LogContext.PushProperty(Headers.Correlation, correlationId))
19+
using (SentrySdk.PushScope())
20+
{
21+
SentrySdk.ConfigureScope(scope =>
22+
{
23+
scope.SetTag("correlation_id", correlationId);
24+
});
25+
26+
await next(context);
27+
}
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Comanda.Subscriptions.WebApi.Middlewares;
2+
3+
[ExcludeFromCodeCoverage(Justification = "contains only dependency injection")]
4+
public static class CorrelationMiddlewareExtension
5+
{
6+
public static IApplicationBuilder UseCorrelationMiddleware(this IApplicationBuilder app)
7+
{
8+
return app.UseMiddleware<CorrelationMiddleware>();
9+
}
10+
}

Boundaries/Comanda.Subscriptions/Source/Comanda.Subscriptions.WebApi/Usings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
global using Comanda.Subscriptions.WebApi.Extensions;
88
global using Comanda.Subscriptions.WebApi.Constants;
9+
global using Comanda.Subscriptions.WebApi.Middlewares;
910
global using Comanda.Subscriptions.Domain.Errors;
1011

1112
global using Comanda.Subscriptions.Application.Payloads.Traceability;
@@ -19,4 +20,5 @@
1920

2021
global using Scalar.AspNetCore;
2122
global using Serilog;
23+
global using Serilog.Context;
2224
global using FluentValidation.AspNetCore;

0 commit comments

Comments
 (0)