Skip to content

Commit

Permalink
Telemetry improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
harryy94 committed Jan 27, 2025
1 parent 4320a3a commit 6afaa8c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/web/CareLeavers.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using CareLeavers.Web.Caching;
using CareLeavers.Web.Configuration;
using CareLeavers.Web.ContentfulRenderers;
using CareLeavers.Web.Telemetry;
using Contentful.AspNetCore;
using Contentful.Core;
using Contentful.Core.Models;
using GovUk.Frontend.AspNetCore;
using Microsoft.Extensions.Caching.Distributed;
using OpenTelemetry.Trace;
using Serilog;

Log.Logger = new LoggerConfiguration()
Expand All @@ -24,14 +26,21 @@
builder.Services.AddSerilog((_, lc) => lc
.ConfigureLogging(builder.Configuration["ApplicationInsights:ConnectionString"]));

var connString = builder.Configuration.GetValue<string>("ApplicationInsights:ConnectionString");
var appInsightsConnectionString = builder.Configuration.GetValue<string>("ApplicationInsights:ConnectionString");

if (!string.IsNullOrEmpty(connString))
if (!string.IsNullOrEmpty(appInsightsConnectionString))
{
builder.Services.AddOpenTelemetry()
.UseAzureMonitor(x => x.ConnectionString = connString);
.WithTracing(x =>
{
x.AddAspNetCoreInstrumentation();
x.AddProcessor<RouteTelemetryProcessor>();
})
.UseAzureMonitor(x => x.ConnectionString = appInsightsConnectionString);
}

builder.Services.AddHttpContextAccessor();

builder.Services.AddContentful(builder.Configuration);

builder.Services.AddTransient<HtmlRenderer>((c) =>
Expand Down
28 changes: 28 additions & 0 deletions src/web/CareLeavers.Web/Telemetry/RouteTelemetryProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;
using OpenTelemetry;

namespace CareLeavers.Web.Telemetry;

public class RouteTelemetryProcessor(IHttpContextAccessor httpContextAccessor) : BaseProcessor<Activity>
{
public override void OnEnd(Activity activity)
{
var httpContext = httpContextAccessor.HttpContext;

if (httpContext == null)
{
base.OnEnd(activity);
return;
}

var routeData = httpContext.GetRouteData();
routeData.Values.TryGetValue("slug", out var slug);

if (slug != null)
{
activity.SetTag("http.route", $"/{slug}");
}

base.OnEnd(activity);
}
}

0 comments on commit 6afaa8c

Please sign in to comment.