Skip to content

Commit 8dce002

Browse files
Add Azure Monitor OpenTelemetry Profiler for container deployments
- Add Azure.Monitor.OpenTelemetry.Profiler 1.0.0-beta9 to CPM and web project - Wire up AddAzureMonitorProfiler() chained on UseAzureMonitor() in Program.cs - Profiler only activates when APPLICATIONINSIGHTS_CONNECTION_STRING is set - Add profiler debug logging to appsettings.Development.json for local diagnostics - Uses EventPipe (in-process) — compatible with chiseled container base image - Enables Code Optimizations (AI-based perf insights) in Azure portal Tested locally: profiler agent loads and initializes correctly on both dotnet run and Docker (noble-chiseled-extra base image).
1 parent 1f57837 commit 8dce002

4 files changed

Lines changed: 25 additions & 19 deletions

File tree

Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<PackageVersion Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.5.1" />
2323
<PackageVersion Include="Azure.Identity" Version="1.21.0" />
2424
<PackageVersion Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.5.0" />
25+
<!-- TODO: update to stable release when Azure.Monitor.OpenTelemetry.Profiler reaches GA -->
26+
<PackageVersion Include="Azure.Monitor.OpenTelemetry.Profiler" Version="1.0.0-beta9" />
2527
<PackageVersion Include="TUnit" Version="1.40.5" />
2628
<PackageVersion Include="EssentialCSharp.Shared.Models" Version="$(ToolingPackagesVersion)" />
2729
<PackageVersion Include="HtmlAgilityPack" Version="1.12.4" />

EssentialCSharp.Web/EssentialCSharp.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" />
3939
<PackageReference Include="Azure.Identity" />
4040
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" />
41+
<PackageReference Include="Azure.Monitor.OpenTelemetry.Profiler" />
4142
<PackageReference Include="EssentialCSharp.Shared.Models" />
4243
<PackageReference Include="HtmlAgilityPack" />
4344
<PackageReference Include="IntelliTect.Multitool" />

EssentialCSharp.Web/Program.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.AspNetCore.Identity.UI.Services;
2020
using Microsoft.AspNetCore.RateLimiting;
2121
using Azure.Monitor.OpenTelemetry.AspNetCore;
22+
using Azure.Monitor.OpenTelemetry.Profiler;
2223
using Microsoft.AspNetCore.DataProtection;
2324
using Microsoft.AspNetCore.Diagnostics;
2425
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
@@ -90,7 +91,7 @@ private static void Main(string[] args)
9091
});
9192

9293
if (useAzureMonitor)
93-
otel.UseAzureMonitor();
94+
otel.UseAzureMonitor().AddAzureMonitorProfiler();
9495
else if (useOtlp)
9596
otel.UseOtlpExporter();
9697

@@ -511,7 +512,7 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
511512
}
512513
app.UseStaticFiles();
513514

514-
app.UseRouting();
515+
app.UseRouting();
515516

516517
app.UseWhen(
517518
context => context.Request.Path.StartsWithSegments("/mcp"),
@@ -542,10 +543,10 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
542543
await next(context);
543544
}));
544545

545-
app.UseRateLimiter();
546-
547-
app.UseAuthorization();
548-
app.UseOutputCache();
546+
app.UseRateLimiter();
547+
548+
app.UseAuthorization();
549+
app.UseOutputCache();
549550

550551
app.UseMiddleware<ReferralMiddleware>();
551552

@@ -584,13 +585,13 @@ await McpJsonRpcResponseWriter.WriteErrorAsync(
584585
try
585586
{
586587
SitemapXmlHelpers.EnsureSitemapHealthy(siteMappingService.SiteMappings.ToList());
587-
LogSitemapValidationSucceeded(logger);
588-
}
589-
catch (Exception ex)
590-
{
591-
LogSitemapValidationFailed(logger, ex);
592-
// Continue startup even if sitemap validation fails
593-
}
588+
LogSitemapValidationSucceeded(logger);
589+
}
590+
catch (Exception ex)
591+
{
592+
LogSitemapValidationFailed(logger, ex);
593+
// Continue startup even if sitemap validation fails
594+
}
594595

595596
app.Run();
596597
}
@@ -604,11 +605,11 @@ private static bool IsMcpTransportRequest(HttpRequest request) =>
604605
[LoggerMessage(Level = LogLevel.Error, Message = "Unhandled exception on {Path}")]
605606
private static partial void LogUnhandledException(ILogger<Program> logger, Exception? exception, PathString path);
606607

607-
[LoggerMessage(Level = LogLevel.Information, Message = "Sitemap validation completed successfully during application startup")]
608-
private static partial void LogSitemapValidationSucceeded(ILogger<Program> logger);
609-
610-
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to validate sitemap during application startup")]
611-
private static partial void LogSitemapValidationFailed(ILogger<Program> logger, Exception exception);
608+
[LoggerMessage(Level = LogLevel.Information, Message = "Sitemap validation completed successfully during application startup")]
609+
private static partial void LogSitemapValidationSucceeded(ILogger<Program> logger);
610+
611+
[LoggerMessage(Level = LogLevel.Error, Message = "Failed to validate sitemap during application startup")]
612+
private static partial void LogSitemapValidationFailed(ILogger<Program> logger, Exception exception);
612613

613614
[LoggerMessage(Level = LogLevel.Warning, Message = "Ignoring invalid TryDotNet origin in CSP: {Origin}")]
614615
private static partial void LogIgnoringInvalidTryDotNetOrigin(ILogger logger, string origin);

EssentialCSharp.Web/appsettings.Development.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"Logging": {
44
"LogLevel": {
55
"Default": "Information",
6-
"Microsoft.AspNetCore": "Warning"
6+
"Microsoft.AspNetCore": "Warning",
7+
"Microsoft.ServiceProfiler": "Debug",
8+
"Azure.Monitor.OpenTelemetry.Profiler": "Debug"
79
}
810
},
911
"ConnectionStrings": {

0 commit comments

Comments
 (0)