From 780b835370116b01cebce0cc3c592005f8e4f567 Mon Sep 17 00:00:00 2001 From: Loi Nguyen Date: Fri, 31 Jul 2026 02:34:25 +0700 Subject: [PATCH] docs: add Application Insights telemetry example --- Directory.Packages.props | 1 + .../AspNetCoreMcpServer.csproj | 1 + samples/AspNetCoreMcpServer/Program.cs | 19 ++++++-- samples/AspNetCoreMcpServer/README.md | 48 +++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 samples/AspNetCoreMcpServer/README.md diff --git a/Directory.Packages.props b/Directory.Packages.props index d204d871f..3d25bff04 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -64,6 +64,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/samples/AspNetCoreMcpServer/AspNetCoreMcpServer.csproj b/samples/AspNetCoreMcpServer/AspNetCoreMcpServer.csproj index 29aebc0d2..ca6411997 100644 --- a/samples/AspNetCoreMcpServer/AspNetCoreMcpServer.csproj +++ b/samples/AspNetCoreMcpServer/AspNetCoreMcpServer.csproj @@ -15,6 +15,7 @@ + diff --git a/samples/AspNetCoreMcpServer/Program.cs b/samples/AspNetCoreMcpServer/Program.cs index f35d0efec..3441083bb 100644 --- a/samples/AspNetCoreMcpServer/Program.cs +++ b/samples/AspNetCoreMcpServer/Program.cs @@ -1,3 +1,4 @@ +using Azure.Monitor.OpenTelemetry.AspNetCore; using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; @@ -34,15 +35,27 @@ .WithTools() .WithResources(); -builder.Services.AddOpenTelemetry() +var telemetry = builder.Services.AddOpenTelemetry() .WithTracing(b => b.AddSource("*") .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation()) .WithMetrics(b => b.AddMeter("*") .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation()) - .WithLogging() - .UseOtlpExporter(); + .WithLogging(); + +string? applicationInsightsConnectionString = + builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]; + +if (string.IsNullOrWhiteSpace(applicationInsightsConnectionString)) +{ + telemetry.UseOtlpExporter(); +} +else +{ + telemetry.UseAzureMonitor(options => + options.ConnectionString = applicationInsightsConnectionString); +} // Configure HttpClientFactory for weather.gov API builder.Services.AddHttpClient("WeatherApi", client => diff --git a/samples/AspNetCoreMcpServer/README.md b/samples/AspNetCoreMcpServer/README.md new file mode 100644 index 000000000..9ec124df4 --- /dev/null +++ b/samples/AspNetCoreMcpServer/README.md @@ -0,0 +1,48 @@ +# ASP.NET Core MCP server sample + +This sample hosts an MCP server with Streamable HTTP and configures OpenTelemetry for MCP operations, ASP.NET Core requests, outgoing HTTP requests, metrics, and structured logs. + +## Export to Application Insights + +Set the Application Insights connection string and run the sample: + +```bash +export APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=;IngestionEndpoint=https://.in.applicationinsights.azure.com/' +dotnet run --project samples/AspNetCoreMcpServer +``` + +PowerShell: + +```powershell +$env:APPLICATIONINSIGHTS_CONNECTION_STRING = 'InstrumentationKey=;IngestionEndpoint=https://.in.applicationinsights.azure.com/' +dotnet run --project samples/AspNetCoreMcpServer +``` + +The connection string is read through ASP.NET Core configuration. Keep it outside source control and use your deployment platform's secret configuration in production. + +When `APPLICATIONINSIGHTS_CONNECTION_STRING` is absent or empty, the sample retains its default OTLP exporter. Configure that path with standard `OTEL_EXPORTER_OTLP_*` environment variables. + +## Signals and MCP attributes + +Application Insights receives the signals already emitted by the SDK and the configured ASP.NET Core/OpenTelemetry instrumentation: + +- server activities appear as requests and client activities appear as dependencies; +- structured logs appear as traces; +- MCP histograms appear as custom metrics; +- HTTP server and client instrumentation provides the surrounding transport spans. + +MCP activities can include attributes such as `mcp.method.name`, `mcp.protocol.version`, `mcp.session.id`, `jsonrpc.request.id`, and `gen_ai.tool.name`. The exact attributes depend on the operation and whether sensitive-data telemetry is enabled. + +For example, query MCP request and dependency telemetry in Logs: + +```kusto +union withsource=TelemetryType requests, dependencies +| extend McpMethod = tostring(customDimensions["mcp.method.name"]), + ToolName = tostring(customDimensions["gen_ai.tool.name"]), + McpSession = tostring(customDimensions["mcp.session.id"]) +| where isnotempty(McpMethod) +| project timestamp, TelemetryType, name, success, resultCode, McpMethod, ToolName, McpSession +| order by timestamp desc +``` + +Invalid tool calls, malformed JSON-RPC messages, and other failures appear only when the current SDK or ASP.NET Core instrumentation emits a log or activity for that path. This sample exports existing telemetry; it does not add custom protocol events or per-tool instrumentation.