From 95a1dc12c6e93d250883c8000a8edb6a1a729164 Mon Sep 17 00:00:00 2001 From: Koen Metsu Date: Wed, 20 Dec 2023 18:04:52 +0100 Subject: [PATCH] feat: or-2013 add initiator to otel tracing via middleware --- .../Middleware/InitiatorActivityMiddleware.cs | 28 +++++++++++++++++++ src/AssociationRegistry.Admin.Api/Program.cs | 1 + 2 files changed, 29 insertions(+) create mode 100644 src/AssociationRegistry.Admin.Api/Infrastructure/Middleware/InitiatorActivityMiddleware.cs diff --git a/src/AssociationRegistry.Admin.Api/Infrastructure/Middleware/InitiatorActivityMiddleware.cs b/src/AssociationRegistry.Admin.Api/Infrastructure/Middleware/InitiatorActivityMiddleware.cs new file mode 100644 index 000000000..5a7011ae4 --- /dev/null +++ b/src/AssociationRegistry.Admin.Api/Infrastructure/Middleware/InitiatorActivityMiddleware.cs @@ -0,0 +1,28 @@ +namespace AssociationRegistry.Admin.Api.Infrastructure.Middleware; + +using Microsoft.AspNetCore.Http; +using System.Diagnostics; +using System.Threading.Tasks; + +public class InitiatorActivityMiddleware +{ + private readonly RequestDelegate _next; + + public InitiatorActivityMiddleware(RequestDelegate next) + { + _next = next; + } + + public async Task InvokeAsync(HttpContext context) + { + if (context.Request.Headers.TryGetValue(WellknownHeaderNames.Initiator, out var headerValue)) + { + var currentActivity = Activity.Current; + if (currentActivity != null) + { + currentActivity.SetTag("vr.initiator", headerValue.ToString()); + } + } + await _next(context); + } +} diff --git a/src/AssociationRegistry.Admin.Api/Program.cs b/src/AssociationRegistry.Admin.Api/Program.cs index 24748aabd..3ab761836 100755 --- a/src/AssociationRegistry.Admin.Api/Program.cs +++ b/src/AssociationRegistry.Admin.Api/Program.cs @@ -278,6 +278,7 @@ private static void ConfigureMiddleware(IApplicationBuilder app) }) .UseMiddleware() .UseMiddleware() + .UseMiddleware() .UseResponseCompression(); }