Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion EssentialCSharp.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using System.Runtime.InteropServices;
using System.Threading.RateLimiting;
using ModelContextProtocol.Protocol;
using EssentialCSharp.Chat.Common.Extensions;
Expand Down Expand Up @@ -51,6 +52,8 @@ private static void Main(string[] args)
string? appInsightsConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
bool useAzureMonitor = !string.IsNullOrWhiteSpace(appInsightsConnectionString);
bool useOtlp = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
bool profilerSupportedPlatform = OperatingSystem.IsWindows() || OperatingSystem.IsLinux();
bool profilerSkippedUnsupportedPlatform = false;

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

if (useAzureMonitor)
otel.UseAzureMonitor().AddAzureMonitorProfiler();
{
// Azure Monitor export is supported cross-platform, but the profiler currently only
// supports Windows and Linux.
var azureMonitor = otel.UseAzureMonitor();
if (profilerSupportedPlatform)
azureMonitor.AddAzureMonitorProfiler();
else
profilerSkippedUnsupportedPlatform = true;
}
else if (useOtlp)
otel.UseOtlpExporter();

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

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

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

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

[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.")]
private static partial void LogSkippingUnsupportedAzureMonitorProfiler(ILogger<Program> logger, string platform);
}
Loading