Skip to content

Commit e1b7d3f

Browse files
feature(#6): this commit implements correlation middleware to improve traceability in observability and tracing tools
1 parent b354f78 commit e1b7d3f

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Comanda.Orchestrator.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.Orchestrator.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+
}

0 commit comments

Comments
 (0)