Skip to content

Commit 2b3aa37

Browse files
fix: Skip Azure Monitor profiler on unsupported OS (#1113)
## Why AddAzureMonitorProfiler() is only supported on Windows and Linux. On macOS, enabling Azure Monitor caused startup to fail with System.NotSupportedException. ## What changed - Gate profiler registration to supported platforms (OperatingSystem.IsWindows() or OperatingSystem.IsLinux()). - Keep UseAzureMonitor() enabled so telemetry export still works cross-platform. - Add a structured warning log when profiler registration is skipped on unsupported platforms. ## Result The app no longer fails at startup on macOS due to profiler registration, while Azure Monitor telemetry remains enabled.
1 parent c90b5b7 commit 2b3aa37

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

EssentialCSharp.Web/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Claims;
2+
using System.Runtime.InteropServices;
23
using System.Threading.RateLimiting;
34
using ModelContextProtocol.Protocol;
45
using EssentialCSharp.Chat.Common.Extensions;
@@ -51,6 +52,8 @@ private static void Main(string[] args)
5152
string? appInsightsConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
5253
bool useAzureMonitor = !string.IsNullOrWhiteSpace(appInsightsConnectionString);
5354
bool useOtlp = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
55+
bool profilerSupportedPlatform = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
56+
bool profilerSkippedUnsupportedPlatform = false;
5457

5558
builder.Logging.AddOpenTelemetry(logging =>
5659
{
@@ -91,7 +94,15 @@ private static void Main(string[] args)
9194
});
9295

9396
if (useAzureMonitor)
94-
otel.UseAzureMonitor().AddAzureMonitorProfiler();
97+
{
98+
// Azure Monitor export is supported cross-platform, but the profiler currently only
99+
// supports Windows and Linux.
100+
var azureMonitor = otel.UseAzureMonitor();
101+
if (profilerSupportedPlatform)
102+
azureMonitor.AddAzureMonitorProfiler();
103+
else
104+
profilerSkippedUnsupportedPlatform = true;
105+
}
95106
else if (useOtlp)
96107
otel.UseOtlpExporter();
97108

@@ -127,6 +138,8 @@ private static void Main(string[] args)
127138
var loggerFactory = LoggerFactory.Create(loggingBuilder =>
128139
loggingBuilder.AddConsole().SetMinimumLevel(LogLevel.Information));
129140
var initialLogger = loggerFactory.CreateLogger<Program>();
141+
if (profilerSkippedUnsupportedPlatform)
142+
LogSkippingUnsupportedAzureMonitorProfiler(initialLogger, RuntimeInformation.OSDescription);
130143

131144
builder.Services.AddDbContext<EssentialCSharpWebContext>(options => options.UseSqlServer(connectionString, sql => sql.EnableRetryOnFailure(5)));
132145

@@ -623,4 +636,7 @@ private static bool IsMcpTransportRequest(HttpRequest request) =>
623636

624637
[LoggerMessage(Level = LogLevel.Warning, Message = "Ignoring invalid TryDotNet origin in CSP: {Origin}")]
625638
private static partial void LogIgnoringInvalidTryDotNetOrigin(ILogger logger, string origin);
639+
640+
[LoggerMessage(Level = LogLevel.Warning, Message = "Azure Monitor profiler is not supported on this platform ({Platform}). Skipping profiler registration and continuing with Azure Monitor telemetry export.")]
641+
private static partial void LogSkippingUnsupportedAzureMonitorProfiler(ILogger<Program> logger, string platform);
626642
}

0 commit comments

Comments
 (0)