Skip to content

Commit

Permalink
backend serviceName as library.name in split HC charts
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Sep 6, 2024
1 parent d519822 commit 00c2977
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Backend/Otel/BackendActivitySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class BackendActivitySource
{
public static ActivitySource Get()
{
return new ActivitySource("service");
// return new ActivitySource("service");
return new ActivitySource(OtelKernel.ServiceName);
}
}
53 changes: 27 additions & 26 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// using System.Diagnostics.Metrics;
// using System.Net.Http;
// using System.Net.Http.Json;
using System.Security.Claims;
// using System.Security.Claims;
// using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
// using Microsoft.AspNetCore.Http;
// using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
Expand All @@ -20,7 +20,7 @@ namespace BackendFramework.Otel
public static class OtelKernel
{

// public const string ServiceName = "Backend-Otel";
public const string ServiceName = "Backend-Otel";
// private readonly LocationCache _locationCache;

// public OtelKernel(LocationCache locationCache, IServiceCollection serviceCollection)
Expand All @@ -32,10 +32,11 @@ public static class OtelKernel
public static void AddOpenTelemetryInstrumentation(this IServiceCollection services)
{
var appResourceBuilder = ResourceBuilder.CreateDefault();
// var appResourceBuilder = ResourceBuilder.CreateDefault().AddService(ServiceName);
// todo: include version
services.AddOpenTelemetry().WithTracing(tracerProviderBuilder => tracerProviderBuilder
.SetResourceBuilder(appResourceBuilder)
// .AddSource(ServiceName)
.AddSource(ServiceName)
.AddProcessor<LocationEnricher>()
.AddAspNetCoreInstrumentation(options =>
{
Expand All @@ -51,7 +52,7 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
{
activity.SetTag("inbound.http.request.body.size", "no content");
}
activity.EnrichWithUser(request.HttpContext);
// activity.EnrichWithUser(request.HttpContext);
};
options.EnrichWithHttpResponse = (activity, response) =>
{
Expand All @@ -64,7 +65,7 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
{
activity.SetTag("inbound.http.response.body.size", "no content");
}
activity.EnrichWithUser(response.HttpContext);
// activity.EnrichWithUser(response.HttpContext);
};
})
.AddHttpClientInstrumentation(options =>
Expand All @@ -83,7 +84,7 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi

if (request.RequestUri is not null)
{
// activity.SetTag("url.path", 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 @@ -123,25 +124,25 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi

}

private static void EnrichWithUser(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)
{
activity.SetTag("app.user.role", userRole);
}
if (httpContext.RequestAborted.IsCancellationRequested)
{
activity.SetTag("http.abort", true);
}
}
// private static void EnrichWithUser(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)
// {
// activity.SetTag("app.user.role", userRole);
// }
// if (httpContext.RequestAborted.IsCancellationRequested)
// {
// activity.SetTag("http.abort", true);
// }
// }

private class LocationEnricher(LocationProvider locationProvider) : BaseProcessor<Activity>
{
Expand Down

0 comments on commit 00c2977

Please sign in to comment.