Skip to content

Commit

Permalink
set and access session
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Sep 20, 2024
1 parent 7e004b1 commit 86be276
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 20 deletions.
81 changes: 61 additions & 20 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

// using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;

// using Microsoft.AspNetCore.Authentication.Cookies;

// using System.Security.Claims;
// using Microsoft.AspNetCore.Http;

// using System.Diagnostics.Metrics;
// using System.Net.Http;
// using System.Net.Http.Json;
Expand Down Expand Up @@ -53,6 +60,13 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
activity.SetTag("inbound.http.request.body.size", "no content");
}
// activity.EnrichWithUser(request.HttpContext);
// activity.SetTag("BEFORE", "before");
// var sess = request.HttpContext.Session;
// activity.SetTag("SESSION!", sess);
// activity.SetTag("AFTER", "after");
// var id = request.HttpContext.Request.Cookies[pConfig.CookieName].Value();
// var id = request.HttpContext.Session.Id;
};
options.EnrichWithHttpResponse = (activity, response) =>
{
Expand All @@ -66,6 +80,9 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
activity.SetTag("inbound.http.response.body.size", "no content");
}
// activity.EnrichWithUser(response.HttpContext);
activity.SetTag("COOKIE-RESPONSE", "before");
var id = response.HttpContext.Session.Id;
activity.SetTag("COOKIE-RESPONSE", "after");
};
})
.AddHttpClientInstrumentation(options =>
Expand All @@ -84,7 +101,7 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
if (request.RequestUri is not null)
{
activity.SetTag("url.pathHERE", request.RequestUri.AbsolutePath);
// activity.SetTag("url.pathHERE", request.RequestUri.AbsolutePath);
if (!string.IsNullOrEmpty(request.RequestUri.Query))
activity.SetTag("url.query", request.RequestUri.Query);
}
Expand Down Expand Up @@ -142,34 +159,58 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
// {
// activity.SetTag("http.abort", true);
// }
// }

private class LocationEnricher(LocationProvider locationProvider) : BaseProcessor<Activity>
{
public override async void OnEnd(Activity data)

// private class LocationEnricher(IHttpContextAccessor contextAccessor, LocationProvider locationProvider) : BaseProcessor<Activity>
// private class LocationEnricher(LocationProvider locationProvider, SessionProvider sessionProvider) : BaseProcessor<Activity>
private class LocationEnricher(LocationProvider locationProvider, IHttpContextAccessor contextAccessor) : BaseProcessor<Activity>
{
string? uriPath = (string?)data.GetTagItem("url.full");
string locationUri = LocationProvider.locationGetterUri;

if (uriPath == null || !uriPath.Contains(locationUri))
// public override void OnStart(Activity data)
// {
// // if (contextAccessor.HttpContext is { } context) data.EnrichWithUser(context);
// var existingSession = sessionProvider.GetSession();
// data?.AddTag("SESSIONONSTART", existingSession);
// }
public override async void OnEnd(Activity data)
{
LocationApi? response = await locationProvider.GetLocation();
string? uriPath = (string?)data.GetTagItem("url.full");
string locationUri = LocationProvider.locationGetterUri;

var location = new
if (uriPath == null || !uriPath.Contains(locationUri))
{
Country = response?.country,
Region = response?.regionName,
City = response?.city,
};

data?.AddTag("country", location.Country);
data?.AddTag("region", location.Region);
data?.AddTag("city", location.City);
}
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);

if (contextAccessor.HttpContext is { } context)
{
// data?.SetTag("ONEND BEFORE", "before");
// var sess = context.Session;
// data?.SetTag("SESSION!", sess);

}


// var existingSession = sessionProvider.GetSession();
// data?.AddTag("SESSIONONSTART", existingSession);
}



}
}
}
}

}


29 changes: 29 additions & 0 deletions Backend/Otel/SessionProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Http;

namespace BackendFramework.Otel
{
public class SessionProvider
{
private readonly IHttpContextAccessor _contextAccessor;
public SessionProvider(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;

}
public string GetSession()
{
// note: adding any activity tags in this function will cause overflow
// because function called on each activity in OtelKernel
if (_contextAccessor.HttpContext is { } context)
{
// context.Session.SetString("mysession", "mysessionValue");

var sessId = context.Session.Id;
// var sessId = context;

return sessId;
}
return "noSess";
}
}
}
15 changes: 15 additions & 0 deletions Backend/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,24 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IWordService, WordService>();

// OpenTelemetry


services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
// options.Cookie.HttpOnly = true;
// options.Cookie.IsEssential = true;
}
);
services.AddSingleton<SessionProvider>();

services.AddMemoryCache();
services.AddHttpContextAccessor();

services.AddHttpClient();
services.AddSingleton<LocationProvider>();

services.AddOpenTelemetryInstrumentation();

}
Expand All @@ -320,6 +333,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApp
app.UseRouting();
// Apply CORS policy to all requests.
app.UseCors(LocalhostCorsPolicy);
// app.UseSession();

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
Expand All @@ -328,6 +342,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApp

app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
Expand Down

0 comments on commit 86be276

Please sign in to comment.