Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ private IHost CreateStdioHost(ServiceStartOptions serverOptions)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.ConfigureOpenTelemetryLogger();
logging.AddEventSourceLogger();

if (serverOptions.Debug)
Expand Down Expand Up @@ -451,7 +450,6 @@ private IHost CreateHttpHost(ServiceStartOptions serverOptions)

// Configure logging
builder.Logging.ClearProviders();
builder.Logging.ConfigureOpenTelemetryLogger();
builder.Logging.AddEventSourceLogger();
builder.Logging.AddConsole();
ConfigureSupportLogging(builder.Logging, serverOptions);
Expand Down Expand Up @@ -629,7 +627,6 @@ private IHost CreateIncomingAuthDisabledHttpHost(ServiceStartOptions serverOptio

// Configure logging
builder.Logging.ClearProviders();
builder.Logging.ConfigureOpenTelemetryLogger();
builder.Logging.AddEventSourceLogger();
builder.Logging.AddConsole();
ConfigureSupportLogging(builder.Logging, serverOptions);
Expand Down
53 changes: 39 additions & 14 deletions core/Azure.Mcp.Core/src/Extensions/OpenTelemetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Mcp.Core.Commands;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
Expand Down Expand Up @@ -48,14 +49,6 @@ public static void ConfigureOpenTelemetry(this IServiceCollection services)
EnableAzureMonitor(services);
}

public static void ConfigureOpenTelemetryLogger(this ILoggingBuilder builder)
{
builder.AddOpenTelemetry(logger =>
{
logger.AddProcessor(new TelemetryLogRecordEraser());
});
}

private static void EnableAzureMonitor(this IServiceCollection services)
{
#if DEBUG
Expand Down Expand Up @@ -92,7 +85,7 @@ private static void EnableAzureMonitor(this IServiceCollection services)
if (!string.IsNullOrWhiteSpace(userProvidedAppInsightsConnectionString))
{
// Configure telemetry to be sent to user-provided Application Insights instance regardless of build configuration.
ConfigureAzureMonitorExporter(otelBuilder, userProvidedAppInsightsConnectionString, "UserProvided");
ConfigureUserProvidedAzureMonitorExporter(otelBuilder, userProvidedAppInsightsConnectionString);
}

// Configure Microsoft-owned telemetry only in RELEASE builds to avoid polluting telemetry during development.
Expand All @@ -105,7 +98,7 @@ private static void EnableAzureMonitor(this IServiceCollection services)

if (shouldCollectMicrosoftTelemetry)
{
ConfigureAzureMonitorExporter(otelBuilder, MicrosoftOwnedAppInsightsConnectionString, "Microsoft");
ConfigureMicrosoftAzureMonitorExporter(otelBuilder, MicrosoftOwnedAppInsightsConnectionString);
}
#endif

Expand All @@ -118,15 +111,47 @@ private static void EnableAzureMonitor(this IServiceCollection services)
}
}

private static void ConfigureAzureMonitorExporter(OpenTelemetry.OpenTelemetryBuilder otelBuilder, string appInsightsConnectionString, string name)
/// <summary>
/// Configures OpenTelemetry to use Azure Monitor exporters with Microsoft's Application Insights instance.
/// </summary>
/// <param name="otelBuilder">The OpenTelemetry builder to configure.</param>
/// <param name="appInsightsConnectionString">The Application Insights connection string for Microsoft's telemetry instance.</param>
private static void ConfigureMicrosoftAzureMonitorExporter(OpenTelemetry.OpenTelemetryBuilder otelBuilder, string appInsightsConnectionString)
{
// We don't configure logging for Microsoft telemetry to avoid sending potentially sensitive log data to Microsoft.
otelBuilder.WithMetrics(metrics =>
{
metrics.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.Microsoft);
});

otelBuilder.WithTracing(tracing =>
{
tracing.AddAzureMonitorTraceExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: AppInsightsInstanceType.Microsoft);
});
}

/// <summary>
/// Configures OpenTelemetry to use Azure Monitor exporters with a user-provided Application Insights connection string.
/// </summary>
/// <param name="otelBuilder">The OpenTelemetry builder to configure.</param>
/// <param name="appInsightsConnectionString">The Application Insights connection string provided by the user.</param>
private static void ConfigureUserProvidedAzureMonitorExporter(OpenTelemetry.OpenTelemetryBuilder otelBuilder, string appInsightsConnectionString)
{
otelBuilder.WithLogging(logging =>
{
logging.AddAzureMonitorLogExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
},
name: name);
name: AppInsightsInstanceType.UserProvided);
});

otelBuilder.WithMetrics(metrics =>
Expand All @@ -135,7 +160,7 @@ private static void ConfigureAzureMonitorExporter(OpenTelemetry.OpenTelemetryBui
{
options.ConnectionString = appInsightsConnectionString;
},
name: name);
name: AppInsightsInstanceType.UserProvided);
});

otelBuilder.WithTracing(tracing =>
Expand All @@ -144,7 +169,7 @@ private static void ConfigureAzureMonitorExporter(OpenTelemetry.OpenTelemetryBui
{
options.ConnectionString = appInsightsConnectionString;
},
name: name);
name: AppInsightsInstanceType.UserProvided);
});
}
}
6 changes: 6 additions & 0 deletions core/Microsoft.Mcp.Core/src/Commands/TelemetryConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ public class ActivityName
public const string ToolExecuted = "ToolExecuted";
public const string ServerStarted = "ServerStarted";
}

public class AppInsightsInstanceType
{
public const string Microsoft = "Microsoft";
public const string UserProvided = "UserProvided";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pr: 1638
changes:
- section: "Features Added"
description: "Add log telemetry support for customer-owned AppInsights"
1 change: 0 additions & 1 deletion servers/Azure.Mcp.Server/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private static async Task<int> Main(string[] args)

services.AddLogging(builder =>
{
builder.ConfigureOpenTelemetryLogger();
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
Expand Down
1 change: 0 additions & 1 deletion servers/Fabric.Mcp.Server/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private static async Task<int> Main(string[] args)

services.AddLogging(builder =>
{
builder.ConfigureOpenTelemetryLogger();
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
Expand Down
1 change: 0 additions & 1 deletion servers/Template.Mcp.Server/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private static async Task<int> Main(string[] args)

services.AddLogging(builder =>
{
builder.ConfigureOpenTelemetryLogger();
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
Expand Down