From 9190110e2d9f49b1fe86f7eba6afceea46c3a5a8 Mon Sep 17 00:00:00 2001 From: Andra Constantin Date: Thu, 10 Oct 2024 04:51:13 -0400 Subject: [PATCH] back to original OnEnd (still whitespace err) --- Backend/Otel/OtelKernel.cs | 44 +++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/Backend/Otel/OtelKernel.cs b/Backend/Otel/OtelKernel.cs index b51896ceb..1a4f16e7e 100644 --- a/Backend/Otel/OtelKernel.cs +++ b/Backend/Otel/OtelKernel.cs @@ -1,26 +1,17 @@ -// using System.Diagnostics; -// using BackendFramework.Interfaces; +using System.Diagnostics; +using BackendFramework.Interfaces; using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; -// using OpenTelemetry; +using OpenTelemetry; using OpenTelemetry.Instrumentation.AspNetCore; using OpenTelemetry.Instrumentation.Http; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using System.Linq; -using OpenTelemetry; -using System.Diagnostics; // using BackendFramework.Otel; namespace BackendFramework.Otel { - - internal class UserEnricher() : BaseProcessor - { - public override void OnStart(Activity data) - { - } - } public static class OtelKernel { public const string SourceName = "Backend-Otel"; @@ -138,7 +129,7 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => tracerProviderBuilder .SetResourceBuilder(appResourceBuilder) .AddSource(SourceName) - // .AddProcessor() + .AddProcessor() .AddAspNetCoreInstrumentation(AspNetCoreBuilder) .AddHttpClientInstrumentation(HttpClientBuilder) .AddConsoleExporter() @@ -146,5 +137,32 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi ); } } + internal class LocationEnricher(ILocationProvider locationProvider) : BaseProcessor + { + public override async void OnEnd(Activity data) + { + string? uriPath = (string?)data.GetTagItem("url.full"); + string locationUri = LocationProvider.locationGetterUri; + if (uriPath == null || !uriPath.Contains(locationUri)) + { + LocationApi? response = await locationProvider.GetLocation(); + var location = new + { + Country = response?.country, + Region = response?.regionName, + City = response?.city, + }; + data?.AddTag("country", location.Country); + data?.AddTag("region", location.Region); + data?.AddTag("city", location.City); + } + data?.SetTag("SESSIONID BAGGAGE", data?.GetBaggageItem("sessionId")); + if (uriPath != null && uriPath.Contains(locationUri)) + { + data?.SetTag("url.full", ""); + data?.SetTag("url.redacted.ip", LocationProvider.locationGetterUri); + } + } + } }