From 6de01743a13f9fdbb34e805c7bfad5be02826b3f Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 14 May 2026 22:23:25 -0700 Subject: [PATCH 1/4] Add Azure Monitor OTel Profiler + Code Optimizations integration - NuGet.config: add nuget.org source + Azure.Monitor.* / OpenTelemetry.* package source mapping - Directory.Packages.props: pin Azure.Monitor.OpenTelemetry.AspNetCore 1.5.0 and Azure.Monitor.OpenTelemetry.Profiler 1.0.1-beta.1 - Microsoft.TryDotNet.csproj: add PackageReference for both OTel packages - Program.cs: wire AddOpenTelemetry().UseAzureMonitor().AddAzureMonitorProfiler(); add startup warning for missing connection string - appsettings.json: suppress OTel log noise (Default=Warning), keep Microsoft.ServiceProfiler=Information for initial validation --- Directory.Packages.props | 4 ++++ NuGet.config | 5 +++++ src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj | 2 ++ src/Microsoft.TryDotNet/Program.cs | 13 +++++++++++++ src/Microsoft.TryDotNet/appsettings.json | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/Directory.Packages.props b/Directory.Packages.props index a3fa5a87a..e18362d52 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,6 +4,10 @@ true + + + + diff --git a/NuGet.config b/NuGet.config index 689748dac..887291f11 100644 --- a/NuGet.config +++ b/NuGet.config @@ -11,6 +11,7 @@ + @@ -25,5 +26,9 @@ + + + + diff --git a/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj b/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj index a986414a9..bf4a33e93 100644 --- a/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj +++ b/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj @@ -10,6 +10,8 @@ + + diff --git a/src/Microsoft.TryDotNet/Program.cs b/src/Microsoft.TryDotNet/Program.cs index 00c5746bf..50274fc62 100644 --- a/src/Microsoft.TryDotNet/Program.cs +++ b/src/Microsoft.TryDotNet/Program.cs @@ -5,6 +5,8 @@ using System.Reflection; using System.Text; using System.Text.Json; +using Azure.Monitor.OpenTelemetry.AspNetCore; +using Azure.Monitor.OpenTelemetry.Profiler; using Microsoft.DotNet.Interactive; using Microsoft.DotNet.Interactive.Connection; using Microsoft.DotNet.Interactive.CSharpProject; @@ -29,6 +31,13 @@ public static async Task Main(string[] args) { StartLogging(); + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + { + Console.WriteLine( + "WARNING: APPLICATIONINSIGHTS_CONNECTION_STRING is not set. " + + "Application Insights telemetry and Profiler will be inactive."); + } + await EnsurePrebuildIsReadyAsync(); var app = await CreateWebApplicationAsync(new WebApplicationOptions { Args = args }); @@ -64,6 +73,10 @@ public static async Task CreateWebApplicationAsync(WebApplicatio }); }); + builder.Services.AddOpenTelemetry() + .UseAzureMonitor() + .AddAzureMonitorProfiler(); + builder.Services.AddResponseCompression(compressionOptions => { compressionOptions.EnableForHttps = true; diff --git a/src/Microsoft.TryDotNet/appsettings.json b/src/Microsoft.TryDotNet/appsettings.json index 10f68b8c8..738e132d7 100644 --- a/src/Microsoft.TryDotNet/appsettings.json +++ b/src/Microsoft.TryDotNet/appsettings.json @@ -3,6 +3,12 @@ "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" + }, + "OpenTelemetry": { + "LogLevel": { + "Default": "Warning", + "Microsoft.ServiceProfiler": "Information" + } } }, "AllowedHosts": "*" From 93a449ad27f6e8ee44bce82a0b4a875446f3977b Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 14 May 2026 22:46:56 -0700 Subject: [PATCH 2/4] fix: correct OTel profiler logger category in appsettings.json Add 'Azure.Monitor.OpenTelemetry.Profiler' category to OpenTelemetry log provider section. The OTel profiler emits startup/session logs under 'Azure.Monitor.OpenTelemetry.Profiler.*' (not 'Microsoft.ServiceProfiler' which is the classic AI SDK category). Without this, profiler startup confirmation logs were suppressed in Azure Monitor (still visible in container stdout via the global Default:Information, but not exported). Keep 'Microsoft.ServiceProfiler' as the README troubleshooting guide recommends it for internal diagnostics from the underlying profiler agent. --- src/Microsoft.TryDotNet/appsettings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Microsoft.TryDotNet/appsettings.json b/src/Microsoft.TryDotNet/appsettings.json index 738e132d7..45735af07 100644 --- a/src/Microsoft.TryDotNet/appsettings.json +++ b/src/Microsoft.TryDotNet/appsettings.json @@ -7,6 +7,7 @@ "OpenTelemetry": { "LogLevel": { "Default": "Warning", + "Azure.Monitor.OpenTelemetry.Profiler": "Information", "Microsoft.ServiceProfiler": "Information" } } From 005df1e9aa4fe173b44a6ab28baa1b90f95a7500 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 14 May 2026 22:56:34 -0700 Subject: [PATCH 3/4] fix: guard UseAzureMonitor() behind connection string check UseAzureMonitor() throws InvalidOperationException when APPLICATIONINSIGHTS_CONNECTION_STRING is not set, which breaks CI test runs and local dev environments without Azure Monitor configured. Wrap the OTel registration in a null/empty check so it's a no-op when the connection string is absent. The startup warning in Main() already informs operators when telemetry is inactive. --- src/Microsoft.TryDotNet/Program.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.TryDotNet/Program.cs b/src/Microsoft.TryDotNet/Program.cs index 50274fc62..ee2f00d82 100644 --- a/src/Microsoft.TryDotNet/Program.cs +++ b/src/Microsoft.TryDotNet/Program.cs @@ -73,9 +73,12 @@ public static async Task CreateWebApplicationAsync(WebApplicatio }); }); - builder.Services.AddOpenTelemetry() - .UseAzureMonitor() - .AddAzureMonitorProfiler(); + if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + { + builder.Services.AddOpenTelemetry() + .UseAzureMonitor() + .AddAzureMonitorProfiler(); + } builder.Services.AddResponseCompression(compressionOptions => { From 40eaefd7c178290974be55c78a3d9024ec1aec62 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Thu, 14 May 2026 23:13:49 -0700 Subject: [PATCH 4/4] refactor: extract env var name to const, reference from both call sites Avoids silent drift if the warning check and OTel guard ever used different strings. Both callers now use AppInsightsConnectionStringEnvVar. --- src/Microsoft.TryDotNet/Program.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.TryDotNet/Program.cs b/src/Microsoft.TryDotNet/Program.cs index ee2f00d82..ed4a23cde 100644 --- a/src/Microsoft.TryDotNet/Program.cs +++ b/src/Microsoft.TryDotNet/Program.cs @@ -24,6 +24,8 @@ namespace Microsoft.TryDotNet; public class Program { + private const string AppInsightsConnectionStringEnvVar = "APPLICATIONINSIGHTS_CONNECTION_STRING"; + private static Prebuild? _consolePrebuild; private static bool _loggingEnabled; @@ -31,10 +33,10 @@ public static async Task Main(string[] args) { StartLogging(); - if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(AppInsightsConnectionStringEnvVar))) { Console.WriteLine( - "WARNING: APPLICATIONINSIGHTS_CONNECTION_STRING is not set. " + + $"WARNING: {AppInsightsConnectionStringEnvVar} is not set. " + "Application Insights telemetry and Profiler will be inactive."); } @@ -73,7 +75,7 @@ public static async Task CreateWebApplicationAsync(WebApplicatio }); }); - if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(AppInsightsConnectionStringEnvVar))) { builder.Services.AddOpenTelemetry() .UseAzureMonitor()