Skip to content

Commit 50dd02f

Browse files
committed
refactor: separate tracing and analytics middlewares as it was
1 parent 6ed948b commit 50dd02f

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Middleware/AnalyticsMiddleware.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace VirtualFinland.UserAPI.Middleware;
66

7+
/// <summary>
8+
/// Adds analytics to the request pipeline (for authenticated user requests)
9+
/// </summary>
710
public class AnalyticsMiddleware
811
{
912
private readonly RequestDelegate _next;
@@ -17,11 +20,6 @@ public AnalyticsMiddleware(RequestDelegate next, AnalyticsService analyticsServi
1720

1821
public async Task Invoke(HttpContext context)
1922
{
20-
if (context.Request.Headers.TryGetValue(Constants.Headers.XRequestTraceId, out var traceId))
21-
{
22-
context.TraceIdentifier = traceId;
23-
}
24-
2523
await _next.Invoke(context);
2624

2725
var authenticatedUser = context.Items["User"];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using VirtualFinland.UserAPI.Helpers;
2+
3+
namespace VirtualFinland.UserAPI.Middleware;
4+
5+
/// <summary>
6+
/// Overrides the trace identifier with the value of the X-Request-TraceId header (if present)
7+
/// </summary>
8+
public class RequestTracingMiddleware
9+
{
10+
private readonly RequestDelegate _next;
11+
12+
public RequestTracingMiddleware(RequestDelegate next)
13+
{
14+
_next = next;
15+
}
16+
17+
public async Task Invoke(HttpContext context)
18+
{
19+
if (context.Request.Headers.TryGetValue(Constants.Headers.XRequestTraceId, out var traceId))
20+
{
21+
context.TraceIdentifier = traceId;
22+
}
23+
24+
await _next.Invoke(context);
25+
}
26+
}

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
};
192192
});
193193

194+
app.UseMiddleware<RequestTracingMiddleware>();
194195
app.UseMiddleware<AnalyticsMiddleware>();
195196
app.UseMiddleware<ErrorHandlerMiddleware>();
196197
app.UseHttpsRedirection();

0 commit comments

Comments
 (0)