-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorrelationMiddleware.cs
More file actions
29 lines (23 loc) · 1.07 KB
/
CorrelationMiddleware.cs
File metadata and controls
29 lines (23 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace Comanda.Orders.WebApi.Middlewares;
public sealed class CorrelationMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext context)
{
var correlationId = context.Request.Headers[Headers.Correlation].FirstOrDefault();
if (string.IsNullOrWhiteSpace(correlationId))
correlationId = context.TraceIdentifier;
context.Items[Headers.Correlation] = correlationId;
context.Response.Headers[Headers.Correlation] = correlationId;
/* enriches the logging scope with the current correlation identifier, ensuring all logs within this request pipeline share the same identifier */
/* more details: https://microsoft.github.io/code-with-engineering-playbook/observability/correlation-id/ */
using (LogContext.PushProperty(Headers.Correlation, correlationId))
using (SentrySdk.PushScope())
{
SentrySdk.ConfigureScope(scope =>
{
scope.SetTag("correlation_id", correlationId);
});
await next(context);
}
}
}