Skip to content

Commit 19bec38

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

6 files changed

Lines changed: 48 additions & 2 deletions

File tree

Boundaries/Comanda.Stores/Source/Comanda.Stores.WebApi/Constants/Headers.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ namespace Comanda.Stores.WebApi.Constants;
22

33
public static class Headers
44
{
5-
public const string Authorization = "Authorization";
5+
public const string Credential = "X-Credential";
6+
public const string Location = "Location";
7+
public const string Link = "Link";
68
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";
712
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void UseHttpPipeline(this IApplicationBuilder app)
1414
app.UseAuthentication();
1515
app.UseAuthorization();
1616

17+
app.UseCorrelationMiddleware();
1718
app.UseEndpoints(endpoints =>
1819
{
1920
endpoints.MapControllers();

Boundaries/Comanda.Stores/Source/Comanda.Stores.WebApi/Extensions/ObservabilityExtension.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public static void AddObservability(this WebApplicationBuilder builder)
2424
options.Dsn = settings.Observability.SentryDsn;
2525
options.TracesSampleRate = 1.0;
2626
options.AttachStacktrace = true;
27-
options.Debug = true;
2827
});
2928
});
3029
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Comanda.Stores.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.Stores.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.Stores/Source/Comanda.Stores.WebApi/Usings.cs

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

99
global using Comanda.Stores.WebApi.Extensions;
1010
global using Comanda.Stores.WebApi.Constants;
11+
global using Comanda.Stores.WebApi.Middlewares;
1112
global using Comanda.Stores.Domain.Errors;
1213

1314
global using Comanda.Stores.Application.Payloads.Establishment;
@@ -29,4 +30,5 @@
2930

3031
global using Scalar.AspNetCore;
3132
global using Serilog;
33+
global using Serilog.Context;
3234
global using FluentValidation.AspNetCore;

0 commit comments

Comments
 (0)