Skip to content

Commit

Permalink
cleaning OtelKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Sep 26, 2024
1 parent ccf10aa commit 8098773
Showing 1 changed file with 40 additions and 55 deletions.
95 changes: 40 additions & 55 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
using BackendFramework.Interfaces;

using System.Diagnostics.CodeAnalysis;
// using System.Diagnostics.Metrics;
// using System.Net.Http;
// using System.Net.Http.Json;
// using System.Security.Claims;
// using System.Threading.Tasks;
// using Microsoft.AspNetCore.Http;
// using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
// using System.Collections.Generic;
using System.Linq;
// using Microsoft.AspNetCore.Http;

namespace BackendFramework.Otel
{
Expand All @@ -46,20 +37,15 @@ private static void AspNetCoreBuilder(AspNetCoreTraceInstrumentationOptions opti
activity.SetTag("inbound.http.request.body.size", "no content");
}
if (request.Headers.ContainsKey("sessionId"))
string? sessionId = request.Headers.TryGetValue("sessionId", out var values) ? values.FirstOrDefault() : null;
if (sessionId != null)
{
activity.SetTag("ID EXISTS asp", "TRUE");
activity.SetTag("SESSIONID ASP REQUEST", sessionId);
}
string? theId = request.Headers.TryGetValue("sessionId", out var values) ? values.FirstOrDefault() : null;
if (theId != null)
else
{
activity.SetTag("ID IS asp", theId);
activity.SetTag("SESSIONID ASP REQUEST", "NULL");
}
// activity.EnrichWithUser(request.HttpContext);
activity.SetTag("BEFORE", "inasp-request");
var sessionContext = request.HttpContext;
activity.SetTag("context", sessionContext);
activity.SetTag("AFTER", "inasp-request");
};
options.EnrichWithHttpResponse = (activity, response) =>
{
Expand All @@ -72,12 +58,15 @@ private static void AspNetCoreBuilder(AspNetCoreTraceInstrumentationOptions opti
{
activity.SetTag("inbound.http.response.body.size", "no content");
}
// activity.EnrichWithUser(response.HttpContext);
activity.SetTag("BEFORE", "inasp-response");
// var sessionId = response.HttpContext;
var sessionContext = response.HttpContext;
activity.SetTag("context", sessionContext);
activity.SetTag("AFTER", "inasp-response");
string? sessionId = response.Headers.TryGetValue("sessionId", out var values) ? values.FirstOrDefault() : null;
if (sessionId != null)
{
activity.SetTag("SESSIONID ASP RESPONSE", sessionId);
}
else
{
activity.SetTag("SESSIONID ASP RESPONSE", "NULL");
}
};
}

Expand Down Expand Up @@ -106,11 +95,15 @@ private static void HttpClientBuilder(HttpClientTraceInstrumentationOptions opti
{
activity.SetTag("outbound.http.response.body.size", "no content");
}
activity.SetTag("BEFORE", "inhttp-request");
// var sessionId = response.HttpContext;
var sessionContext = request.Headers.Connection;
activity.SetTag("context", sessionContext);
activity.SetTag("AFTER", "inhttp-request");
string? sessionId = request.Headers.TryGetValues("sessionId", out var values) ? values.FirstOrDefault() : null;
if (sessionId != null)
{
activity.SetTag("SESSIONID HTTP REQUEST", sessionId);
}
else
{
activity.SetTag("SESSIONID HTTP REQUEST", "NULL");
}
};
options.EnrichWithHttpResponseMessage = (activity, response) =>
{
Expand All @@ -119,21 +112,15 @@ private static void HttpClientBuilder(HttpClientTraceInstrumentationOptions opti
{
activity.SetTag("outbound.http.response.body.size", contentLength.Value);
}
if (response.Headers.Contains("sessionId"))
string? sessionId = response.Headers.TryGetValues("sessionId", out var values) ? values.FirstOrDefault() : null;
if (sessionId != null)
{
activity.SetTag("ID EXISTS http", "TRUE");
activity.SetTag("SESSIONID HTTP RESPONSE", sessionId);
}
// IEnumerable<string> values;
string? theId = response.Headers.TryGetValues("sessionId", out var values) ? values.FirstOrDefault() : null;
if (theId != null)
else
{
activity.SetTag("ID IS http", theId);
activity.SetTag("SESSIONID HTTP RESPONSE", "NULL");
}
activity.SetTag("BEFORE", "inhttp-response");
// var sessionId = response.HttpContext;
var sessionContext = response.Headers.Connection;
activity.SetTag("context", sessionContext);
activity.SetTag("AFTER", "inhttp-response");
};
}

Expand All @@ -154,27 +141,25 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi

}

// private static void EnrichWithUser(this Activity activity, HttpContext httpContext)
// private static void EnrichWithSession(this Activity activity, HttpContext httpContext)
// {
// var claimsPrincipal = httpContext.User;
// // var userId = claimsPrincipal?.FindFirstValue("sub");
// var userId = claimsPrincipal;
// if (userId != null)
// {
// activity.SetTag("app.user.id", userId);
// }
// var userRole = claimsPrincipal?.FindFirstValue("role");
// if (userRole != null)

// // in progress

// string? sessionId = httpContext.Headers.TryGetValues("sessionId", out var values) ? values.FirstOrDefault() : null;
// if (sessionId != null)
// {
// activity.SetTag("app.user.role", userRole);
// activity.SetTag("SESSIONID HTTP RESPONSE", sessionId);
// }
// if (httpContext.RequestAborted.IsCancellationRequested)
// else
// {
// activity.SetTag("http.abort", true);
// activity.SetTag("SESSIONID HTTP RESPONSE", "NULL");
// }


// }

private class LocationEnricher(ILocationProvider locationProvider) : BaseProcessor<Activity>
internal class LocationEnricher(ILocationProvider locationProvider) : BaseProcessor<Activity>
{
public override async void OnEnd(Activity data)
{
Expand Down

0 comments on commit 8098773

Please sign in to comment.